登录页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="java.net.URLDecoder" %>
<%
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 'index.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>
<%
Cookie[] cookies = request.getCookies(); //从request中获得Cookie对象的集合
String user = ""; //登录用户
String pwd = ""; //登录密码
String date = ""; //注册的时间
if (cookies != null) {
for (int i = 0; i < cookies.length; i++) { //遍历cookie对象的集合
if (cookies[i].getName().equals("mrCookie")) { //如果cookie对象的名称为mrCookie
user = URLDecoder.decode(cookies[i].getValue().split("#")[0]); //获取用户名
pwd = URLDecoder.decode(cookies[i].getValue().split("#")[1]); //获取用户名
date = cookies[i].getValue().split("#")[2]; //获取注册时间
}
}
}
if ("".equals(user) && "".equals(date)&&"".equals(pwd)) { //如果没有注册
%>
游客您好,欢迎您初次光临!
<form action="deal.jsp" method="post">
请输入姓名:<input name="user" type="text" value=""><br/>
请输入密码:<input name="pwd" type="text" value="">
<input type="submit" value="确定">
</form>
<%
} else { //已经注册
%>
欢迎[<b><%=user %></b>]再次光临<br>
密码是:<%=pwd %>
您注册的时间是:<%=date %>
<%
}
%>
</body>
</html>
处理页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="java.net.URLEncoder" %>
<%
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 'deal.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>
<%
request.setCharacterEncoding("utf-8"); //设置请求的编译为GB18030
String user=URLEncoder.encode(request.getParameter("user")); //获取用户名
String pwd=URLEncoder.encode(request.getParameter("pwd"),"utf-8"); //获取用户名
Cookie cookie = new Cookie("mrCookie", user+"#"+pwd+"#"+new java.util.Date().toLocaleString());//创建并实例化cookie对象
cookie.setMaxAge(60*60*24*30); //设置cookie有效期30天
response.addCookie(cookie); //保存cookie
%>
</body>
</html>