JavaWeb登陸功能實(shí)現(xiàn)代碼
本文實(shí)例為大家分享了JavaWeb登陸功能的方法,供大家參考,具體內(nèi)容如下
首先我們要JavaWeb登陸的基本流程:JSP頁面發(fā)送請(qǐng)求——>Servlet——>Servlet通過調(diào)用方法從數(shù)據(jù)庫中得到數(shù)據(jù)并將結(jié)果返回頁面。
我們先建立三個(gè)jsp頁面,包括login.jsp(登陸頁面)、index.jsp(顯示登陸成功后的信息)、error.jsp(登錄失敗的頁面),其中后兩個(gè)頁面的內(nèi)容可以隨意寫,而login.jsp頁面的主要內(nèi)容如下:
<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"(同時(shí)不要忘記設(shè)置開發(fā)工具的編碼格式,不然jsp頁面會(huì)顯示亂碼)
根據(jù)用戶名和密碼兩個(gè)屬性我們建立相應(yīng)的實(shí)體類,并添加get和set方法,代碼如下:
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”是指將請(qǐng)求發(fā)送到Servlet處理。接下來我們轉(zhuǎn)到Servlet來進(jìn)行處理:
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;
//創(chuàng)建時(shí)為Servlet而不是Class,需要在web.xml中進(jìn)行配置,配置的代碼Myeclipse將自動(dòng)生成
public class LoginServlet extends HttpServlet {
//創(chuàng)建UserDao的對(duì)象,以便于查詢數(shù)據(jù)庫
UserDao userDao=new UserDao();
//以下doGet方法和doPost方法分別對(duì)應(yīng)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方法獲取到前臺(tái)文本框中輸入的值,其中括號(hào)內(nèi)的內(nèi)容為<input/>標(biāo)簽中的name屬性
String userName=request.getParameter("userName");
String password=request.getParameter("password");
//調(diào)用UserDao中的getSelect方法并獲取到返回值
boolean flag=userDao.getSelect(userName, password);
//若用戶名和密碼存在則轉(zhuǎn)發(fā)到index.jsp頁面,否則重定向到error.jsp頁面
if (flag) {
request.getRequestDispatcher("index.jsp").forward(request, response);
}
else
response.sendRedirect("error.jsp");
}
}
注釋 中已經(jīng)說的很明白了,就不再重復(fù)了,可以看看第26行和29行,其中26行是轉(zhuǎn)發(fā),29行是重定向,感興趣的小伙伴可以查查兩者的區(qū)別。剩下的一部分就是我們之前提到過的關(guān)于數(shù)據(jù)庫的查詢操作了,我們?cè)?3行進(jìn)行了調(diào)用,下面我們完成調(diào)用的方法:
package com.test.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class UserDao {
//連接數(shù)據(jù)庫的代碼
public Connection getCon() {
//數(shù)據(jù)庫連接名稱
String username="root";
//數(shù)據(jù)庫連接密碼
String password="";
String driver="com.mysql.jdbc.Driver";
//其中test為數(shù)據(jù)庫名稱
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;
}
//進(jìn)行查詢的方法,若含有滿足條件的數(shù)據(jù)則返回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;
}
}
在這個(gè)方法中我們首先連接數(shù)據(jù)庫,然后在查詢的方法中傳入從jsp頁面獲取到的userName和password,判斷數(shù)據(jù)庫中是否存在此用戶名和密碼的用戶,如果存在則返回true,否則返回false(不要忘記導(dǎo)入數(shù)據(jù)庫鏈接的包)。
至于數(shù)據(jù)庫中的字段則參照實(shí)體類User建立即可,即包含userName和password兩個(gè)屬性,如果數(shù)據(jù)庫鏈接還有問題的請(qǐng)參照之前的關(guān)于數(shù)據(jù)庫部分的隨筆。
最后看一下web.xml中的配置:
<?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>,里面的內(nèi)容也可以隨意寫,但是jsp頁面中form表單的action屬性必須與此名稱相同(action中不包含"/")
最后我們需要將web項(xiàng)目發(fā)布到tomcat中然后在瀏覽器輸入:http://localhost:8080/項(xiàng)目名稱/login.jsp就可以進(jìn)行訪問并登陸了。
這只是一個(gè)簡單的應(yīng)用,目的是為了幫助各位小伙伴了解jsp+servlet開發(fā)的基本流程,當(dāng)然我們?cè)趯?shí)際開發(fā)的過程中會(huì)進(jìn)行更為精細(xì)的分割,包括接口,實(shí)現(xiàn)類等。
相關(guān)文章
Spring實(shí)現(xiàn)IoC的多種方式小結(jié)
本篇文章主要介紹了Spring實(shí)現(xiàn)IoC的多種方式小結(jié),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02
Java實(shí)現(xiàn)字符串倒序輸出的常用方法小結(jié)
這篇文章主要介紹了Java實(shí)現(xiàn)字符串倒序輸出的常用方法,通過三個(gè)實(shí)例從不同角度實(shí)現(xiàn)該功能,有不錯(cuò)的借鑒價(jià)值,需要的朋友可以參考下2014-09-09
SpringBoot3整合SpringDoc實(shí)現(xiàn)在線接口文檔的詳細(xì)過程
這篇文章主要介紹了SpringBoot3整合SpringDoc實(shí)現(xiàn)在線接口文檔的詳細(xì)過程,本文通過示例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-06-06
mybatis配置文件簡介_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了mybatis配置文件簡介的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09
SpringMVC+EasyUI實(shí)現(xiàn)頁面左側(cè)導(dǎo)航菜單功能
這篇文章主要介紹了SpringMVC+EasyUI實(shí)現(xiàn)頁面左側(cè)導(dǎo)航菜單功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-09-09
Java C++題解leetcode 1684統(tǒng)計(jì)一致字符串的數(shù)目示例
這篇文章主要為大家介紹了Java C++題解leetcode 1684統(tǒng)計(jì)一致字符串的數(shù)目示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
BufferedReader中read()方法和readLine()方法的使用
這篇文章主要介紹了BufferedReader中read()方法和readLine()方法的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04

