用servlet进行用户名和密码校验
Login.html效果图
登陆成功图
LoginFail.html效果图
代码结构图
各项代码:
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="UTF-8"> 5 <title>登录界面</title> 6 <link rel="stylesheet" type="text/css" href="style.css" /> 7 </head> 8 <body> 9 <div class="box"> 10 请登录 11 <div class="box2"> 12 <form action="login" id="eee" method="post"> 13 <br><br> 14 账号:<input type="text" name="uname"> 15 <br><br><br> 16 密码: <input type="password" name="upassword"> 17 <br><br> 18 <button type="submit" id="btn">登录</button> 19 </form> 20 </div> 21 </div> 22 </body> 23 </html>
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="UTF-8"> 5 <title>登录失败</title> 6 </head> 7 <body style="text-align:center"> 8 <h1>登录失败:账号或密码错误</h1> 9 </body> 10 </html>
1 @CHARSET "UTF-8"; 2 3 .box{ 4 width:320px; 5 height:260px; 6 background-color:#66ccff; 7 text-align:center; 8 margin-left:35%; 9 margin-top:15%; 10 padding-top:10px; 11 } 12 .box2{ 13 width:300px; 14 height:200px; 15 text-align:center; 16 margin-top:10px; 17 margin-left:10px; 18 }
1 //重写父类的doPost方法,收到客户端的请求(post方式)时执行此方法 2 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 3 //设置请求和响应的字符集编码 4 response.setContentType("text/html; charset=UTF-8"); 5 request.setCharacterEncoding("UTF-8"); 6 PrintWriter out = response.getWriter(); //获得输出流 7 //从请求对象中获得指定参数的值 8 String strName = request.getParameter("uname"); 9 String strPassWord = request.getParameter("upassword"); 10 if("tom".equals(strName)&&"123".equals(strPassWord)) 11 { 12 out.println("你的账号是:"+strName+"<br>"); 13 out.println("你的密码是:"+strPassWord); 14 } 15 else{ 16 response.sendRedirect("LoginFail.html"); 17 } 18 }
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> 3 <display-name>ShowLoginInfo</display-name> 4 <welcome-file-list> 5 <welcome-file>index.html</welcome-file> 6 <welcome-file>index.htm</welcome-file> 7 <welcome-file>index.jsp</welcome-file> 8 <welcome-file>default.html</welcome-file> 9 <welcome-file>default.htm</welcome-file> 10 <welcome-file>default.jsp</welcome-file> 11 </welcome-file-list> 12 <servlet> 13 <!-- 声明Servlet对象 --> 14 <servlet-name>login</servlet-name> 15 <!-- 上面一句指定Servlet对象的名称 --> 16 <servlet-class>com.myservlet.showinfo</servlet-class> 17 <!-- 上面一句指定Servlet对象的完整位置,包含包名和类名 --> 18 </servlet> 19 <servlet-mapping> 20 <!-- 映射Servlet --> 21 <servlet-name>login</servlet-name> 22 <!--<servlet-name>与上面<Servlet>标签的<servlet-name>元素相对应,不可以随便起名 --> 23 <url-pattern>/login</url-pattern> 24 <!-- 上面一句话用于映射访问URL --> 25 </servlet-mapping> 26 27 </web-app>
链接:https://pan.baidu.com/s/1KoeTGLlj9nsO6g7kQSNKsw
提取码:nupd
复制这段内容后打开百度网盘手机App,操作更方便哦