带有Cookie的登录(无数据库)*支持中文用户名:
这个是演示简单的带有Cookie的登录,用户名和密码是写死在final中的
需求:(1)进入网页输入账号密码。
(2)登录后保存cookie
(3)下次登录后无和需输入账号密码。
(4)清除Cookie后还需要输入账号和密码,
(5)用户名支持中文字符
过程:进入index.jsp中先进行Cookie的检测,如果发现对应的用户名和密码,
则登录成功,如果Cookie中没有对应的则需要输入用户名密码进行匹配.
index.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 '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">
</head>
<body>
<form action="LoginServlet" method="post">
<input type="text" name="username"> <br> <input
type="text" name="password"> <br> <input type="submit"
value="提交">
</form>
</body>
<%
//判断Cookie中是否有用户名。
int index = 0;//代表 浏览器的Cookie中是否含有 账号和密码。0代表无,2代表有2个匹配
Cookie[] cookies = request.getCookies();
if (cookies != null) {
for (Cookie c : cookies) {
if (c.getValue().equals("wang") || c.getValue().equals("121")) {
index++;//如果有的话index加1。
}
}
}
if (index == 2) {
//如果寻找到2个匹配的用户名和密码。
//并重定向到登录成功的页面。
request.getRequestDispatcher("login.jsp").forward(request, response);
}
%>
</html>
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">
</head>
<body>
登陆成功!
</body>
</html>
LoginServlet
package com.wang;
import java.io.IOException;
import java.io.PrintWriter;
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 void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//写死的用户名和密码。
final String Myname="wang";
final String Mypassword="121";
//设置字符编码
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
String name=request.getParameter("username");
String password=request.getParameter("password");
System.out.println(name+"----"+password);
if(name.equals(Myname)&&password.equals(Mypassword)){
//保存Cookie
Cookie cookie_username=new Cookie("username", name);
Cookie cookie_password=new Cookie("password", password);
//设置cookie的生命周期,一个小时
cookie_username.setMaxAge(60*60);
cookie_password.setMaxAge(60*60);
response.addCookie(cookie_username);
response.addCookie(cookie_password);
//登陆成功后进行重定向
request.getRequestDispatcher("/demo/login.jsp").forward(request, response);
}
}
}