个人登录与注销

myeclipse或eclipse运行web项目!

login.jsp

<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"
	isErrorPage="false"
	import="com.hh.j2ee.login.Person,java.text.SimpleDateFormat,java.util.Date,org.apache.tomcat.util.descriptor.web.MessageDestination,java.security.MessageDigest"
	trimDirectiveWhitespaces="true"%>
//trimDirectiveWhitespaces去除网页头空白的
<%!private static final String KEY = ":cookiekshwmh.com";
//加密算法(用于cookie的保护)  因篇幅不够,不介绍加密算法,自己去百度 
	public final static String calcMD5(String ss) {
		String s = ss == null ? "" : ss;
		char[] hexDigihts = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };              
		try {
			byte[] strTemp = s.getBytes();
			MessageDigest mdTemp = MessageDigest.getInstance("MD5");
			mdTemp.update(strTemp);
			byte[] md = mdTemp.digest();
			int j = md.length;
			char[] str = new char[j * 2];
			int k = 0;
			for (int i = 0; i < j; i++) {
				byte byte0 = md[i];
				str[k++] = hexDigihts[byte0 >>> 4 & 0xf];
				str[k++] = hexDigihts[byte0 & 0xf];
			}
			return new String(str);
		} catch (Exception e) {
			return null;
		}
	}%>                 //md5加密算法
<%!SimpleDateFormat birthdaytime = new SimpleDateFormat("yyyy-MM-dd");%>  //格式日期 
<%
	Person[] persons = { new Person("小海", "xiaohai520xm", 100, birthdaytime.parse("1918-01-01")),
	new Person("小美", "xiaomei520xh", 100, birthdaytime.parse("1918-01-01")) };//
parse方法将String类型装换为Date的对象
	request.setCharacterEncoding("UTF-8");  //设置编码  一般采用UTF-8
	response.setCharacterEncoding("UTF-8");//设置编码
	if (request.getMethod().equals("POST")) {         //判断提交的方式
		String username = request.getParameter("username");   //用户名
		String password = request.getParameter("password");   //密码
		String action = request.getParameter("action");       //参数
		String ssid = calcMD5(KEY + username);               //加密
		if (action.equals("login")) {                 //判断是注销还是登陆
	int timeout = Integer.parseInt(request.getParameter("timeout"));    //cookie的有效期
	for (Person p : persons) {
		Person person = p;
		if (username.equals(person.getUsername()) && password.equals(person.getPassword())) {
			HttpSession sessions = request.getSession();  //获取cookie的对象
			session.setAttribute("person", person);    //通过session带person的对象
			session.setAttribute("logintime", new Date());  //同理    
			Cookie usernamecookie = new Cookie("usernamecookie", ssid);  创建cookie
			usernamecookie.setMaxAge(timeout);    //设置有效期
			response.addCookie(usernamecookie);   //添加cookie
			response.sendRedirect(response.encodeRedirectURL(
					request.getContextPath() + "/loginout.jsp" + "?" + System.currentTimeMillis()));     //重写url   以免用户无法通过cookie访问网页
			return;
		}
	}
		}

     //同理
       else if (action.equals("loginout")) {   
	Cookie usernamecookie = new Cookie("usernamecookie", "");
	usernamecookie.setMaxAge(0);
	response.addCookie(usernamecookie);
	response.sendRedirect(response.encodeRedirectURL(
					request.getRequestURI() + "?" + System.currentTimeMillis()));
			return;
		}
	}
