这是一个简单的MVC设计模式实现的登陆程序.
?
?
先实现DAO;
?
vo:
package org.mvcdemo.vo; public class User { private String userid; private String name; private String password; public void setUserId(String userid){ this.userid = userid; } public void setName(String name){ this.name = name; } public void setPassword(String password){ this.password = password; } public String getUserid(){ return this.userid; } public String getName(){ return this.name; } public String getPassword(){ return this.password; } }
?
dbc; //数据库连接;
package org.mvcdemo.dbc; import java.sql.*; public class DatabaseConnection { private static final String DBDRIVER = "org.gjt.mm.mysql.Driver"; private static final String DBURL = "jdbc:mysql://localhost:3306/mysql"; private static final String DBUSER = "root"; private static final String DBPASSWORD = "123"; 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; } } } }
?
定义接口类;
package org.mvcdemo.dao; import org.mvcdemo.vo.User; public interface IUserDAO { //现在完成的是登陆验证,那么登陆操作只有两种返回结果 public boolean findLogin(User user) throws Exception; }
?
实现接口;
package org.mvcdemo.dao.impl; import org.mvcdemo.vo.User; import org.mvcdemo.dbc.*; import org.mvcdemo.dao.*; import java.sql.*; public class UserDAOImpl implements IUserDAO { private Connection conn = null; private PreparedStatement pstmt = null; public UserDAOImpl(Connection conn){ this.conn = conn; } public boolean findLogin(User user)throws Exception{ boolean flag = false; String sql = "SELECT name FROM user1 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; } this.pstmt.close(); return flag; } }
?
代理类;
package org.mvcdemo.dao.proxy; import org.mvcdemo.vo.User; import org.mvcdemo.dbc.*; import org.mvcdemo.dao.*; import org.mvcdemo.dao.impl.*; import java.sql.*; 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(dbc.getConnection()); } public boolean findLogin(User user)throws Exception{ boolean flag = false; try{ flag = this.dao.findLogin(user);//调用真实主题,完成操作 }catch(Exception e){ throw e; }finally{ this.dbc.close(); } return flag; } }
?
工厂类;
package org.mvcdemo.dao.factory; import org.mvcdemo.dao.*; import org.mvcdemo.dao.proxy.*; public class DAOFactory { public static IUserDAO getIUserDAOInstance(){ return new UserDAOProxy(); } }
?
package org.mvcdemo.servlet; import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; import org.mvcdemo.dao.factory.*; import org.mvcdemo.vo.*; public class LoginServlet extends HttpServlet { public void doGet(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{ String path = "login.jsp"; String userid = req.getParameter("userid"); String userpass = req.getParameter("userpass"); List<String> info = new ArrayList<String>(); if(userid==null||"".equals(userid)){ info.add("用户id不能为空!"); } if(userpass==null||"".equals(userpass)){ info.add("密码不能为空!"); } if(info.size()==0){ User user = new User(); user.setUserId(userid); user.setPassword(userpass); try{ if(DAOFactory.getIUserDAOInstance().findLogin(user)){ info.add("用户登陆成功,欢迎" + user.getName() + "光临"); }else{ info.add("用户登陆失败,错误的用户名或密码"); } }catch(Exception e){ e.printStackTrace(); } } req.setAttribute("info",info); req.getRequestDispatcher(path).forward(req,resp); } public void doPost(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{ this.doGet(req,resp); } }
?
前台的JSP页面;
<%@ page contentType="text/html;charset=GBK"%> <%@ page import="java.util.*"%> <html> <head> <title>www.baidu.com</title> </head> <body> <script language="javascript"> function validate(f){ if(!(/^\w{5,15}$/.test(f.userid.value))){ alert("用户ID必须是5~15位!"); f.userid.focus(); return false; } if(!(/^\w{5,15}$/.test(f.userpass.value))){ alert("密码必须是5~15位!"); f.userpass.focus(); return false; } } </script> <% request.setCharacterEncoding("GBK"); %> <% List<String> info = (List<String>) request.getAttribute("info"); if(info!=null){ Iterator<String> iter = info.iterator(); while(iter.hasNext()){ %> <h4><%=iter.next()%></h4> <% } } %> <form action="LoginServlet" method="post" onSubmit="return validate(this)"> 用户ID:<input type="text" name="userid"><br> 密  码:<input type="password" name="userpass"><br> <input type="submit" value="登陆"> <input type="reset" value="重置"> </form> </body> </html>
?
数据库脚本;
USE mysql create table user( userid varchar(30) primary key, name varchar(30) not null, password varchar(32) not null ); insert into user(userid,name,password) values('admin','administrator','admin');
?
?
?