Servlet登录验证
Servlet用户名及密码验证
实验结果
代码
FakeLogin.java
package com.fakelogin.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class FakeLogin extends HttpServlet { @Override public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String userName = request.getParameter("userid"); String userPwd = request.getParameter("passwd"); System.out.println("userName"+userName); System.out.println("userPwd"+userPwd); String info = ""; if(("tom".equals(userName))&&"123".equals(userPwd)) //if(userName == "tom" && userPwd == "123") { info = "用户"+userName+"的密码是"+userPwd; }else{ info = "用户名或密码不正确!"; } request.setAttribute("outputMessage", info); request.getRequestDispatcher("./"+"NewFile.jsp").forward(request,response); } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } }
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <welcome-file-list> <welcome-file> login.jsp </welcome-file> </welcome-file-list> <servlet> <servlet-name>FakeLogin</servlet-name> <servlet-class>com.fakelogin.servlet.FakeLogin</servlet-class> </servlet> <servlet-mapping> <servlet-name>FakeLogin</servlet-name> <url-pattern>/FakeLogin</url-pattern> </servlet-mapping> </web-app>
login.jsp
<%@ page language="java" contentType="text/html; charset=gb2312" %> <!DOCTYPE html> <html> <head> <link rel="stylesheet" href="css/style.css"> <title>FAKE NEWS</title> </head> <body> <div class="logform"> <label class="title"for="AAAA">帐号登录</label> <form class="loginform" id="aaa" action="FakeLogin" method="post"> <input class="inputbox" type="text" id="userid" name="userid" placeholder="用户名"> <input class="inputbox" type="password" id="passwd" name="passwd" placeholder="密码"> <input class="submit" type="submit" name="submit" value="登陆"> </form> </div> </body> </html>
NewFile.jsp
<%@ page language="java" contentType="text/html; charset=gb2312" %> <!DOCTYPE html> <html> <head> </head> <body> <%=request.getAttribute("outputMessage") %> <p> end </p> </body> </html>
百度云
链接: https://pan.baidu.com/s/1Ruxdii5kzZrb4BMRS1G0PQ 提取码: mg6i 复制这段内容后打开百度网盘手机App,操作更方便哦