• MVC设计模式应用


    1. MVC登录程序清单                                                                                                                                                                                                                                                                                                                                                            
      1 User   JAVABean 用户登录操作类,跟数据库中表的信息对应
      2 DatabaseConnection JavaBean 负责数据库的连接和关闭操作
      3 IUserDAO JavaBean 定义登录操作的DAO接口
      4 UserDAOImpl JavaBean DAO接口的真实实现类,完成具体的登录验证
      5 UserDAOProxy JavaBean 定义代理操作,负责数据库的打开和关闭并且调用真实主体
      6 UserDAOFactory JavaBean 工厂类,取得DAO接口的实例
      7 LoginServlet Servlet 接收请求参数,进行参数验证,调用DAO完成具体的登录验证,并更具DAO的验证结果返回登录信息
      8 index.html jsp 提供用户输入
      9 login.jsp jsp 返回信息

    User类

    /**
     * 
     */
    package org.lh.mvcdemo.vo;
    
    /**
     * @author huanli2
     *
     */
    public class User {
        private String userid;
        private String name;
        private String password;
        public String getUserid() {
            return userid;
        }
        public void setUserid(String userid) {
            this.userid = userid;
            
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getPassword() {
            return password;
        }
        public void setPassword(String password) {
            this.password = password;
        }
        
    }
    1. DateBaseConnectio类

      

    /**
     * 
     */
    package org.lh.mvcdemo.dbc;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    
    /**
     * @author huanli2
     *
     */
    public class DatabaseConnection {
        private static final String DBDRIVER = "org.gjt.mm.mysql.Driver";
        private static final String DBURL = "jdbc:mysql://localhost:3306/mydb";
        private static final String DBUSER = "root";
        private static final String DBPASSWORD = "root";
        private Connection conn = null;
        public DatabaseConnection() throws Exception{
            try{
                Class.forName(DBDRIVER);
                this.conn=DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD);
            }catch(Exception e){
                throw e;
            }
        }
        public Connection getConnection() {
            return this.conn;
        }
        public void close() throws Exception{
            if(this.conn!=null){
                try{
                    this.conn.close();
                }catch(Exception e){
                    throw e;
                }
            }
        }
        
    }
    1. IUserDAO类
    /**
     * 
     */
    package org.lh.mvcdemo.dao;
    
    import org.lh.mvcdemo.vo.User;
    
    /**
     * @author huanli2
     *
     */
    public interface IUserDAO {
        public boolean findLogin(User user) throws Exception;
    }
    1. UserDAOImpl
    /**
     * 
     */
    package org.lh.mvcdemo.dao.ipl;
    
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    
    import org.lh.mvcdemo.dao.IUserDAO;
    import org.lh.mvcdemo.vo.User;
    
    /**
     * @author huanli2
     *
     */
    public class UserDAOImpl implements IUserDAO{
    
        private Connection conn=null;
        private PreparedStatement pstmt = null;
        public UserDAOImpl (Connection conn){
            this.conn = conn;
        }
        /* (non-Javadoc)
         * @see org.lh.mvcdemo.dao.IUserDAO#findLogin(org.lh.mvcdemo.vo.User)
         */
        public boolean findLogin(User user) throws Exception {
            // TODO Auto-generated method stub
            boolean flag =false;
            try{
                String sql = "SELECT name FROM info WHERE userid=? AND password=?";
                this.pstmt=this.conn.prepareStatement(sql);
                this.pstmt.setString(1, user.getUserid());
                this.pstmt.setString(2, user.getPassword());
                ResultSet rs = this.pstmt.executeQuery();
                if(rs.next()){
                    user.setName(rs.getString(1));
                    flag=true;
                }
            }catch(Exception e){
                throw e;
            }finally{
                if(this.pstmt!=null){
                    try{
                        this.pstmt.close();
                    }catch(Exception e){
                        throw e;
                    }
                }
            }
            return flag;
        }
    
    }

    UserDAOProxy

    /**
     * 
     */
    package org.lh.mvcdemo.dao.proxy;
    
    import org.lh.mvcdemo.dao.IUserDAO;
    import org.lh.mvcdemo.dao.ipl.UserDAOImpl;
    import org.lh.mvcdemo.dbc.DatabaseConnection;
    import org.lh.mvcdemo.vo.User;
    
    /**
     * @author huanli2
     *
     */
    public class UserDAOProxy implements IUserDAO{
        private DatabaseConnection dbc = null;
        private IUserDAO dao = null;
        public UserDAOProxy(){
            try{
                this.dbc=new DatabaseConnection();
            }catch(Exception e){
                e.printStackTrace();
            }
            this.dao=new UserDAOImpl(this.dbc.getConnection());
        }
    
        /* (non-Javadoc)
         * @see org.lh.mvcdemo.dao.IUserDAO#findLogin(org.lh.mvcdemo.vo.User)
         */
        public boolean findLogin(User user) throws Exception {
            // TODO Auto-generated method stub
            boolean flag = false;
            try{
                flag = this.dao.findLogin(user);
            }catch(Exception e){
                throw e;
            }finally{
                this.dbc.close();
            }
            return flag;
        }
        
    }

    DAOFactory

    /**
     * 
     */
    package org.lh.mvcdemo.factory;
    
    import org.lh.mvcdemo.dao.IUserDAO;
    import org.lh.mvcdemo.dao.proxy.UserDAOProxy;
    
    /**
     * @author huanli2
     *
     */
    public class DAOFactory {
        public static IUserDAO getIUserDAOInstance(){
            return new UserDAOProxy();
        }
    }

    LoginServlet

    package org.lh.mvcdemo.servlet;
    
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.lh.mvcdemo.factory.DAOFactory;
    import org.lh.mvcdemo.vo.User;
    
    /**
     * Servlet implementation class LoginServlet
     */
    @WebServlet("/LoginServlet")
    public class LoginServlet extends HttpServlet {
        private static final long serialVersionUID = 1L;
           
        /**
         * @see HttpServlet#HttpServlet()
         */
        public LoginServlet() {
            super();
            // TODO Auto-generated constructor stub
        }
    
        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            String path = "login.jsp";
            String userid = request.getParameter("userid");
            String userpass = request.getParameter("userpass");
            List<String> info = new ArrayList<String>();
            if(userid==null||"".equals(userpass)){
                info.add("用户id不能为空!");
            }
            
            if(userpass==null||"".equals(userpass)){
                info.add("密码不能为空!");
            }
            if(info.size()==0){
                User user = new User();
                user.setPassword(userpass);
                user.setUserid(userid);
                try{
                    if(DAOFactory.getIUserDAOInstance().findLogin(user)){
                        info.add("用户登录成功,欢迎"+user.getName()+"光临");
                    }else{
                        info.add("用户登录失败,错误的用户名和密码!");
                    }
                }catch(Exception e){
                    e.printStackTrace();
                }
            }
            request.setAttribute("info", info);
            request.getRequestDispatcher(path).forward(request, response);
    //        String userid = request.getParameter("userid");
    //        String userpass = request.getParameter("userpass");
    //        User user = new User();
    //        user.setPassword(userpass);
    //        user.setUserid(userid);
    //        try{
    //            if(DAOFactory.getIUserDAOInstance().findLogin(user)){
    //                System.out.println("用户登录成功,欢迎"+user.getName()+"光临");
    //            }else{
    //                System.out.println("用户登录失败,错误的用户名和密码!");
    //            }
    //        }catch(Exception e){
    //            e.printStackTrace();
    //        }
        }
    
        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            this.doGet(request, response);
        }
    
    }

    index.html

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    <form method="post" action="LoginServlet.do" >
    用户Id: <input type="text" name="userid"><br>&nbsp;&nbsp;<input type="password" name="userpass"><br>
    
    <input type="submit" value="登录">
    <input type="reset" value="重置">
    </form>
    </body>
    </html>

    login.jsp

    <%@ page language="java" contentType="text/html; charset=GBK" import="java.util.*"
        pageEncoding="GBK"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
    <%
    List<String> info = (List<String>)request.getAttribute("info");
    if(info!=null){
        Iterator it = info.iterator();
        while(it.hasNext()){
            %> <h4><%=it.next() %></h4><% 
        }
    }
    %>
    </body>
    </html>

    web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <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>m</display-name>
      <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
      </welcome-file-list>
      <servlet>
          <servlet-name>login</servlet-name>
          <servlet-class>org.lh.mvcdemo.servlet.LoginServlet</servlet-class>
      </servlet>
      <servlet-mapping>
          <servlet-name>login</servlet-name>
          <url-pattern>/LoginServlet.do</url-pattern>
      </servlet-mapping>
    </web-app>
  • 相关阅读:
    javascript模拟枚举
    javascript实现命名空间效果
    js数组去重
    文件上传插件
    script标签的defer属性
    js数组复制
    更改visual studio2010的主题
    关于json格式在.net前后台传值的详细示例
    where T : class泛型类型约束
    JavaScript模仿鼠标拖动选择功能
  • 原文地址:https://www.cnblogs.com/yixianyixian/p/3501857.html
Copyright © 2020-2023  润新知