DOC

mvc

By Hazel Perry,2014-07-25 00:03
10 views 0
mvc mvc mvc mvc mvc mvc mvc mvc mvc

1、定义vo

    package hhxy.jkx.vo;

public class Tuser {

     private String userid ;

     private String name ;

     private String password ;

     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;

     }

     public String getUserid() {

     return userid;

     }

     public void setUserid(String userid) {

     this.userid = userid;

     }

    }

2、定义接口

    package hhxy.jkx.mvc;

    import hhxy.jkx.vo.Tuser;

    public interface ITuser mvc {

     public boolean findLogin(Tuser user) throws Exception ;

    }

3、实现接口

    package hhxy.jkx. mvc.impl;

     import java.sql.Connection; import java.sql.ResultSet;

    import hhxy.jkx. mvc.ITusermvc; import hhxy.jkx.vo.Tuser;

    public class ITusermvcImpl implements ITuser mvc {

     private Connection conn = null;

     public ITusermvcImpl(Connection conn) {

     this.conn = conn;

     }

     public boolean findLogin(Tuser user) throws Exception {

     boolean flag = false;

     java.sql.PreparedStatement pstmt = null;

     try {

     String sql = "SELECT name FROM tuser WHERE userid=? AND

    password=?" ;

     pstmt = this.conn.prepareStatement(sql) ;

     pstmt.setString(1, user.getUserid()) ;

     pstmt.setString(2, user.getPassword()) ;

     ResultSet rs = pstmt.executeQuery() ;

     if(rs.next()){

     flag = true ;

     user.setName(rs.getString(1)) ; // 取出真实姓名

     }

     } catch (Exception e) {

     throw e;

     } finally {

     try {

     pstmt.close();

     } catch (Exception e) {

     throw e;

     }

     }

     return flag;

     }

    }

4、定义数据库连接

    package hhxy.jkx.dbc;

import java.sql.* ;

    public class DataBaseConnection{

     public static final String strl="jdbc:odbc:driver={Microsoft Access Driver

    (*.mdb)};DBQ=d: /user.mdb"; //注意修改路径

     public static final String URL ="sun.jdbc.odbc.JdbcOdbcDriver" ;

     private Connection conn = null ;

     public DataBaseConnection(){

     try{

     Class.forName(str) ;

     conn = DriverManager.getConnection(URL,””,””) ;

     }catch(Exception e){

     e.printStackTrace() ;

     }

     }

     public Connection getConnection(){

     return this.conn ;

     }

     public void close(){

     if(this.conn!=null){

     try{

     this.conn.close() ;

     }catch(Exception e){}

     }

     }

    };

5、定义代理

    package hhxy.jkx.dao.proxy;

import hhxy.jkx.dao.ITuserDAO;

    import hhxy.jkx.dao.impl.ITuserDAOImpl; import hhxy.jkx.dbc.DataBaseConnection; import hhxy.jkx.vo.Tuser;

public class ITuserDAOProxy implements ITuserDAO {

     private DataBaseConnection dbc = null;

     private ITuserDAO dao = null;

     public ITuserDAOProxy() {

     this.dbc = new DataBaseConnection();

     this.dao = new ITuserDAOImpl(this.dbc.getConnection());

     }

     public boolean findLogin(Tuser user) throws Exception {

     boolean flag = false ;

     try{

     flag = this.dao.findLogin(user) ;

     }catch(Exception e){

     throw e ;

     }

     return flag;

     }

}

6、定义工厂

    package hhxy.jkx.dao.factory;

import hhxy.jkx.dao.ITuserDAO;

    import hhxy.jkx.dao.proxy.ITuserDAOProxy;

public class DAOFactory {

     public static ITuserDAO getITuserDAOInstance(){

     return new ITuserDAOProxy() ;

     }

    }

7、定义servlet

    package hhxy.jkx.servlet;

import java.io.IOException;

    import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;

    import hhxy.jkx.dao.factory.DAOFactory; import hhxy.jkx.vo.Tuser;

    public class TuserServlet extends HttpServlet {

     public void doGet(HttpServletRequest request, HttpServletResponse

    response)

     throws ServletException, IOException {

     this.doPost(request, response);

     }

     public void doPost(HttpServletRequest request, HttpServletResponse

    response)

     throws ServletException, IOException {

     request.setCharacterEncoding("GBK"); // 设置乱码

     String path = "login.jsp"; // 默认的跳转页面

     String code = request.getParameter("code"); // 接收表单参数

     String rand = (String) request.getSession().getAttribute("rand");

     if (!rand.equals(code)) { // 验证码不正确?则设置错误信息

     request.setAttribute("err", "输入验证码不正确;");

    码正确?则执行数据库验证 } else { // 验证

     Tuser user = new Tuser();

     user.setUserid(request.getParameter("userid"));

     user.setPassword(request.getParameter("password"));

     try {

     if (DAOFactory.getITuserDAOInstance().findLogin(user)) {

     // 如果登陆成功?则跳转页面设置到success.jsp

     request.getSession().setAttribute("name", user.getName());

     path = "success.jsp";

     } else {

     request.setAttribute("err", "错误的用户名或密码;");

     }

     } catch (Exception e) {

     e.printStackTrace();

     }

     }

     request.getRequestDispatcher(path).forward(request, response); // 跳转

     }

    }

Report this document

For any questions or suggestions please email
cust-service@docsford.com