礼悟:
好好学习合思考,尊师重道存感恩。叶见寻根三返一,江河湖海同一体。
虚怀若谷良心主,愿行无悔给最苦。读书锻炼强身心,诚劝且行且珍惜。
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视频库。如果您有公开的资源,可以分享给我的话,用您的资源学习也可以。
博文是观看视频后,融入思考写成的。博文好,是老师讲得好。博文坏,是 给最苦 没认真。
时间: 2024-11-29 07:09:28