JavaWeb Servlet 登录功能 注销功能


有三个逻辑:
处理登陆的逻辑,用户主页的逻辑,退出登录逻辑

登陆的逻辑LoginServlet.java

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 处理登陆的逻辑
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		// 1.接收参数
		String userName = request.getParameter("userName");
		String userPwd = request.getParameter("userPwd");
		String yanzheng = request.getParameter("yanzheng");
		// 2.判断逻辑
		if ("admin".equals(userName) && "123456".equals(userPwd)) {
			// 登陆成功

			/*
			 * 分析: context域对象:不合适,可能会覆盖数据。 request域对象: 不合适,整个网站必须得使用转发技术来跳转页面
			 * session域对象:合适。
			 */
//			 request.setAttribute("loginName", userName);
//			 //request.getRequestDispatcher("/IndexServlet").forward(request,
//			 response);
//			 response.sendRedirect(request.getContextPath()+"/IndexServlet");
//			

			// 把用户保存在session对象中
			// 1.创建session对象
			HttpSession session = request.getSession();
			// 2.把数据保存到session域中
			session.setAttribute("loginName", userName);
			// 3.跳转到用户主页(可以使用重定向)
			response.sendRedirect(request.getContextPath() + "/IndexServlet");
		} else {
			// 登录失败
			// 请求重定向
			response.sendRedirect(request.getContextPath() + "/error.html");
		}

	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}

用户主页的逻辑IndexServlet.java

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 用户主页的逻辑
 */
@WebServlet("/IndexServlet")
public class IndexServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");// 设置返回的数据类型
		PrintWriter writer = response.getWriter();

		String html = "";// 创建一个html字符串

		// // 接收request域对象的数据
		// String loginName = (String)request.getAttribute("loginName");

		// 从session域中获取会话数据
		// 1.得到session对象
		HttpSession session = request.getSession(false);
		if (session == null) {
			// 没有登录成功,跳转到登录页面
			response.sendRedirect(request.getContextPath() + "/login.html");
			return;
		}
		// 2.取出会话数据
		String loginName = (String) session.getAttribute("loginName");
		if (loginName == null) {
			// 没有登录成功,跳转到登录页面(涉及到LogoutServlet)
			response.sendRedirect(request.getContextPath() + "/login.html");
			return;
		}

		html = "<html><body>欢迎回来," + loginName + "<br><br><a href='"
				+ request.getContextPath()
				+ "/LogoutServlet'><button>安全退出</button></a></body></html>";

		writer.write(html);// 用write()写出一个html

	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}

退出登录逻辑Logout.java

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 退出逻辑
 */
@WebServlet("/LogoutServlet")
public class LogoutServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		// 安全退出: 删除掉session对象中指定的loginName属性即可
		// 1.得到session对象
		HttpSession session = request.getSession(false);
		if (session != null) {
			// 2.删除属性
			session.removeAttribute("loginName");
		}
		// 2.返回登录页面
		response.sendRedirect(request.getContextPath() + "/login.html");
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}

login.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录界面</title>
</head>
<body>
	<form action="/day0309Login/LoginServlet" method="post">
		用户名:<input type="text" name="userName" placeholder="英文字母或数字, 长度3-15"><br>
		密码:<input type="password" name="userPwd" placeholder="必填且长度6-15"><br>
		<button>
		<input type="submit" value="登录">
		</button>
	</form>
</body>
</html>

login2.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录界面</title>
<style>
div {
	width: 50px;
	height: 36px;
	background-color: #f40;
	line-height: 36px;
	text-align: center;
	color: #fff;
	font-size: 16px;
	margin: 30px left; /* auto */
	cursor: pointer;
	-webkit-user-select: none; /*禁止用户选中文字*/
}
</style>
</head>
<body>
	<form action="/day0309Login/LoginServlet" method="post">
		用户名:<input type="text" name="userName" placeholder="英文字母或数字, 长度3-15"><br>
		密码:<input type="password" name="userPwd" placeholder="必填且长度6-15"><br>
		验证码:<input type="text"  placeholder="请输入验证码">
		<input type="submit" value="登录">
	</form>
	<div id="code_box">Af3D</div>
	<script>
    var code_box = document.getElementById("code_box");
 
    function refreshCode() {
    	
    	//62个字符 随机选择4位
        var code = '0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM',
            char = '',
            result = '';
 
        for (var i = 0; i < 4; i++) {
 
            //随机选择一位  (0,61) 写出0到61的随机的索引数字
            var code_index = Math.round(Math.random()*61);
 
            //得到随机的索引  取出随机地字符
            var char = code[code_index];
 
            //随机取出的字符 存在几个相同重复的问题 ,而且对于字母,不能区分大小写。
            // 避免重复的思路是:取出字符之后,和最后的result对比一下,看看里边是不是已经存在了,如果存在本次循环就终止,进行下一次
 
            if (result.toUpperCase().indexOf(char.toUpperCase()) > -1)
 
            //indexOf() == -1 说明结果里边没有要找的字符 那么 > -1 就是 里边有重复的字符
            {
                i --;
                //为什么会 --? 因为如果条件成立,那么本轮循环就结束进行下一轮循环(自然i就加1了),那么本轮本应该取出的字符就没有了
                //到最后会少一个字符 缺席
                continue;//终止本轮循环 进行下一轮
            }
 
            result += char;
        }
 
        code_box.innerHTML = result;
    }
 
    //点击事件
    code_box.onclick = refreshCode;
 
</script>

</body>
</html>

error.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>error</title>
</head>
<body>
	<font color="red" size='5'>用户名或密码输入有误,请重新输入</font>
	<br>
	
	<a href="/day0309Login/login.html"><button>重新登陆</button></a>

</body>
</html>

Java Timer注销 java用户登录注册注销_session


Java Timer注销 java用户登录注册注销_登录功能_02


Java Timer注销 java用户登录注册注销_session_03