JSP + JavaBean + Servlet实现MVC设计模式

1、流程图:

《JSP + JavaBean + Servlet实现MVC设计模式》

2、代码清单

《JSP + JavaBean + Servlet实现MVC设计模式》

数据库脚本:

 1 DROP TABLE IF EXISTS `user`;
 2 
 3 CREATE TABLE `user` (
 4   `userid` varchar(30) NOT NULL,
 5   `name` varchar(30) NOT NULL,
 6   `password` varchar(30) NOT NULL,
 7   PRIMARY KEY  (`userid`)
 8 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
 9 
10 INSERT INTO `user` VALUES ('admin', 'administrator', 'admin');

User 类:

《JSP + JavaBean + Servlet实现MVC设计模式》
《JSP + JavaBean + Servlet实现MVC设计模式》

 1 package com.shawn.mvcdemo.vo;
 2 
 3 public class User{
 4     private String userID;
 5 
 6     private String name;
 7 
 8     private String password;
 9 
10     public String getUserID(){
11         return this.userID;
12     }
13 
14     public String getName(){
15         return this.name;
16     }
17 
18     public String getPassword(){
19         return this.password;
20     }
21 
22     public void setUserID(String userID){
23         this.userID = userID;
24     }
25 
26     public void setName(String name){
27         this.name = name;
28     }
29 
30     public void setPassword(String password){
31         this.password = password;
32     }
33 
34 }

View Code

DatabaseConnection 类:

《JSP + JavaBean + Servlet实现MVC设计模式》
《JSP + JavaBean + Servlet实现MVC设计模式》

 1 package com.shawn.mvcdemo.dbc;
 2 
 3 import java.sql.*;
 4 
 5 public class DatabaseConnection{
 6     private static final String DBDRIVER = "org.gjt.mm.mysql.Driver";    //数据库驱动
 7     private static final String DBURL = "jdbc:mysql://localhost:3306/mldn";    //数据库URL
 8     private static final String DBUSER = "root";    //用户名
 9     private static final String DBPASSWORD = "root";    //密码
10 
11     private Connection conn = null;
12 
13     public DatabaseConnection() throws Exception{
14         try{
15             Class.forName(DBDRIVER);
16             this.conn = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD);
17         } catch(Exception e){
18             throw e;
19         }
20     }
21 
22     public Connection getConnection(){
23         return this.conn;
24     }
25 
26     public void close() throws Exception{
27         if(this.conn != null){
28             try{
29                 this.conn.close();
30             } catch(Exception e){
31                 throw e;
32             }
33         }
34     }
35 
36     public static void main(String args[]){
37         try{
38             System.out.println(new DatabaseConnection().getConnection());
39         } catch(Exception e){
40             e.printStackTrace();
41         }
42     }
43 }

View Code

IUserDAO 接口:

《JSP + JavaBean + Servlet实现MVC设计模式》
《JSP + JavaBean + Servlet实现MVC设计模式》

1 package com.shawn.mvcdemo.dao;
2 
3 import com.shawn.mvcdemo.vo.User;
4 
5 public interface IUserDAO{
6     //现在完成的是登录验证,那么登录操作只有两种返回结果
7     public boolean findLogin(User user) throws Exception;
8 }

View Code

UserDAOImpl 类:

《JSP + JavaBean + Servlet实现MVC设计模式》
《JSP + JavaBean + Servlet实现MVC设计模式》

 1 package com.shawn.mvcdemo.dao.impl;
 2 
 3 import java.sql.*;
 4 
 5 import com.shawn.mvcdemo.vo.User;
 6 import com.shawn.mvcdemo.dbc.*;
 7 import com.shawn.mvcdemo.dao.*;
 8 
 9 public class UserDAOImpl implements IUserDAO{
10     private Connection conn = null;
11 
12     private PreparedStatement pstmt = null;
13 
14     public UserDAOImpl(Connection conn){
15         this.conn = conn;
16     }
17 
18     public boolean findLogin(User user) throws Exception{
19         boolean flag = false;
20         String sql = "select name from user where userid = ? and password = ?";
21         this.pstmt = this.conn.prepareStatement(sql);
22         this.pstmt.setString(1,user.getUserID());
23         this.pstmt.setString(2,user.getPassword());
24         ResultSet rs = this.pstmt.executeQuery();
25         if(rs.next()){
26             user.setName(rs.getString("name"));
27             flag = true;
28         }
29         this.pstmt.close();
30         return flag;
31     }
32 }

View Code

UserDAOProxy 类:

《JSP + JavaBean + Servlet实现MVC设计模式》
《JSP + JavaBean + Servlet实现MVC设计模式》

 1 package com.shawn.mvcdemo.dao.proxy;
 2 
 3 import java.sql.*;
 4 
 5 import com.shawn.mvcdemo.vo.User;
 6 import com.shawn.mvcdemo.dbc.*;
 7 import com.shawn.mvcdemo.dao.*;
 8 import com.shawn.mvcdemo.dao.impl.*;
 9 
10 public class UserDAOProxy implements IUserDAO{
11     private DatabaseConnection dbc = null;
12     private IUserDAO dao = null;
13 
14     public UserDAOProxy(){
15         try{
16             this.dbc = new DatabaseConnection();
17         } catch(Exception e){
18             e.printStackTrace();
19         }
20         this.dao = new UserDAOImpl(this.dbc.getConnection());
21     }
22 
23     public boolean findLogin(User user) throws Exception{
24         boolean flag = false;
25         try{
26             flag = this.dao.findLogin(user);    //调用真实主题,完成操作
27         } catch(Exception e){
28             throw e;
29         } finally{
30             this.dbc.close();
31         }
32         return flag;
33     }
34 
35 
36 }

