1.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>
<%
//拿到客户端的cookies文件
Cookie[] cookies=request.getCookies();
//若cookies不为空,,循环去数据
if(cookies != null)
{
for(Cookie c: cookies)
{ //若有name为"autoLogin"的cookies,,直接跳转到主页面
if(c.getName().equals("autoLogin"))
{
request.getRequestDispatcher("index.jsp").forward(request,response);
}
}
}
%><!-- 否则显示登录页面 -->
<form method="post" action="servlet/LoginServlet">
<table align="center">
<tr><td>账户:</td><td><input type="text" name="user"></td></tr>
<tr><td>密码:</td><td><input type="password" name="pwd"></td></tr>
<tr ><td><input type="checkbox" name="autologin">自动登录</td></tr>
<tr><td><input type="submit" value="提交"></td><td><input type="reset" value="取消"></td></tr>
<tr><td></td></tr>
</table>
</form>
</body>
</html>
2,LoginServlet
package com.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;
import java.util.Iterator;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginServlet extends HttpServlet {
public LoginServlet() {
super();
}
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
}
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 user = request.getParameter("user");
System.out.println(user);
//设置user的cookie,完整页面间共享,替代session
//中文保存数据 encode保存进去 decode出来
user = URLEncoder.encode(user,"utf-8");
Cookie cookieUser = new Cookie("user", user);
cookieUser.setMaxAge(20*60000);
cookieUser.setPath("/");
//将cookie添加到http首部里去
response.addCookie(cookieUser);
//判断是否自动登录,
String autoLogin = request.getParameter("autologin");
if(autoLogin != null)
{
Cookie autoLoginCookie=new Cookie("autoLogin","auto");
autoLoginCookie.setMaxAge(20*60000);
autoLoginCookie.setPath("/");
response.addCookie(autoLoginCookie);
}
response.sendRedirect(request.getContextPath()+"/index.jsp");
}
public void init() throws ServletException {
}
}
3,index.jsp
<%@ page language="java" import="java.util.*,java.net.URLDecoder,com.servlet.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<<style>
.d1{font-size:xx-large;font-weight: 300;font-style:italic;color: red; }
</style>
<!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用户信息
String user=" ";
Cookie[] cookies = request.getCookies();
for (int i = 0; i < cookies.length; i++)
{
if(cookies[i].getName().equals("user"))
{
user=cookies[i].getValue();
user=URLDecoder.decode(user, "utf-8");
%>
<div align="center" class="d1">
欢迎<%=user%>登录!!
</div>
<%
}
}
%>
<div align="center">
</div>
</body>
</html>