两个Servlet 一个用于初始化转发 一个用于显示 以下为第一个
package com.test; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; public class Tip extends HttpServlet { /** * @author Administrator */ private static final long serialVersionUID = 1L; @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setAttribute("request", "Request"); HttpSession session = request.getSession(); // 秒为单位,单个session的生存时长设置, // 优先级高于Web.xml的全局设置,默认为TomCat服务器的30min,其他服务器略有不同 session.setMaxInactiveInterval(300); session.setAttribute("session", "Session"); ServletContext servletContext = getServletContext(); servletContext.setAttribute("context", "Context"); response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); out.println("Tip1已运行"); request.getRequestDispatcher("tip2").forward(request, response); } }
第二个显示Servlet
package com.test; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; public class Tip2 extends HttpServlet { /** * @author Administrator */ private static final long serialVersionUID = 1L; @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String rv = (String) request.getAttribute("request"); HttpSession session = request.getSession(); String sv = (String) session.getAttribute("session"); ServletContext servletContext = getServletContext(); String cv = (String) servletContext.getAttribute("context"); System.out.println("Request值为" + rv); System.out.println("Session值为" + sv); System.out.println("Context值为" + cv); System.out.println("----分界线----"); response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); out.println("Tip2已运行"); out.flush(); } }
XML文档
<servlet> <servlet-name>Tip</servlet-name> <servlet-class>com.test.Tip</servlet-class> </servlet> <servlet-mapping> <servlet-name>Tip</servlet-name> <url-pattern>/tip</url-pattern> </servlet-mapping> <servlet> <servlet-name>Tip2</servlet-name> <servlet-class>com.test.Tip2</servlet-class> </servlet> <servlet-mapping> <servlet-name>Tip2</servlet-name> <url-pattern>/tip2</url-pattern> </servlet-mapping>
直接访问tip并转发,同一请求,Request不变
显示如下
Request值为Request Session值为Session Context值为Context ----分界线----
同窗口直接访问tip2,Request为空,其他正常
Request值为null Session值为Session Context值为Context ----分界线----
同项目换浏览器直接访问tip2,只有Context存在
Request值为null Session值为null Context值为Context ----分界线----
xml中定义session的存活时间,需要浏览器cookies开启支持
<session-config> <session-timeout>30</session-timeout> </session-config>
Anr-C 2016.10.21
本站由以下主机服务商提供服务支持:
0条评论