Servlet实现简单的用户登录



login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




登录页面


用户名:

密码:

  


loginServlet.java

package com.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;

/**
 * Servlet implementation class loginServlet
 */
@WebServlet("/loginServlet")
public class loginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public loginServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();//获取session
		Object name = session.getAttribute("name");//从session中获取用户名
		Object pwd = session.getAttribute("pwd");//从session获取用户密码
		System.out.println(name+":"+pwd); //测试一下,输出获取的用户名和密码
		session.invalidate(); //注销session
		response.sendRedirect("login.jsp"); //跳转到login.jsp页面
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// doGet(request, response);
		// 设置编码为utf-8
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");

		// 获取用户名和密码
		String name = request.getParameter("uname");
		String pwd = request.getParameter("pwd");

		// 校验用户名和密码是否正确
		if ("admin".equals(name) && "123".equals(pwd)) {// 验证成功
			HttpSession session = request.getSession();//获取session
			session.setAttribute("name", name);// 将用户名和密码保存在session中
			session.setAttribute("pwd", pwd);// 将用户名和密码保存在session中
			response.sendRedirect("success.jsp");// 跳转到success.jsp页面
		} else {// 校验不成功,则留在跳转到login.jsp页面
			response.sendRedirect("login.jsp");
		}
	}

}


success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




登录成功页面


登录成功!${name},${pwd}











你可能感兴趣的:(JavaSE)