我在這裡用的不是maven項目,用的一般的web項目,所以需要用到的架包需要自己去下載添加,在項目中,一定注意環境的配置,我用的是jre1.7
1 新建項目
2 建立好MVC的管理包,導入對應的架包servlet
3 建立好與數據庫對應的實體類 teacher.java
public class Teacher { private int tid; private String tname; private String tpsw; public int getTid() { return tid; } public void setTid(int tid) { this.tid = tid; } public String getTname() { return tname; } public void setTname(String tname) { this.tname = tname; } public String getTpsw() { return tpsw; } public void setTpsw(String tpsw) { this.tpsw = tpsw; } public Teacher(String tname, String tpsw) { super(); this.tname = tname; this.tpsw = tpsw; } public Teacher(int tid, String tname, String tpsw) { super(); this.tid = tid; this.tname = tname; this.tpsw = tpsw; } public Teacher() { super(); } }
4 在WebContent新建login.jsp文件編寫登陸框
1 <body> 2 <form action="login" method="post"> 3 用戶名:<input type="text" name="tname"> 4 密碼:<input type="text" name="tpsw"> 5 <input type="submit" value="登錄"> 6 </form>
5 配置web.xml文件對應表單請求login
web.xml文件
注意:配置時<servlet-name>要在之前,否則會報錯<servlet-class>
<!-- 提交登錄請求 --> <servlet> <servlet-name>login</servlet-name> <servlet-class>com.zr.controller.LoginController</servlet-class> </servlet> <servlet-mapping> <servlet-name>login</servlet-name> <url-pattern>/login</url-pattern> </servlet-mapping>
6 編寫對應的請求實體類LoginController.java:繼承HttpServlet重寫doget(),dopost()方法,根據method請求的不同調用doget或者dopost方法
LoginController.java
import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import com.zr.model.Teacher; import com.zr.service.valiDateService; import com.zr.serviceIm.valiDateServiceImpl; public class LoginController extends HttpServlet{ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.doPost(req, resp); } protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //獲取前台form表單的input輸入框 String tname=req.getParameter("tname"); String tpsw=req.getParameter("tpsw"); Teacher tc=new Teacher(); tc.setTname(tname); tc.setTpsw(tpsw); valiDateService vds=new valiDateServiceImpl(); 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 interface TeacherDao { /** * 驗證老師是否存在 * @param tc * @return */ public Teacher validateTeacher(Teacher tc); }
8.1 編寫封裝類建立與數據庫的連接JDBCUtil.java
package JDBCUtil; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; public class JDBCUtil { //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 static Connection 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(); } return con; }
//關閉連接 public static void closeJDBC(Statement st,Connection con) throws SQLException{ if(st!=null){ st.close(); } if(con!=null){ con.close(); } } }
8.2 dao層的實現
import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import com.zr.dao.TeacherDao; import com.zr.model.Teacher; import JDBCUtil.JDBCUtil; public class TeacherDaoImpl implements TeacherDao{ /** * 輸入老師的對象,返回老師對象 * @param args */ public Teacher validateTeacher(Teacher tc) { Teacher teacher=new Teacher(); //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(); } return teacher; } }
9 Service層
public interface valiDateService { /** * @param tc * @return 老師對象 * 根據用戶輸入值驗證老師是否存在 */ public Teacher valiDateTeacher(Teacher tc); }
10 Service層實現ServiceImpl.java
import com.zr.dao.TeacherDao; import com.zr.daoIm.TeacherDaoImpl; import com.zr.model.Teacher; import com.zr.service.valiDateService; public class valiDateServiceImpl implements valiDateService{ public Teacher valiDateTeacher(Teacher tc) { //父類的引用指向子類的對象,父類可以直接調用子類的方法 TeacherDao teacherDao=new TeacherDaoImpl(); //調用dao層的方法驗證存在 Teacher teacher=teacherDao.validateTeacher(tc); return teacher; } }
11 com.zr.controller層
import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import com.zr.model.Teacher; import com.zr.service.valiDateService; import com.zr.serviceIm.valiDateServiceImpl; public class LoginController extends HttpServlet { protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.doPost(req, resp); } protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // 獲取前台form表單的input輸入框 String tname = req.getParameter("tname"); String tpsw = req.getParameter("tpsw"); // 將前台對象放入tc對象,作為輸入參數 Teacher tc = new Teacher(); tc.setTname(tname); tc.setTpsw(tpsw); // 調用Service層的方法傳入tc對象,並用t接收返回結果 valiDateService vds = new valiDateServiceImpl(); 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); } } }