• MVC案例-----登录验证


    结构图:

    View视图层:

     1 <%@ page language="java" contentType="text/html; charset=UTF-8"
     2     pageEncoding="UTF-8"%>
     3 <!DOCTYPE html>
     4 <html>
     5 <head>
     6 <meta charset="UTF-8">
     7 <title>登录</title>
     8 </head>
     9 <body>
    10     <form action="../Login" method="post">
    11         姓名:<input type="text" name="uname"><br>
    12         密码:<input type="password" name="upwd">
    13         <input type="submit" value="提交">
    14     </form>
    15 </body>
    16 </html>
    login.jsp

    控制器,LoginServlet:

     1 package dao;
     2 import java.io.IOException;
     3 import javax.servlet.ServletException;
     4 import javax.servlet.annotation.WebServlet;
     5 import javax.servlet.http.HttpServlet;
     6 import javax.servlet.http.HttpServletRequest;
     7 import javax.servlet.http.HttpServletResponse;
     8 
     9 import LoginDao.Login;
    10 import LoginDao.LoginDao;
    11 
    12 
    13 @WebServlet("/Login" )
    14 
    15 //控制器层:接收view请求,并分发给模型层处理
    16 public class LoginServlet extends HttpServlet {
    17 
    18     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    19         request.setCharacterEncoding("UTF-8");
    20         //获取前端数据
    21         String name = request.getParameter("uname");
    22         String pwd =request.getParameter("upwd");
    23         
    24         Login login = new Login(name,pwd);
    25         
    26         int result = LoginDao.login(login);
    27         if(result >0) {
    28             response.sendRedirect("jsp/welcome.jsp");//登录成功
    29         }else {
    30             response.sendRedirect("jsp/login.jsp");//登录失败,可能是密码有误,也可能是服务器出错
    31         }
    32     
    33     }
    34     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    35 
    36 
    37         doGet(request, response);
    38     }
    39 
    40 }
    LoginServlet.java

    Model层:

     1 package LoginDao;
     2 
     3     public class Login {
     4     private String uname;
     5     private String upwd;
     6     
     7     public Login(String uname, String upwd, int id) {    
     8         this.uname = uname;
     9         this.upwd = upwd;
    10         this.id = id;
    11     }
    12     public Login(String uname, String upwd) {
    13         this.uname = uname;
    14         this.upwd = upwd;    
    15     }
    16     public Login() {
    17     
    18     }
    19     private int id;
    20     public int getId() {
    21         return id;
    22     }
    23     public void setId(int id) {
    24         this.id = id;
    25     }
    26     public String getUname() {
    27         return uname;
    28     }
    29     public void setUname(String uname) {
    30         this.uname = uname;
    31     }
    32     public String getUpwd() {
    33         return upwd;
    34     }
    35     public void setUpwd(String upwd) {
    36         this.upwd = upwd;
    37     }
    38 
    39     
    40     
    41     
    42 
    43 
    44 }
    login.java

    构造方法:

     1 package LoginDao;
     2 
     3     public class Login {
     4     private String uname;
     5     private String upwd;
     6     
     7     public Login(String uname, String upwd, int id) {    
     8         this.uname = uname;
     9         this.upwd = upwd;
    10         this.id = id;
    11     }
    12     public Login(String uname, String upwd) {
    13         this.uname = uname;
    14         this.upwd = upwd;    
    15     }
    16     public Login() {
    17     
    18     }
    19     private int id;
    20     public int getId() {
    21         return id;
    22     }
    23     public void setId(int id) {
    24         this.id = id;
    25     }
    26     public String getUname() {
    27         return uname;
    28     }
    29     public void setUname(String uname) {
    30         this.uname = uname;
    31     }
    32     public String getUpwd() {
    33         return upwd;
    34     }
    35     public void setUpwd(String upwd) {
    36         this.upwd = upwd;
    37     }
    38 
    39 }
    login.jsp

    遇到的问题:

    索引超出范围:

    问题位置:模糊查询:select count(*) from login where uname = ? and upwd = ?

    原因:“=”与“?”之间有空格,去掉中间的空格即可

  • 相关阅读:
    谷歌关闭中国区购物搜索小思考
    java生成本地头文件用javah出错问题
    hadoop源代码分析(4)org.apache.hadoop.util包GenericOptionsParser类【原创】
    Ext.util.MixedCollection 用法
    eval 函数用法
    Rails Devise_demo
    rails rake 指南
    accepts_nested_attributes_for
    将Rails3.0无缝升级到Rails3.1.0.beta1
    spork + autotest 实现rails自动化测试
  • 原文地址:https://www.cnblogs.com/mi-9/p/12855370.html
Copyright © 2020-2023  润新知