学习笔记输出来源:拉勾教育Java就业急训营
修改时间:2021年1月29日
作者:pp_x
邮箱:[email protected]
文章目录
Servlet的核心技术
Servlet和JDBC的应用
- 在Servlet中可以使用JDBC技术访问数据库,常见功能如下:
- 查询DB数据,然后生成显示页面,例如:列表显示功能。
- 接收请求参数,然后对DB操作,例如:注册、登录、修改密码等功能。
- 为了方便重用和便于维护等目的,经常会采用DAO(Data Access Object)模式对数据库操作进行独立封装。
- **DAO工厂(工厂模式) **
- 工厂类:封装了对象的创建细节,为调用者提供符合要求的对象。
对象类
package com.lagou.demo01.model;
public class User {
private int id;
private String userName;
private String password;
public User() {
}
public User(String userName, String password) {
this.userName = userName;
this.password = password;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"userName='" + userName + '\'' +
", password='" + password + '\'' +
'}';
}
}
注册类
public class RegisterServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1、获取请求对象中保存的用户名和密码信息
String userName = request.getParameter("userName");
System.out.println(userName);
String password = request.getParameter("password");
System.out.println(password);
//2、将接受的用户名和密码信息打包成用户对象交给Dao层
User user = new User(userName,password);
UserDao userDao = new UserDao();
int res = userDao.creatUser(user);
//3、将结果响应到浏览器
// 设置浏览器的响应的编码
response.setContentType("text/html;charset=utf-8");
PrintWriter writer = response.getWriter();
if (res == 1){
writer.write("<script>alert('注册成功')</script>");
}else {
writer.write("<script>alert('注册失败')</script>");
}
writer.close();
}
}
Dao工厂
package com.lagou.demo01.dao;
import com.lagou.demo01.util.DbUtil;
import com.lagou.demo01.model.User;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class UserDao {
public int creatUser(User user){
Connection con = null;
PreparedStatement preparedStatement = null;
try {
// 1、获取连接
con = DbUtil.getCon();
//2、准备一个sql语句
String sql = "insert into t_user values(null,?,?);";
//3、获取PreparedStatement类型的引用
preparedStatement = con.prepareStatement(sql);
//4、 向占位符赋值
preparedStatement.setString(1,user.getUserName());
preparedStatement.setString(2,user.getPassword());
//5、执行sql语句
int row = preparedStatement.executeUpdate();
return row;
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
DbUtil.closeCon(con,preparedStatement);
} catch (SQLException e) {
e.printStackTrace();
}
}
return 0;
}
}
dbutil工具类
* 数据库工具类
*/
public class DbUtil {
private static String jdbcName;
private static String dbUrl;
private static String dbUserName;
private static String dbPassword;
static {
jdbcName = "com.mysql.jdbc.Driver";
dbUrl = "jdbc:mysql://localhost:3306/db_web";
dbUserName = "root";
dbPassword = "root";
try {
Class.forName(jdbcName);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getCon() throws SQLException {
Connection connection = DriverManager.getConnection(dbUrl, dbUserName, dbPassword);
return connection;
}
public static void closeCon(Connection connection, Statement statement) throws SQLException {
if (connection !=null){
connection.close();
}
if (connection !=null){
statement.close();
}
}
}
重定向和转发
重定的概念
- 首先客户浏览器发送http请求,当web服务器接受后发送302状态码响应及对应新的location给客户浏览器,客户浏览器发现是302响应,则自动再发送一个新的http请求,请求url是新的location地址,服务器根据此请求寻找资源并发送给客户
重定向的实现
- 实现重定向需要借助
javax.servlet.http.HttpServletResponse
接口中的以下方法:void sendRedirect(String location)
:使用指定的重定向位置URL向客户端发送临时重定向响应(Servlet、JSP、HTML)
重定向的原理
重定向的特点
- 浏览器地址栏的URL会发生改变。
- 重定向过程中会将前面Request对象销毁,然后创建一个新的Request对象
- 重定向的URL可以是其它项目工程。(跨工程)
转发
转发的概念
- 一个Web组件(Servlet/JS)将未完成的处理通过容器转交给另外一个Web组件继续处理,转发的各个组件会共享Request和Response对象
Servlet
public class RedirectServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//response.sendRedirect("target.html");
System.out.println(request.getContextPath());
response.sendRedirect(/*request.getContextPath()+*/"TargetServlet");
}
}
html
<body>
<form action="redirectServlet" method="post">
<input type="submit" value="重定向"/>
</form>
xml
<servlet>
<servlet-name>RedirectServlet</servlet-name>
<servlet-class>com.lagou.demo02.servlet.RedirectServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RedirectServlet</servlet-name>
<url-pattern>/redirectServlet</url-pattern>
</servlet-mapping>
转发的实现
- 绑定数据到Request对象
Object getAttribute(String name)
:将指定属性值作为对象返回,若给定名称属性不存在,则返回空值void setAttribute(String name,Object o)
:在此请求中存储属性值
- 获取转发器对象
RequestDispatcher getRequestDispatcher(String path)
:返回一个RequestDispatcher对象,该对象充当位于给定路径上的资源的包装器
- 转发操作
void forward(ServletRequest request, ServletResponse response)
:将请求从一个servlet转发到服务器上的另一个资源(Servlet、JSP文件或HTML文件)
转发的特点
- 转发之后浏览器地址栏的URL不会发生改变
- 转发过程中共享Request对象
- 转发的URL不可以是其它项目工程
Servlet
public class ForwardServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("接收到了浏览器的请求");
//转发 也就是让web组件将任务转发给另一个web组件
//获取调度器
RequestDispatcher requestDispatcher = request.getRequestDispatcher("/TargetServlet");
requestDispatcher.forward(request,response);
}
}
public class TargetServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("转发过来啦");
}
}
html
<body>
<form action="forwardServlet" method="post">
<input type="submit" value="转发"/>
</form>
</body>
xml
<servlet>
<servlet-name>ForwardServlet</servlet-name>
<servlet-class>com.lagou.demo02.servlet.ForwardServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ForwardServlet</servlet-name>
<url-pattern>/forwardServlet</url-pattern>
</servlet-mapping>
重定向和转发的比较
- 客户端进行跳转就是重定向(类似登录成功后的操作)
- 服务器进行跳转就是转发
Servlet线程安全
- 服务器在收到请求之后,会启动一个线程来进行相应的请求处理。
- 默认情况下,服务器为每个Servlet只创建一个对象实例。当多个请求访问同一个Servlet时,会有多个线程访问同一个Servlet对象,此时就可能发生线程安全问题。
- 多线程并发逻辑,需要使用synchronized对代码加锁处理,但尽量避免使用。
public class ThreadServlet extends HttpServlet {
private String name ;//全局变量会产生多线程
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
synchronized (this){
name = request.getParameter("name");
System.out.println("name="+name);
PrintWriter writer = response.getWriter();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
writer.write("<h1>"+name+"</h1>");
}
}
}
html
<body>
<iframe width="600px" height="100px" src="thread?name=zhangfei"></iframe><br/>
<iframe width="600px" height="100px" src="thread?name=guanyu"></iframe><br/>
<iframe width="600px" height="100px" src="thread?name=liubei"></iframe><br/>
</body>
状态管理
- 把浏览器与服务器之间多次交互作为一个整体,将多次交互所涉及的数据保存下来,即状态管理。
- 客户端管理:将状态保存在客户端。基于Cookie技术实现。
- 服务器管理:将状态保存在服务器端。基于Session技术实现
Cookie技术
基本概念
- Cookie本意为”饼干“的含义,在这里表示客户端以“名-值”形式进行保存的一种技术。
- 浏览器向服务器发送请求时,服务器将数据以Set-Cookie消息头的方式响应给浏览器,然后浏览器会将这些数据以文本文件的方式保存起来。
- 当浏览器再次访问服务器时,会将这些数据以Cookie消息头的方式发送给服务器。
常用的方法
- 使用javax.servlet.http.Cookie类的构造方法实现Cookie的创建。
Cookie(String name, String value)
:根据参数指定数值构造对象
- 使用javax.servlet.http.HttpServletResponse接口的成员方法实现Cookie的添加
void addCookie(Cookie cookie)
:添加参数指定的对象到响应
- 使用javax.servlet.http.HttpServletRequest接口的成员方法实现Cookie对象的获取
Cookie[] getCookies()
:返回此请求中包含的所有Cookie对象
- 使用javax.servlet.http.Cookie类的构造方法实现Cookie对象中属性的获取和修改
String getName()
:获取Cookie对象的名字String getValue()
:获取Cookie对象的值void setValue(String newValue)
:修改Cookie对象的值
@WebServlet(name = "CookieServlet", urlPatterns = "/cookie")
public class CookieServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//创建cookie对象并添加到响应信息中
Cookie cookie = new Cookie("name","liuxiao");
response.addCookie(cookie);
Cookie[] cookies = request.getCookies();//获取cookie对象
for (Cookie c:cookies
) {
System.out.println(c.getName()+c.getValue());
if ("name".equalsIgnoreCase(c.getName())){
c.setValue("xiaoliu");
System.out.println(c.getValue());//获取cookie的名字和值
}
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
}
Cookie的生命周期
- 默认情况下,浏览器会将Cookie信息保存在内存中,只要浏览器关闭,Cookie信息就会消失
- 获取何设置保留时间的方法:
int getMaxAge()
:返回cookie的最长使用期限(以秒为单位)void setMaxAge(int expiry)
: 设置cookie的最长保留时间(秒)
@WebServlet(name = "CookieServlet2", urlPatterns = "/cookie2")
public class CookieServlet2 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1、创建cookie信息
Cookie cookie = new Cookie("name","shuaige");
//2、获取cookie信息默认使用时间
int maxAge = cookie.getMaxAge();
System.out.println("默认时间为:"+maxAge);
//3、设置cook的使用期限
//负数表示浏览器关闭后失效 0表示立即失效 正数表示指定的秒数
cookie.setMaxAge(1000);
//4、添加到响应头中
response.addCookie(cookie);
System.out.println("设置成功");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
}
Cookie的路径问题
- 浏览器在访问服务器时,会比较Cookie的路径与请求路径是否匹配,只有匹配的Cookie才会发送给服务器。(比如访问百度指挥发送百度的cookie给服务器)
- Cookie的默认路径等于添加这个Cookie信息时的组件路径,例如:
/项目名/目录/add.do
请求添加了一个Cookie信息,则该Cookie的路径是/项目名/目录
- 访问的请求地址必须符合Cookie的路径或者其子路径时,浏览器才会发送Cookie信息
void setPath(String uri)
: 设置cookie的路径信息
@WebServlet(name = "CookieServlet3", urlPatterns = "/cookie3")
public class CookieServlet3 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1、创建cookie信息
// Cookie cookie = new Cookie("name","baba");
// 2、设置cookie路径
Cookie cookie = new Cookie("name","mama");
cookie.setPath(request.getContextPath()+"/hello");
//当访问路径为request.getContextPath()+"/hello"时 浏览器才在请求头中加入baba 和mama
//当访问路径为request.getContextPath()时 浏览器只会在请求头中加入baba
//2、添加到响应头中
response.addCookie(cookie);
System.out.println("设置cookie路径成功");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
}
Cookie的特点
- Cookie技术不适合存储所有数据,程序员只用于存储少量、非敏感信息,原因如下:
- 将状态数据保存在浏览器端,不安全
- 保存数据量有限制,大约4KB左右
- 只能保存字符串信息
- 可以通过浏览器设置为禁止使用
Session技术
基本概念
- Session本意为"会话"的含义,是用来维护一个客户端和服务器关联的一种技术
- 浏览器访问服务器时,服务器会为每一个浏览器都在服务器端的内存中分配一个空间,用于创建一个Session对象,该对象有一个id属性且该值唯一,我们称为SessionId,并且服务器会将这个SessionId以Cookie方式发送给浏览器存储。
- 浏览器再次访问服务器时会将SessionId发送给服务器,服务器可以依据SessionId查找相对应的Session对象
相关的方法
- 使用javax.servlet.http.HttpServletRequest接口的成员方法实现Session的获取。
HttpSession getSession()
:返回此请求关联的当前Session,若此请求没有则创建一个
- 使用javax.servlet.http.HttpSession接口的成员方法实现判断和获取
boolean isNew()
:判断是否为新创建的SessionString getId()
:获取Session的编号
@WebServlet(name = "SessionServlet", urlPatterns = "/session")
public class SessionServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1、调用getSession方法获取或者创建Session对象
HttpSession session = request.getSession();
//2、判断此Session是否为新创建的
System.out.println(session.isNew()? "新创建的对象":"不是新创建的对象");
//3、获取SessionId
String id = session.getId();
System.out.println("session对象的id"+id);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
- 使用javax.servlet.http.HttpSession接口的成员方法实现属性的管理。
Object getAttribute(String name)
:返回在此会话中用指定名称绑定的对象,如果没有对象在该名称下绑定,则返回空值void setAttribute(String name, Object value)
:使用指定的名称将对象绑定到此会话void removeAttribute(String name)
:从此会话中删除与指定名称绑定的对象
@WebServlet(name = "SessionServlet2", urlPatterns = "/session2")
public class SessionServlet2 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
// 1.设置属性名和属性值
session.setAttribute("name", "machao");
// 2.获取指定属性名对应的属性值
System.out.println("获取到的属性值为:" + session.getAttribute("name")); // machao
// 3.删除指定的属性名
session.removeAttribute("name");
// 4.获取指定属性名对应的属性值
System.out.println("获取到的属性值为:" + session.getAttribute("name")); // null
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
Session的生命周期
- 为了节省服务器内存空间资源,服务器会将空闲时间过长的Session对象自动清除掉,服务器默认的超时限制一般是30分钟
- 设置失效时间的api
int getMaxInactiveInterval()
:获取失效时间void setMaxInactiveInterval(int interval)
设置失效时间
@WebServlet(name = "SessionServlet3", urlPatterns = "/session3")
public class SessionServlet3 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 1.获取Session对象
HttpSession session = request.getSession();
// 2.获取对象的默认失效时间并打印
int maxInactiveInterval = session.getMaxInactiveInterval();
System.out.println("获取到的失效时间为:" + maxInactiveInterval); // 1800
// 3.修改实现时间后重新获取并打印
session.setMaxInactiveInterval(1200);
maxInactiveInterval = session.getMaxInactiveInterval();
System.out.println("获取到的失效时间为:" + maxInactiveInterval); // 1200
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
- 配置web.xml的形式
<session-config>
<session-timeout>30</session-timeout>
</session-config>
Session的特点
- 数据比较安全。
- 能够保存的数据类型丰富,而Cookie只能保存字符串。
- 能够保存更多的数据,而Cookie大约保存4KB。
- 数据保存在服务器端会占用服务器的内存空间,如果存储信息过多、用户量过大,会严重影响服务器的性能。
两者区别
- cookie类似通行证
- session类似用户信息表