我在这里用的不是maven项目,用的一般的web项目,所以需要用到的架包需要自己去下载添加,在项目中,一定注意环境的配置,我用的是jre1.7
1 新建项目
2 建立好MVC的管理包,导入对应的架包servlet
3 建立好与数据库对应的实体类 teacher.java
public classTeacher {private inttid;privateString tname;privateString tpsw;public intgetTid() {returntid;
}public void setTid(inttid) {this.tid =tid;
}publicString getTname() {returntname;
}public voidsetTname(String tname) {this.tname =tname;
}publicString getTpsw() {returntpsw;
}public voidsetTpsw(String tpsw) {this.tpsw =tpsw;
}publicTeacher(String tname, String tpsw) {super();this.tname =tname;this.tpsw =tpsw;
}public Teacher(inttid, String tname, String tpsw) {super();this.tid =tid;this.tname =tname;this.tpsw =tpsw;
}publicTeacher() {super();
}
}
4 在WebContent新建login.jsp文件编写登陆框
1
2
3 用户名:
4 密码:
5
6
5 配置web.xml文件对应表单请求login
web.xml文件
注意:配置时要在之前,否则会报错
login
com.zr.controller.LoginController
login
/login
6 编写对应的请求实体类LoginController.java:继承HttpServlet重写doget(),dopost()方法,根据method请求的不同调用doget或者dopost方法
LoginController.java
importjava.io.IOException;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjavax.servlet.http.HttpSession;importcom.zr.model.Teacher;importcom.zr.service.valiDateService;importcom.zr.serviceIm.valiDateServiceImpl;public class LoginController extendsHttpServlet{protected voiddoGet(HttpServletRequest req, HttpServletResponse resp)throwsServletException, IOException {super.doPost(req, resp);
}protected voiddoPost(HttpServletRequest req, HttpServletResponse resp)throwsServletException, IOException {//获取前台form表单的input输入框
String tname=req.getParameter("tname");
String tpsw=req.getParameter("tpsw");
Teacher tc=newTeacher();
tc.setTname(tname);
tc.setTpsw(tpsw);
valiDateService vds=newvaliDateServiceImpl();
Teacher t=vds.valiDateTeacher(tc);
HttpSession session=req.getSession();
session.setAttribute("teacher", t);if (t!=null) {//返回的不是空值,重定向到登录成功界面
req.getRequestDispatcher("main.jsp").forward(req, resp);
}else{//返回空值,请求转发到登录界面
resp.sendRedirect("login.jsp");
}
}
}
7 从后台dao层写到control层
public interfaceTeacherDao {/*** 验证老师是否存在
*@paramtc
*@return
*/
publicTeacher validateTeacher(Teacher tc);
}
8.1 编写封装类建立与数据库的连接JDBCUtil.java
packageJDBCUtil;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.SQLException;importjava.sql.Statement;public classJDBCUtil {//1.数据库地址 (根据不同的数据标准是不一样)
private final static String DBURL = "jdbc:mysql://localhost:3306/student_crm?useUnicode=true&characterEncoding=UTF8";//2.设置用户和密码
private final static String USERNAME = "root";private final static String PASSWORD = "root";//3.设置驱动名称 (根据不同的数据标准是不一样)
private final static String DBDRIVER = "com.mysql.jdbc.Driver";/*** 获取数据库连接
*@return返回数据库连接*/
public staticConnection getConnection(){
Connection con= null;try{
Class.forName(DBDRIVER);
con=DriverManager.getConnection(DBURL, USERNAME, PASSWORD);
}catch(ClassNotFoundException e) {//TODO Auto-generated catch block
e.printStackTrace();
}catch(SQLException e) {//TODO Auto-generated catch block
e.printStackTrace();
}returncon;
}
//关闭连接public static void closeJDBC(Statement st,Connection con) throwsSQLException{if(st!=null){
st.close();
}if(con!=null){
con.close();
}
}
}
8.2 dao层的实现
importjava.sql.Connection;importjava.sql.PreparedStatement;importjava.sql.ResultSet;importjava.sql.SQLException;importcom.zr.dao.TeacherDao;importcom.zr.model.Teacher;importJDBCUtil.JDBCUtil;public class TeacherDaoImpl implementsTeacherDao{/*** 输入老师的对象,返回老师对象
*@paramargs*/
publicTeacher validateTeacher(Teacher tc) {
Teacher teacher=newTeacher();//sql语句
StringBuffer sql=new StringBuffer("select * from teacher where tname=? and tpsw=?");//获取数据库连接
Connection con=JDBCUtil.getConnection();try{
PreparedStatement pst=con.prepareStatement(sql.toString());
pst.setString(1, tc.getTname());
pst.setString(2, tc.getTpsw());//返回一个结果集
ResultSet rs=pst.executeQuery();if(rs.next()) {//把结果集里面的数据放入对应的teacher对象
teacher=new Teacher(rs.getInt("tid"),rs.getString("tname"),rs.getString("tpsw"));
}
}catch(SQLException e) {
e.printStackTrace();
}returnteacher;
}
}
9 Service层
public interfacevaliDateService {/***@paramtc
*@return老师对象
* 根据用户输入值验证老师是否存在*/
publicTeacher valiDateTeacher(Teacher tc);
}
10 Service层实现ServiceImpl.java
importcom.zr.dao.TeacherDao;importcom.zr.daoIm.TeacherDaoImpl;importcom.zr.model.Teacher;importcom.zr.service.valiDateService;public class valiDateServiceImpl implementsvaliDateService{publicTeacher valiDateTeacher(Teacher tc) {//父类的引用指向子类的对象,父类可以直接调用子类的方法
TeacherDao teacherDao=newTeacherDaoImpl();//调用dao层的方法验证存在
Teacher teacher=teacherDao.validateTeacher(tc);returnteacher;
}
}
11 com.zr.controller层
importjava.io.IOException;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjavax.servlet.http.HttpSession;importcom.zr.model.Teacher;importcom.zr.service.valiDateService;importcom.zr.serviceIm.valiDateServiceImpl;public class LoginController extendsHttpServlet {protected void doGet(HttpServletRequest req, HttpServletResponse resp) throwsServletException, IOException {super.doPost(req, resp);
}protected void doPost(HttpServletRequest req, HttpServletResponse resp) throwsServletException, IOException {//获取前台form表单的input输入框
String tname = req.getParameter("tname");
String tpsw= req.getParameter("tpsw");//将前台对象放入tc对象,作为输入参数
Teacher tc = newTeacher();
tc.setTname(tname);
tc.setTpsw(tpsw);//调用Service层的方法传入tc对象,并用t接收返回结果
valiDateService vds = newvaliDateServiceImpl();
Teacher t=vds.valiDateTeacher(tc);//获取JSP作用域session,将老师t对象放入session
HttpSession session =req.getSession();int a = t.getTid();//最好根据返回的老师的id进行判断
if (a != 0) {//返回的有id,重定向到登录成功界面
req.getRequestDispatcher("main.jsp").forward(req, resp);
session.setAttribute("teacher", t);
}else{//返回空值,请求转发到登录界面
req.getRequestDispatcher("login.jsp").forward(req, resp);
}
}
}