带有Cookie的登录(无数据库)*支持中文用户名

带有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);
		}
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王子健121

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值