【servlet】登录验证

Check.java写业务逻辑,login.jsp写登录界面,welcome.jsp写欢迎界面


Check.java

package com.yan.servlet;

import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/check")

public class Check extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public Check() {
        super();
    }

    public void process(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	String u = request.getParameter("username");
		String p = request.getParameter("passwd");
		
		request.setAttribute("username", u);
		request.setAttribute("passwd", p);
		
		if (u.equals("1") && p.equals("1")) {
			request.getRequestDispatcher("/jsp/welcome.jsp").forward(request, response);
		} else {
			request.getRequestDispatcher("/jsp/login.jsp").forward(request, response);
		}
    }
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		process(request, response);
	}

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

}


login.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>Please input below</h1>
<form action="/TestPro/check" method="post">
username: <input type="text" name="username"><br/><br/>
password: <input type="password" name="passwd"><br/><br/>
<input type="submit" value="submit" >
<div>
<h4>刚刚输入的用户名是:</h4>${username}
<h4>刚刚输入的密码是:</h4>${passwd}
</div>
</form>
</body>
</html>


welcome.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>hello</h1>
<div>
<h1>你的用户名是${username },密码是${passwd }</h1>
</div>
</body>
</html>


web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>TestPro</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>


你可能感兴趣的:(【servlet】登录验证)