java Web(Jsp和Servlet登录校验)

1、Login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

  
    用户注册
  
  
  
"LoginServlet" method="post">

用户注册

username: type = "username" name = "username">
password: type = "password" name = "password">
repassword: type = "password" name = "repassword">
age:type = "Text" name = "age">
type = "submit" value = "提交">   type = "reset" value = "重置">

2、web.xml


<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>Logindisplay-name>
  <servlet>
    <servlet-name>LoginServletservlet-name>
    <servlet-class>com.servlet.LoginServletservlet-class>
  servlet>

  <servlet-mapping>
    <servlet-name>LoginServletservlet-name>
    <url-pattern>/LoginServleturl-pattern>
  servlet-mapping>

web-app>

3、LoginServlet.java

package com.servlet;

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

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginServlet extends HttpServlet {

    private static final String MESSAGE1 = "登录成功";
    private static final String MESSAGE2 = "密码错误";
    private static final String MESSAGE3 = "年龄错误";
    private static final String MESSAGE4 = "密码和年龄错误";

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String name = request.getParameter("username");
        String pass = request.getParameter("password");
        String repass = request.getParameter("repassword");
        String age = request.getParameter("age");
        //String 类型 转化为int 类型
        int a = Integer.parseInt(age);

        if (isPassword(pass, repass) && isAge(a)) {
            demo(request, response, MESSAGE1);
        } else if (!isPassword(pass, repass) && isAge(a)) {
            demo(request, response, MESSAGE2);
        } else if (isPassword(pass, repass) && !isAge(a)) {
            demo(request, response, MESSAGE3);
        } else if (!isPassword(pass, repass) && !isAge(a)) {
            demo(request, response, MESSAGE4);
        }

    }

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

    private void demo(HttpServletRequest request, HttpServletResponse response,
            String string) throws ServletException, IOException {

        response.setCharacterEncoding("UTF-8");
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.print("Page");
        out.print("" + string + "");
        out.flush();

    }

    //判断是否相等
    private boolean isPassword(String str1, String str2) {
        if (str1.equals(str2)) {
            return true;
        }
        return false;
    }

    //年龄是否符合
    private boolean isAge(int age) {
        if ((0 <= age && age >=18)) {
            return true;
        }
        return false;
    }

}

你可能感兴趣的:(java Web(Jsp和Servlet登录校验))