数据库里的字段
# MySQL-Front 3.2  (Build 7.19)
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES 'gbk' */;
# Host: 127.0.0.1    Database: qy
# ------------------------------------------------------
# Server version 5.0.18-nt
#
# Table structure for table qyuser
#
DROP TABLE IF EXISTS `qyuser`;javaservlet和jsp的比较
CREATE TABLE `qyuser` (
  `Id` int(11) NOT NULL auto_increment,
  `uanme` varchar(50) default NULL,
  `upwd` varchar(50) default NULL,
  `tswt` varchar(50) default NULL,
  `wtda` varchar(50) default NULL,
  PRIMARY KEY  (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk;
#
# Dumping data for table qyuser
#
INSERT INTO `qyuser` VALUES (1,'admin','admin','admin','admin');
INSERT INTO `qyuser` VALUES (2,'admin99,'admin99','admin99','admin99');
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
//bean目录
package com.qy.Bean;
public class UserBean {
    //用户id
    private int Id;
    public int getId() {
        return Id;
    }
    public void setId(int id) {
        Id = id;
    }
    public String getUname() {
        return uname;
    }
    public void setUname(String uname) {
        this.uname = uname;
    }
    public String getUpwd() {
        return upwd;
    }
    public void setUpwd(String upwd) {
        this.upwd = upwd;
    }
    public String getTswt() {
        return tswt;
    }
    public void setTswt(String tswt) {
        this.tswt = tswt;
    }
    public String getWtda() {
        return wtda;
    }
    public void setWtda(String wtda) {
        this.wtda = wtda;
    }
    //用户姓名
    private String uname;
    //用户密码
    private String upwd;
    //提示问题
    private String tswt;
    //问题答案
    private String wtda;
}
//
//javadao
package com.qy.Dao;
public interface UserDao {
    //用户登录接口
    public boolean islogin(String uanme,String upwd)throws Exception;
    }
//javaimpl
package com.qy.Dao;
public interface UserDao {
    //用户登录接口
    public boolean islogin(String uanme,String upwd)throws Exception;
    }
//javaDatabase
package com.qy.DataBase;
import java.sql.Connection;
import java.sql.DriverManager;
public class UserDataBase {
    // 用户名
    public  String uname = "root";
    // 用户密码
    public  String upwd = "admin";
    // 驱动程序
    public  String DRIVER = "sql.jdbc.Driver";
    // 连接地址
    public  String URL = "jdbc:mysql://127.0.0.1:3306/qy";
    // 连接
    public Connection conn;
    // 构造方法
    public UserDataBase() {
        try {
            // 加载驱动程序
            Class.forName(DRIVER);
            // 连接数据库
            conn = Connection(URL,uname,upwd);
        } catch (Exception e) {
            // TODO: handle exception
        }
    }
    // 连接
    public Connection conn() {
        // 返回连接
        ;
    }
   
    //关闭
    public void close(){
        try {
            lose();
        } catch (Exception e) {
            // TODO: handle exception
        }
    }
}
//javaFactory
package com.qy.Factory;
import com.qy.Dao.UserDao;
import com.qy.Daoimpl.UserDaoimpl;
public class UserDaoimplFactory {
    //把接口和实现类整合在一起
    public static UserDao userFactory(){
        return new UserDaoimpl();
    }
}
//javaServlet
package com.qy.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 com.qy.Factory.UserDaoimplFactory;
public class UserServlet 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 {
        // 得到你输入的用户名
        String uanme = Parameter("uanme");
        // 得到你输入的密码
        String upwd = Parameter("upwd");
        // 下面掉用工厂
        try {
            if (UserDaoimplFactory.userFactory().islogin(uanme, upwd)) {
                // 去成功页面
                Session().setAttribute("u", uanme);
                response.sendRedirect("../yes.jsp");
            } else {
                // 去失败页面
                request.setAttribute("error", "您输入的用户名或用户密码有误");
                RequestDispatcher("../error.jsp").forward(request,
                        response);
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
//index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = ContextPath();
String basePath = Scheme()+"://"+ServerName()+":"+ServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。