Java_Seesion_登录案例

12 篇文章 0 订阅
9 篇文章 0 订阅

1.IndexServlet

package gz.itcast;

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

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
 * 用户主页的逻辑
 * @author APPle
 *
 */
public class IndexServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		PrintWriter writer = response.getWriter();
		
		
		String html = "";
		
		/**
		 * 接收request域对象的数据
		 */
		/*
		String loginName = (String)request.getAttribute("loginName");
		*/
		
		/**
		 * 二、在用户主页,判断session不为空且存在指定的属性才视为登录成功!才能访问资源。
		 * 从session域中获取会话数据
		 */
		//1.得到session对象
		HttpSession session = request.getSession(false);
		if(session==null){
			//没有登录成功,跳转到登录页面
			response.sendRedirect(request.getContextPath()+"/login.html");
			return;
		}
		//2.取出会话数据
		String loginName = (String)session.getAttribute("loginName");
		if(loginName==null){
			//没有登录成功,跳转到登录页面
			response.sendRedirect(request.getContextPath()+"/login.html");
			return;
		}
		
		html = "<html><body>欢迎回来,"+loginName+",<a href='"+request.getContextPath()+"/LogoutServlet'>安全退出</a></body></html>";
		
		
		writer.write(html);
	}

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

}

2.LoginServlet

package gz.itcast;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
 * 处理登录的逻辑
 * @author APPle
 *
 */
public class LoginServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		//1.接收参数
		String userName = request.getParameter("userName");
		String userPwd = request.getParameter("userPwd");
		
		//2.判断逻辑
		if("eric".equals(userName)
				 && "123456".equals(userPwd)){
			//登录成功
			/**
			 * 分析:
			 * 	  context域对象:不合适,可能会覆盖数据。
			 *    request域对象: 不合适,整个网站必须得使用转发技术来跳转页面
			 *    session域对象:合适。
			 */
			/*
			request.setAttribute("loginName", userName);
			//request.getRequestDispatcher("/IndexServlet").forward(request, response);
			response.sendRedirect(request.getContextPath()+"/IndexServlet");
			*/
			
			/**
			 * 一、登录成功后,把用户数据保存session对象中
			 */
			//1.创建session对象
			HttpSession session = request.getSession();
			//2.把数据保存到session域中
			session.setAttribute("loginName", userName);
			//3.跳转到用户主页
			response.sendRedirect(request.getContextPath()+"/IndexServlet");
			
		}else{
			//登录失败
			//请求重定向
			response.sendRedirect(request.getContextPath()+"/fail.html");
		}
	}

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

}

3.退出逻辑

package gz.itcast;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
 * 退出逻辑
 * @author APPle
 *
 */
public class LogoutServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		/**
		 * 三、安全退出:
		 * 		删除掉session对象中指定的loginName属性即可!  
		 */
		//1.得到session对象
		HttpSession session = request.getSession(false);
		if(session!=null){
			//2.删除属性
			session.removeAttribute("loginName");
		}
		
		//2.回来登录页面
		response.sendRedirect(request.getContextPath()+"/login.html");
		
	}

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

}

4.登录页面

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>登录页面</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>
    <form action="/day12/LoginServlet" method="post">
    	用户名:<input type="text" name="userName"/>
    	<br/>
    	密码:<input type="text" name="userPwd"/>
    	<br/>
    	<input type="submit" value="登录"/>
    </form>
  </body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值