JavaWeb基礎 session isNew 判斷session是新生成的 還是舊有的
阿新 • • 發佈:2017-12-06
esp exception stub tomcat tty writer 服務器 def con
禮悟:
好好學習合思考,尊師重道存感恩。葉見尋根三返一,江河湖海同一體。
虛懷若谷良心主,願行無悔給最苦。讀書鍛煉強身心,誠勸且行且珍惜。
javaEE:7
javaSE:1.8
JSTL:1.2.2
server:tomcat 8.5
browser:Chrome/Firefox
os:windows7 x64
ide:MyEclipse 2017
項目結構
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>Day11</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>NewSessionServlet</servlet-name> <servlet-class>com.jizuiku.servlet.NewSessionServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>NewSessionServlet</servlet-name> <url-pattern>/NewSessionServlet</url-pattern> </servlet-mapping> <servlet> <servlet-name>OldSessionServlet</servlet-name> <servlet-class>com.jizuiku.servlet.OldSessionServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>OldSessionServlet</servlet-name> <url-pattern>/OldSessionServlet</url-pattern> </servlet-mapping> </web-app>
NewSessionServlet.java
package com.jizuiku.servlet; import java.io.IOException; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; /** * * * @author 博客園-給最苦 * @version V2017.11.21 */ public class NewSessionServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.setContentType("text/html;charset=utf-8"); // 服務器不會馬上創建session.而是 在第一次獲取session時,才會創建session。理論通的重要性! // req.getSession();獲取session if (req.getSession().isNew()) { response.getWriter().print("新生成的"); } else { response.getWriter().print("舊有的"); } } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub } }
OldSessionServlet.java
package com.jizuiku.servlet; import java.io.IOException; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; /** * * * @author 博客園-給最苦 * @version V2017.11.21 */ public class OldSessionServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.setContentType("text/html;charset=utf-8"); // 服務器不會馬上創建session.而是 在第一次獲取session時,才會創建session。理論通的重要性! // req.getSession();獲取session if (req.getSession().isNew()) { response.getWriter().print("新生成的"); } else { response.getWriter().print("舊有的"); } } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub } }
瀏覽器進行訪問
這裏要註意的是訪問的先後順序,頁面的效果與訪問的先後順序是有關的
註:
給最苦 在做這個測試的時候,web.xml沒寫好,各種出錯。
學習資源:itcast和itheima視頻庫。如果您有公開的資源,可以分享給我的話,用您的資源學習也可以。
博文是觀看視頻後,融入思考寫成的。博文好,是老師講得好。博文壞,是 給最苦 沒認真。
JavaWeb基礎 session isNew 判斷session是新生成的 還是舊有的