jsp三大作用域总结:
Application:
基于整个web服务器,也是最大的作用域,在该作用域中保存的数据整个web应用可见,从服务器开始到结束一直有效。
适用于保存一些公开可见的信息,例如网页访问人数等;
不适合保存一些私密信息,如用户信息等。
request.getServletContext().setAttribute("参数名","参数值");//servlet获取Application对象并传入数据
Application.getAttribute("参数名")//jsp通过参数名获取数据
Session:
基于一次会话,一次会话可发送多次请求,服务器根据cookie区分不同客户端,如果禁用cookie,session会失效。
session一次有效期为30分钟,如果过期,需重新刷新页面,数据丢失。、
可在web.xml文件中设置时间(单位:分钟)
<session-config>
<session-timeout>1</session-timeout>//过期时间,单位(min)
</session-config>
也可通过代码实现:
<%session.setMaxInactiveInterval(60);%> //单位:秒
request:
基于一次请求的:
如果request保存数据,则不能用重定向发送数据,否则会丢失数据。
需要用请求转发的方式发送。
request.setAttribute()//保存数据
request.getRequestDispatcher("路径").forward(request,response); //请求转发
response.sendRedirect("路径");//重定向
请求转发:
请求转发,即request.getRequestDispatcher().forward(),是一种服务器的行为,客户端只有一次请求,服务器端转发后会将请求对象保存,地址栏中的URL地址不会改变,得到响应后服务器端再将响应发给客户端;
请求重定向:
请求重定向,即response.sendRedirect(),是一种客户端行文,从本质上讲等同于两次请求,前一次请求对象不会保存,地址栏的URL地址会改变。
exp:
我们制作一个简单小demo,由index.jsp页面传回用户提交的数据,提交给servlet代码进行处理,分别保存在不同的作用域中,再由welcome.jsp读取数据,分别在不同的客户端页面读取,观察效果。
Application和session:
servlet分别保存数据到application和session:
提交数据
进入欢迎页面:
application数据:张三
session数据:李四
开启另一个页面查看数据(同一浏览器):
application数据可读取
session数据可读取
开启另一个页面查看(不同浏览器):
application数据可读取
session数据不可读取(null)
request:
servlet保存数据到request作用域:
提交数据
进入欢迎页面:
request数据为:王五
开启另一个页面查看数据(同一浏览器):
request数据不可读取(null)
开启另一个页面查看(不同浏览器):
request数据不可读取(null)
总结:(浏览器==客户端)
Application:不同浏览器不同页面数据共享
session:同一浏览器不同页面数据共享
request:只在当前页面能读取数据
servlet生命周期总结:
代码测试:
附代码:
@WebServlet(urlPatterns = "/servlet.do")
public class ServletTimeTest extends HttpServlet {
public ServletTimeTest() {
System.out.println("无参构造方法");
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("doget方法");
System.out.println("do-get:"+req.getParameter("name3"));
resp.sendRedirect("welcome.jsp");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF-8");
System.out.println("dopost方法");
System.out.println("do-post:"+req.getParameter("name3"));
resp.sendRedirect("welcome.jsp");
}
@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
System.out.println("service方法");
super.service(req,res);//调用父类service方法,来判断当前请求为post还是get,进而判断执行哪种方法
}
@Override
public void destroy() {
System.out.println("destroy方法");
}
@Override
public void init(ServletConfig config) throws ServletException {
System.out.println("init带参方法");
super.init(config);//带参构造初始化方法由tomcat执行,获取初始化信息
}
@Override
public void init() throws ServletException {
System.out.println("init无参方法");//由程序员调用
}
}
结果截图:
运行tomcat并转入servlet.do页面
结束服务器:
总结:
tomcat启动后,分为大致四个阶段:
1.创建对象(执行默认构造方法) 默认情况servlet不会随着服务器的启动而创建对象,第一次向此请求时候创建,之后请求不再创建对象,不管在哪个浏览器上发送请求。 如果在web.xml文件中配置了< load-on-startup>1< /load-on-startup>,会随着服务器的启动而创建对象。
2.初始化(init)(先执行带参init方法(tomcat使用),在执行无参init方法(程序员使用)) 在构造方法中会调用init方法,由于对象只创建一次,则在创建对象时执行构造函数,只会进行一次初始化,init只会执行一次。
3.处理请求(执行service方法来判断当前请求种类,进而掉用doget或者dopost方法) 所有请求到达servlet后先执行service方法,在方法中根据请求方式决定执行doGet还是doPost方法。
service如何判断使用doget或者dopost方法呢?(赋源码)
我们可以看到这里源码获取到方法名然后逐一进行equals判断,进而来调用对应方法;
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String method = req.getMethod();
long lastModified;
if (method.equals("GET")) {
lastModified = this.getLastModified(req);
if (lastModified == -1L) {
this.doGet(req, resp);
} else {
long ifModifiedSince;
try {
ifModifiedSince = req.getDateHeader("If-Modified-Since");
} catch (IllegalArgumentException var9) {
ifModifiedSince = -1L;
}
if (ifModifiedSince < lastModified / 1000L * 1000L) {
this.maybeSetLastModified(resp, lastModified);
this.doGet(req, resp);
} else {
resp.setStatus(304);
}
}
} else if (method.equals("HEAD")) {
lastModified = this.getLastModified(req);
this.maybeSetLastModified(resp, lastModified);
this.doHead(req, resp);
} else if (method.equals("POST")) {
this.doPost(req, resp);
} else if (method.equals("PUT")) {
this.doPut(req, resp);
} else if (method.equals("DELETE")) {
this.doDelete(req, resp);
} else if (method.equals("OPTIONS")) {
this.doOptions(req, resp);
} else if (method.equals("TRACE")) {
this.doTrace(req, resp);
} else {
String errMsg = lStrings.getString("http.method_not_implemented");
Object[] errArgs = new Object[]{method};
errMsg = MessageFormat.format(errMsg, errArgs);
resp.sendError(501, errMsg);
}
}
4.销毁(destory)(调用destory方法) 重新发布项目,正常关闭tomcat时,会执行此方法,如果非正常关闭,相当于程序中断,不会执行方法。