JavaWeb Servlet 登录功能 注销功能

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 = "欢迎回来," + loginName + "

"; 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





登录界面


	
用户名:
密码:

login2.html





登录界面



	
用户名:
密码:
验证码:
Af3D

error.html





error


	用户名或密码输入有误,请重新输入
	

JavaWeb Servlet 登录功能 注销功能_第1张图片


JavaWeb Servlet 登录功能 注销功能_第2张图片


JavaWeb Servlet 登录功能 注销功能_第3张图片


你可能感兴趣的:(JavaWeb)