会话跟踪机制 Session

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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值