本文實例為大家分享了JavaWeb登陸功能的方法,供大家參考,具體內容如下
首先我們要JavaWeb登陸的基本流程:JSP頁面發送請求——>Servlet——>Servlet通過調用方法從數據庫中得到數據并將結果返回頁面。
我們先建立三個jsp頁面,包括login.jsp(登陸頁面)、index.jsp(顯示登陸成功后的信息)、error.jsp(登錄失敗的頁面),其中后兩個頁面的內容可以隨意寫,而login.jsp頁面的主要內容如下:
1
2
3
4
5
|
< form action = "LoginServlet" method = "post" > 用戶名:< input type = "text" name = "userName" /> 密碼:< input type = "password" name = "password" /> < input type = "submit" value = "提交" /> </ form > |
在login.jsp文件的開頭我們需要將pageEncoding="ISO-8859-1"改為pageEncoding="utf-8"(同時不要忘記設置開發工具的編碼格式,不然jsp頁面會顯示亂碼)
根據用戶名和密碼兩個屬性我們建立相應的實體類,并添加get和set方法,代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
public class User { private String userName; private String password; public String getUserName() { return userName; } public void setUserName(String userName) { this .userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this .password = password; } } |
而jsp頁面中的action=“LoginServlet”是指將請求發送到Servlet處理。接下來我們轉到Servlet來進行處理:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
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.test.dao.UserDao; //創建時為Servlet而不是Class,需要在web.xml中進行配置,配置的代碼Myeclipse將自動生成 public class LoginServlet extends HttpServlet { //創建UserDao的對象,以便于查詢數據庫 UserDao userDao= new UserDao(); //以下doGet方法和doPost方法分別對應form表單中的method="get"和method="post" public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //利用getParameter方法獲取到前臺文本框中輸入的值,其中括號內的內容為<input/>標簽中的name屬性 String userName=request.getParameter( "userName" ); String password=request.getParameter( "password" ); //調用UserDao中的getSelect方法并獲取到返回值 boolean flag=userDao.getSelect(userName, password); //若用戶名和密碼存在則轉發到index.jsp頁面,否則重定向到error.jsp頁面 if (flag) { request.getRequestDispatcher( "index.jsp" ).forward(request, response); } else response.sendRedirect( "error.jsp" ); } } |
注釋 中已經說的很明白了,就不再重復了,可以看看第26行和29行,其中26行是轉發,29行是重定向,感興趣的小伙伴可以查查兩者的區別。剩下的一部分就是我們之前提到過的關于數據庫的查詢操作了,我們在23行進行了調用,下面我們完成調用的方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
package com.test.dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; public class UserDao { //連接數據庫的代碼 public Connection getCon() { //數據庫連接名稱 String username= "root" ; //數據庫連接密碼 String password= "" ; String driver= "com.mysql.jdbc.Driver" ; //其中test為數據庫名稱 String url= "jdbc:mysql://localhost:3306/test" ; Connection conn= null ; try { Class.forName(driver); conn=(Connection) DriverManager.getConnection(url,username,password); } catch (Exception e){ e.printStackTrace(); } return conn; } //進行查詢的方法,若含有滿足條件的數據則返回true public boolean getSelect(String userName,String password) { boolean flag= false ; String sql = "select * from user where userName='" +userName+ "' and password='" +password+ "'" ; Connection conn = getCon(); PreparedStatement pst = null ; try { pst = (PreparedStatement) conn.prepareStatement(sql); ResultSet rs = pst.executeQuery(); if (rs.next()) { flag= true ; } } catch (Exception e) { } return flag; } } |
在這個方法中我們首先連接數據庫,然后在查詢的方法中傳入從jsp頁面獲取到的userName和password,判斷數據庫中是否存在此用戶名和密碼的用戶,如果存在則返回true,否則返回false(不要忘記導入數據庫鏈接的包)。
至于數據庫中的字段則參照實體類User建立即可,即包含userName和password兩個屬性,如果數據庫鏈接還有問題的請參照之前的關于數據庫部分的隨筆。
最后看一下web.xml中的配置:
1
2
3
4
5
6
7
8
9
10
11
12
|
<? xml version = "1.0" encoding = "UTF-8" ?> < web-app xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns = "http://java.sun.com/xml/ns/javaee" xsi:schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id = "WebApp_ID" version = "3.0" > < servlet > < servlet-name >LoginServlet</ servlet-name > < servlet-class >com.test.servlet.LoginServlet</ servlet-class > </ servlet > < servlet-mapping > < servlet-name >LoginServlet</ servlet-name > < url-pattern >/LoginServlet</ url-pattern > </ servlet-mapping > </ web-app > |
其中<servlet>中的<servlet-name>可以隨意寫,只需要保證上下兩部分相同即可。
然后是<servlet-class>是自己定義的Servlet的路徑(包含包名),最后是<url-pattern>,里面的內容也可以隨意寫,但是jsp頁面中form表單的action屬性必須與此名稱相同(action中不包含"/")
最后我們需要將web項目發布到tomcat中然后在瀏覽器輸入:http://localhost:8080/項目名稱/login.jsp就可以進行訪問并登陸了。
這只是一個簡單的應用,目的是為了幫助各位小伙伴了解jsp+servlet開發的基本流程,當然我們在實際開發的過程中會進行更為精細的分割,包括接口,實現類等。