%>
<!DOCTYPE html>
<html>
<head>
<title>请先登录</title>
<link rel='stylesheet' type='text/css' href='css/style.css'>
</head>
<body>
	<div align="center" style="margin:10px;">
		<fieldset>
			<legend align="center">请先登录</legend>
			<form action="<%=request.getRequestURI()%>?action=login"
				method="post">
				<table>
					<tr>
						<td>账号:</td>
						<td><input type="text" name="username" maxlength="16"
							placeholder="用户名" required="required"
							style="width:200px;height: 25px;font-size:14px; padding:0px 0px 0px 10px; margin: 0px;" /></td>
					</tr>

					<tr>
						<td>密码:</td>
						<td><input type="password" name="password" maxlength="16"
							placeholder="密码" required="required"
							style="width:200px;height: 25px;font-size:14px;  padding:0px 0px 0px 10px; margin: 0px;" /></td>
					</tr>

					<tr>
						<td>有效期:</td>
						<td><input type="radio" value="-1" name="timeout"
							checked="checked">关闭浏览器即失效<br /> <input type="radio"
							value="${24*60*60*30}" name="timeout">30天内有效<br /> <input
							type="radio" value="<%=Integer.MAX_VALUE%>" name="timeout">永久有效</td>
					</tr>

					<tr>
						<td></td>
						<td><input type="submit" value="登  录" class="button"
							style="width:60px;"></td>
					</tr>
				</table>
			</form>
		</fieldset>
	</div>
</body>
</html>

loginout.jsp

<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"
	import="com.hh.j2ee.login.Person,java.text.SimpleDateFormat,java.util.Date"
	trimDirectiveWhitespaces="true"%>
<%!SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd");%>  //同理
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<%
	request.setCharacterEncoding("UTF-8");  //设置编码
	response.setCharacterEncoding("UTF-8");//设置编码
%>
<%
	HttpSession sessions = request.getSession();        
	Person person = (Person) sessions.getAttribute("person");   //获取person对象
	Date logintime = (Date) sessions.getAttribute("logintime");  //获取Date对象
%>
<!DOCTYPE html>
<html>
<head>
<title>你好,<%=person.getUsername()%>!欢迎您回来
</title>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
	<div align="center" style="margin:10px;">
		<fieldset>
			<legend align="center">欢迎您回来</legend>
			<form action='<%=request.getContextPath() %>/login.jsp?action=loginout' method="post">
				<table>
					<tr>
						<td>您的姓名:</td>
						<td><%=person.getUsername()%></td>
					</tr>

					<tr>
						<td>登录时间:</td>
						<td><%=logintime%></td>
					</tr>

					<tr>
						<td>您的年龄:</td>
						<td><%=person.getAge()%></td>
					</tr>

					<tr>
						<td>您的生日:</td>
						<td><%=date.format(person.getBirthday())%></td>
					</tr>

					<tr>
						<td></td>
						<td><input type="submit" value="注销"
							class="button" style="width:60px;" ></td>
					</tr>

				</table>
			</form>
		</fieldset>
	</div>
</body>
</html>

Person.java

package com.hh.j2ee.login;

/*
 *构造JavaBean
 */
import java.util.Date;

public class Person {
	private String username; // 用户名
	private String password; // 密码
	private int age;         // 年龄
	private Date birthday;   // 出生日期

	public Person() {}

	public Person(String username, String password, int age, Date birthday) {
		this.username = username;
		this.password = password;
		this.age = age;
		this.birthday = birthday;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

}

style.css     》》放在css文件夹下

body, div, td, input {font-size:12px; margin:0px; }
select {height:20px; width:300px; }
.title {font-size: 16px; padding: 10px; margin:10px; width:80%; }
.text {height:20px; width:300px; border:1px solid #AAAAAA; }
.line {margin:2px; }
.leftDiv {width:110px; float:left; height:22px; line-height:22px; font-weight:bold; }
.rightDiv {height:22px; line-height:22px; }
.button {
	color:#fff;
	font-weight:bold;
	font-size: 11px; 
	text-align:center;
	padding:.17em 0 .2em .17em;
	border-style:solid;
	border-width:1px;
	border-color:#9cf #159 #159 #9cf;
	background:#69c url(../images/bg-btn-blue.gif) repeat-x;
}

图片:加我qq:2413176044    !   统一放在image下 

效果图:

点击       注销后如图一

必须填写账号和密码

猜你喜欢

转载自blog.csdn.net/qq_39893313/article/details/82110125