View Code

DAOFactory 类:

《JSP + JavaBean + Servlet实现MVC设计模式》
《JSP + JavaBean + Servlet实现MVC设计模式》

 1 package com.shawn.mvcdemo.factory;
 2 
 3 import com.shawn.mvcdemo.dao.*;
 4 import com.shawn.mvcdemo.dao.proxy.*;
 5 
 6 public class DAOFactory{
 7     public static IUserDAO getIUserDAOInstance(){
 8         return new UserDAOProxy();
 9     }
10 }

View Code

LoginServlet 类:

《JSP + JavaBean + Servlet实现MVC设计模式》
《JSP + JavaBean + Servlet实现MVC设计模式》

 1 package com.shawn.mvcdemo.servlet;
 2 
 3 import java.io.*;
 4 import java.util.*;
 5 
 6 import javax.servlet.*;
 7 import javax.servlet.http.*;
 8 import com.shawn.mvcdemo.factory.*;
 9 import com.shawn.mvcdemo.vo.*;
10 
11 public class LoginServlet extends HttpServlet{
12 
13     public void doGet(HttpServletRequest req,HttpServletResponse resp)
14         throws ServletException,IOException{
15         String path = "login.jsp";
16         String userID = req.getParameter("userID");
17         String password = req.getParameter("password");
18         List<String> info = new ArrayList<String>();//收集错误信息
19         if(userID == null || "".equals(userID)){
20             info.add("用户id不能为空!");
21         }
22         if(password == null || "".equals(password)){
23             info.add("密码不能为空!");
24         }
25         if(info.size() == 0){//里面没有记录任何的错误
26             User user = new User();
27             user.setUserID(userID);
28             user.setPassword(password);
29             try{
30                 if(new DAOFactory().getIUserDAOInstance().findLogin(user)){
31                     info.add("用户登陆成功,欢迎" + user.getName() + "光临!");
32                     System.out.println(1);
33                 } else {
34                     info.add("用户登录失败,错误的用户名和密码!");
35                     System.out.println(2);
36                 }
37             }catch(Exception e){
38                 e.printStackTrace();
39             }
40         }
41         req.setAttribute("info",info);
42         req.getRequestDispatcher(path).forward(req,resp);
43     }
44 
45     public void doPost(HttpServletRequest req,HttpServletResponse resp)
46         throws ServletException,IOException{
47         this.doGet(req,resp);
48     }
49 
50 }

View Code

login.jsp 页面:

《JSP + JavaBean + Servlet实现MVC设计模式》
《JSP + JavaBean + Servlet实现MVC设计模式》

 1 <%@ page contentType="text/html" pageEncoding="utf-8" import="java.util.*"%>
 2 <html>
 3     <head>
 4         <title>www.mldnjava.cn, MLDN高端Java培训</title>
 5         <%
 6             request.setCharacterEncoding("utf-8");
 7         %>
 8         <%
 9             List<String> info = (ArrayList<String>)request.getAttribute("info");
10             String userID = request.getParameter("userID");
11             String password = request.getParameter("password");
12         %>
13         <script>
14             window.onload = function(){
15                 var objForm = document.getElementById("form1");
16 
17                 objForm.onsubmit = function(){
18                     if(!(/^\w{5,15}$/.test(this.userID.value))){
19                         alert("用户ID必须是5~15位!");
20                         this.userID.focus();
21                         return false;
22                     }
23                     
24                     if(!(/^\w{5,15}$/.test(this.password.value))){
25                         alert("密码必须是5~15位!");
26                         this.password.focus();
27                         return false;
28                     }
29                 }
30 
31             }
32             
33         </script>
34     </head>
35     
36     <body>
37         <form id="form1" action="login" method="post">
38             用户ID:<input type="text" name="userID" value="<%= (userID == null) ? "" : userID %>" /><br>
39             密&nbsp;&nbsp;码:<input type="password" name="password" value="<%= (password == null) ? "" : password %>" /><br>
40             <input type="submit" value="登录" />
41             <input type="reset" value="重置" />
42         </form>
43         <%
44             if(info != null){
45                 for(String str : info){
46         %>
47             <h3><%=str %></h3>
48         <%
49                 }
50             }
51         %>
52     </body>
53 </html>

View Code

web.xml 配置:

《JSP + JavaBean + Servlet实现MVC设计模式》
《JSP + JavaBean + Servlet实现MVC设计模式》

 1 <?xml version="1.0" encoding="ISO-8859-1"?>
 2 
 3 <web-app xmlns="http://java.sun.com/xml/ns/javaee"
 4    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 6    version="2.5"> 
 7 
 8   <display-name>Hello World!</display-name>
 9   <welcome-file-list>
10     <welcome-file>main.html</welcome-file>
11   </welcome-file-list>
12 
13   <servlet>
14     <servlet-name>login</servlet-name>
15     <servlet-class>com.shawn.mvcdemo.servlet.LoginServlet</servlet-class>
16   </servlet>
17 
18   <servlet-mapping>
19     <servlet-name>login</servlet-name>
20     <url-pattern>/mvclogin/login</url-pattern>
21   </servlet-mapping>
22 </web-app>

View Code

 

    原文作者:流年如水烟雨随风
    原文地址: https://www.cnblogs.com/ShawnYang/p/7443528.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