用户登录案例

login.jsp中写提交表单
s1.jsp和s2.jsp为受限页面
AServlet处理数据
类User内存储了若干账号,密码

1.首先login.jsp提交登录数据到ASevlet中
2.Aservlet获取到用户名和密码,用request.getParameter()方法
3.然后把User类中用map存的用户名和密码拿出来和表单数据进行比较
4.比较
若用户名匹配到
-----比较密码
----------密码相同,把用户名密码保存到session域中,把用户名保存到cookie中,重定向到s1.jsp
----------密码不同,把密码错误信息储存到request域中,请求转发到login.jsp,
若用户名匹配不到
-----把用户名不存在错误信息储存到request域中,请求转发到login.jsp,

5.login.jsp页面获取cookie显示到页面中
6.s1.jsp和s2.jsp获取session域中的username,若为空,把请登录错误信息储存到request域中,请求转发到login.jsp,

User类

package cn.hpu.user;

import java.util.HashMap;
import java.util.Map;

public class User {
	
	public static Map<String,String> findUser(){
		Map<String,String> map = new HashMap<String, String>();
		map.put("1234567", "1234567");
		map.put("2345678", "2345678");
		map.put("3456789", "3456789");
		return map;
	}
	
}

login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'login.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
  <%
   String user = "";
  	Cookie[] c = request.getCookies();
  	for(Cookie c1:c){
  		if(c!=null&&c1.getName().equals("uname")){
  			user += c1.getValue();
  		}
  	}
   %>
  <% 
  	String s1 = "";
  	String s2 = (String)request.getAttribute("elem");
  	if(s2!=null){
  		s1+=s2;
  	}
  	
  %>
  	<font color="red"><b><%=s1%></b></font>
   	<form action="/day11_01/BServlet" method="post">
   		用户名:<input type="text" name="username" value="<%=user%>"/><br/>
   		密码:<input type="password" name="password"/><br/>
   			<input type="submit" value="提交">
   	</form>
  </body>
</html>

BServlet

package cn.hpu.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

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 org.hibernate.validator.constraints.Length;

import cn.hpu.user.User;

public class BServlet extends HttpServlet {

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//获取表单数据
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		
		/*
		 * 校验用户名密码
		 * 由于没有学数据库,我把用户名密码存到了map集合中
		 * 
		 * */
		//得到所有用户数据
		Map<String,String> map = User.findUser();
		int size = map.size();
		int k = 0;
		//遍历map集合中的用户名密码
		Set<Entry<String,String>> set = map.entrySet();
		for (Entry<String, String> en : set) {
			k++;
			System.out.println(en.getValue()+":"+username);
			if(en.getKey().equals(username)){//找到了用户名,匹配密码
				if(en.getValue().equals(password)){//密码匹配成功,重定向到s1.jsp
					//把用户名信息保存到Cookie中
					Cookie c1 = new Cookie("uname", username);
					c1.setMaxAge(60*60*24*30);
					response.addCookie(c1);
					//保存用户信息到 session域中
					request.getSession().setAttribute("username", username);
					request.getSession().setAttribute("password", password);
					//重定向
					response.sendRedirect("/day11_01/login/s1.jsp");
					break;
					
				}else{
					//错误信息请求转发到登录页面
					request.setAttribute("elem", "密码错误");
					request.getRequestDispatcher("/login/login.jsp").forward(request, response);
					System.out.println("密码错误!");
					break;
				}
			}else if(k==size){
				//错误信息请求转发到登录页面
				request.setAttribute("elem", "用户名不存在");
				request.getRequestDispatcher("/login/login.jsp").forward(request, response);
				System.out.println("用户名不存在!");
//				System.out.println(en.getKey());
//				System.out.println(username);
				break;
			}
		}	
	}

}

s1.jsp

<body>
  <%
	   	String username = (String)session.getAttribute("username");
	   	if(username==null){
	   		request.setAttribute("elem", "请登录!");
	   		request.getRequestDispatcher("/login/login.jsp").forward(request, response);
	   		return;
	   	} 
	   	
  %>
    <a>欢迎参观本页面1</a>
    
    
    <a href="/day11_01/login/s2.jsp">下一页</a>
    
  </body>

s2.jsp

 <body>
  <%
    	String username = (String)session.getAttribute("username");
    	if(username==null){
    		request.setAttribute("elem", "请登录!");
    		request.getRequestDispatcher("/login/login.jsp").forward(request, response);
    		return;
    	}
  %>
   
    <a>欢迎参观本页面2</a>
    
    
  </body>

你可能感兴趣的:(JavaWeb)