javaee session的销毁和过期时间

javaee session的销毁和过期时间_第1张图片

login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title heretitle>
head>
<body>

<form action="LoginServlet" method="post">
<pre>
  用户名:<input type='text' name='uname' />
  密码:<input type='password' name='pwd' />
 <input type='submit' name='sub' value='登录' />
pre>
form>

body>
html>

guanwang.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title heretitle>
head>
<body>

<%
   if(session.getAttribute("uname")!=null)
   {
%>
<%=session.getAttribute("uname") %> 欢迎您,<a href="ExitServlet">退出a>
<% 	   
   }	  
   else
   {
%>
            亲,请<a href="login.jsp">登录a>
<%
   }
%>


body>
html>

LoginServlet

package com.yyy.servlet;

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

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
				//response.getWriter().append("Served at: ").append(request.getContextPath());
			
				//1.获得用户信息
				String uname=request.getParameter("uname");
				String pwd=request.getParameter("pwd");
				//2.判断是否登录成功
				if(uname.equals("zhangsan") && pwd.equals("123"))
				{
				     //3.如果登录成功 则将用户名存入session 
					 HttpSession session=request.getSession();
					 
					 session.setAttribute("uname", uname);
					 
					 //session.setMaxInactiveInterval(60);//session的有效期 默认是30分钟 秒为单位
					 
					 response.sendRedirect("guanwang.jsp");
				}
				else
					response.getWriter().println("用户名或密码出错");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

ExitServlet

package com.yyy.servlet;

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

/**
 * Servlet implementation class ExitServlet
 */
@WebServlet("/ExitServlet")
public class ExitServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ExitServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
				//response.getWriter().append("Served at: ").append(request.getContextPath());
			
				//退出登录  销毁session
				HttpSession session=request.getSession();
				
				//session.removeAttribute("uname"); //移除session中的某个属性
				
				session.invalidate(); //销毁当前session
				
				response.sendRedirect("guanwang.jsp");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

设置session过期时间有两种方式,一种是
session.setMaxInactiveInterval(60),单位是秒。
还有一种就是在web.xml进行配置,单位是分钟。


<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>web5display-name>
  <welcome-file-list>
    <welcome-file>index.htmlwelcome-file>
    <welcome-file>index.htmwelcome-file>
    <welcome-file>index.jspwelcome-file>
    <welcome-file>default.htmlwelcome-file>
    <welcome-file>default.htmwelcome-file>
    <welcome-file>default.jspwelcome-file>
  welcome-file-list>
  <session-config>
       
       <session-timeout>1session-timeout>
   session-config>
web-app>

你可能感兴趣的:(Mac开发,java-ee,servlet,java)