其他分享
首页 > 其他分享> > Session(重点)

Session(重点)

作者:互联网

什么是Session

Session和Cookie的区别

 

使用场景

使用Session

SessionDemo01.java

package com.godairo.servlet;

import pojo.Person;

import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.io.IOException;

public class SessionDemo01 extends HttpServlet {

		@Override
		protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
				//解决乱码
				request.setCharacterEncoding("UTF-8");
				request.setCharacterEncoding("UTF-8");
				response.setContentType("text/html;charset=utf-8");
				//得到Session
				HttpSession session = request.getSession();
				
				//给Session中存东西
				session.setAttribute("name",new Person("小可爱",23));
				
				//获取Session的ID
				String sessionid = session.getId();
				
				//判断Session是不是新创建的
				if (session.isNew()){
						response.getWriter().write("session创建成功,ID为:"+sessionid);
				}else {
						response.getWriter().write("session已经在服务器中存在,ID为:"+sessionid);
				}
				
				//Session创建的时候做了什么事情?
//				Cookie cookie = new Cookie("JSESSIONID",sessionid);
//				response.addCookie(cookie);

		}

		@Override
		protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
				super.doGet(req, resp);
		}
}

SessionDemo02.java

package com.godairo.servlet;

import pojo.Person;

import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.io.IOException;

public class SessionDemo02 extends HttpServlet {

		@Override
		protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
				//解决乱码
				request.setCharacterEncoding("UTF-8");
				request.setCharacterEncoding("UTF-8");
				response.setContentType("text/html;charset=utf-8");
				//得到Session
				HttpSession session = request.getSession();

				Person person = (Person) session.getAttribute("name");

				System.out.println(person.toString());
				response.getWriter().write(person.toString());

		}

		@Override
		protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
				super.doGet(req, resp);
		}
}

SessionDemo03.java

package com.godairo.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class SessionDemo03 extends HttpServlet {

		@Override
		protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
				HttpSession session = request.getSession();
				session.removeAttribute("name");
				//手动注销Session
				session.invalidate();
				
		}

		@Override
		protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
				doGet(req, resp);
		}
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0"
         metadata-complete="true">
        
        <servlet>
                <servlet-name>CookieDemo01</servlet-name>
                <servlet-class>com.godairo.servlet.CookieDemo01</servlet-class>
        </servlet>
        
        <servlet-mapping>
                <servlet-name>CookieDemo01</servlet-name>
                <url-pattern>/c1</url-pattern>
        </servlet-mapping>


        <servlet>
                <servlet-name>CookieDemo02</servlet-name>
                <servlet-class>com.godairo.servlet.CookieDemo02</servlet-class>
        </servlet>

        <servlet-mapping>
                <servlet-name>CookieDemo02</servlet-name>
                <url-pattern>/c2</url-pattern>
        </servlet-mapping>

        <servlet>
                <servlet-name>CookieDemo03</servlet-name>
                <servlet-class>com.godairo.servlet.CookieDemo03</servlet-class>
        </servlet>

        <servlet-mapping>
                <servlet-name>CookieDemo03</servlet-name>
                <url-pattern>/c3</url-pattern>
        </servlet-mapping>

        <servlet>
                <servlet-name>SessionDemo01</servlet-name>
                <servlet-class>com.godairo.servlet.SessionDemo01</servlet-class>
        </servlet>

        <servlet-mapping>
                <servlet-name>SessionDemo01</servlet-name>
                <url-pattern>/s1</url-pattern>
        </servlet-mapping>

        <servlet>
        <servlet-name>SessionDemo02</servlet-name>
        <servlet-class>com.godairo.servlet.SessionDemo02</servlet-class>
</servlet>

        <servlet-mapping>
                <servlet-name>SessionDemo02</servlet-name>
                <url-pattern>/s2</url-pattern>
        </servlet-mapping>

        <servlet>
                <servlet-name>SessionDemo03</servlet-name>
                <servlet-class>com.godairo.servlet.SessionDemo03</servlet-class>
        </servlet>

        <servlet-mapping>
                <servlet-name>SessionDemo03</servlet-name>
                <url-pattern>/s3</url-pattern>
        </servlet-mapping>
        
        <!--设置Session默认的失效时间-->
        <session-config>
                <!--十五分钟后Session自动失效,以分钟为单位-->
                <session-timeout>1</session-timeout>
        </session-config>
</web-app>

 会话自动过期:web.xml配置

<!--设置Session默认的失效时间-->
<session-config>
        <!--十五分钟后Session自动失效,以分钟为单位-->
        <session-timeout>1</session-timeout>
</session-config>

 

标签:ServletException,Session,重点,request,session,import,servlet
来源: https://blog.csdn.net/qq_44706176/article/details/122754038