欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

会话跟踪机制 Session

程序员文章站 2022-03-15 11:04:11
...
package com.oracle.servlet;

import java.io.IOException;
import java.io.PrintWriter;

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;

public class LoginServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
//获得用户名和密码
	String userName=request.getParameter("userName");
	String password=request.getParameter("password");
	
	if(userName.equals(password)){
		//将用户名和密码保存在session中
		HttpSession session=request.getSession();
		session.setAttribute("userName", userName);
		session.setAttribute("password", password);
		//超时时间为30秒
		//session.setMaxInactiveInterval(30);
		
		Cookie c=new Cookie("username",userName);
		//写在文件夹下
		c.setMaxAge(90*24*60*60);
		
		response.addCookie(c);
		
		response.sendRedirect("listmail.html");
	}else{
		response.sendRedirect("first.html");
	}
		
	}

}

package com.oracle.servlet;

import java.io.IOException;
import java.io.PrintWriter;

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;

public class MailServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
this.doPost(request, response);
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
//判断用户是否登录,如没登陆,重定向到登陆页
		HttpSession session=request.getSession();
		
		Cookie[] cc=request.getCookies();
		for(Cookie c:cc){
			System.out.println(c.getName()+":"+c.getValue());
			if(c.getName().equals("userName")){
			System.out.println("用户名是:"+c.getName());	
			}
			
		}
		
		System.out.println(session.getId());
		String name=(String)session.getAttribute("userName");
		if(name!=null){
					response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		
		
		
		
		
	String flag=request.getParameter("flag");
	if(flag.equals("1")){
		out.println(name+"发送邮件成功");
	}else if(flag.equals("2")){
	out.println(name+"删除了邮件");
	}else if(flag.equals("3")){
	out.println(name+"有n封未读邮件");
	}else{
		session.invalidate();
		out.println("用户已注销");
	}
		out.println("</BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
		}else{
			response.sendRedirect("first.html");
		}

	}

}

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>MyHtml.html</title>
	
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

  </head>
  
  <body>
    This is my HTML page. <br>
<form method="post" action="login">
userName:<input type="text" name="userName"/><br/>
password:<input type="text" name="password"/><br/>
<input type="submit" value="登陆"/>
</form>
  </body>
</html>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>listmail.html</title>
	
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

  </head>
  
  <body>
    This is my beatiful page. <br>
    <ul>
    <li><a href="mail?flag=1">发送邮件</a></li>
    <li><a href="mail?flag=2">删除邮件</a></li>
    <li><a href="mail?flag=3">查找邮件</a></li>
       <li><a href="mail?flag=4">注销用户</a></li>
    </ul>
  </body>
</html>

 <servlet-mapping>
    <servlet-name>LoginServlet</servlet-name>
    <url-pattern>/login</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>MailServlet</servlet-name>
    <url-pattern>/mail</url-pattern>
  </servlet-mapping>