javascript
JSP+Servlet + JDBC 实现简单的登录验证模块
數據庫設計+編碼+運行調試
?
數據庫準備:
二話不說,上圖
文件組織如下:
?
?
?
首先寫出三個JSP頁面文件
login.jsp
<%@ page language="java" contentType="text/html;charset=gb2312"
pageEncoding="gb2312"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=gb2312">
<title>登陸頁面</title>
</head>
<body>
<center> 登陸界面</center>
<center>
<form action="login"method="post">
用戶名 <input type="text" name = "username"/><br><br>
密碼 <input type="text" name ="password"> <br>
<input type = "submit" value ="提交">
</form>
</center>
</body>
</html>
back.jsp
<%@ page language="java" contentType="text/html;charset=gb2312"
pageEncoding="gb2312"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=gb2312">
<title>登陸失敗</title>
</head>
<body>
用戶密碼錯誤,單擊
<ahref="login.jsp">這里</a>
返回
</body>
</html>
welcome.jsp
<%@ page import ="java.util.*"contentType="text/html; charset=gb2312"
pageEncoding="gb2312"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=gb2312">
<title>登陸成功</title>
</head>
<body>
歡迎你!登陸成功!<br>
<%
Date today = new Date();
int d = today.getDay();
int h = today.getHours();
String s = "";
if(h>0 && h< 12)
s = "上午好";
else if(h>=12)
s = "下午好";
String day[] = {"日","一","二","三","四","五","六"};
out.println(s+ ",今天是星期" + day[d]);
%>
</body>
</html>
接著,寫一個數據庫處理類以及一個servlet類
DBUtil.java
package javabean;
import java.sql.*;
public class DBUtil {
boolean bInited = false;
//加載驅動
public void initJDBC() throws ClassNotFoundException {
//加載MYSQL JDBC驅動程序
Class.forName("com.mysql.jdbc.Driver");
bInited = true;
System.out.println("Success loading Mysql Driver!");
}
public Connection getConnection() throwsClassNotFoundException,
SQLException{
if(!bInited){
initJDBC();
}
//連接URL為 jdbc:mysql//服務器地址/數據庫名
//后面的2個參數分別是登陸用戶名和密碼
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/user","root","123");
return conn;
}
public boolean loginSuccess(String userName,String password){
boolean returnValue = false;
String sql = "SELECT * FROM user";
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try{
conn = getConnection();
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
while(rs.next()){
String userNameInDB = rs.getString("name");
String passwordInDB = rs.getString("pwd");
if(userNameInDB.equals(userName)&&
passwordInDB.equals(password)){
returnValue = true;
break;
}
}
}catch (ClassNotFoundException e) {
e.printStackTrace();
}catch (SQLException e) {
e.printStackTrace();
}
return returnValue;
}
}
LoginServlet.java
package servlet;
import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javabean.DBUtil;
public class LoginServlet implements javax.servlet.Servlet{
public void destroy() {
}
public ServletConfig getServletConfig() {
return null;
}
public String getServletInfo() {
return null;
}
public void init(ServletConfig arg0) throws ServletException{
}
public void doPost(HttpServletRequestrequest,HttpServletResponse response)
throws ServletException,IOException{
String userName = request.getParameter("username");//取得用戶名
String password = request.getParameter("password");//取得密碼
DBUtil db = new DBUtil();//構建數據庫對象
boolean canLogin = db.loginSuccess(userName, password);
if(canLogin){//根據登陸情況,跳轉頁面
response.sendRedirect("welcome.jsp");
}else{
response.sendRedirect("back.jsp");
}
}
public void service(ServletRequest request, ServletResponseresponse)
throws ServletException, IOException {
HttpServletRequest rq = (HttpServletRequest)request;
HttpServletResponse rs = (HttpServletResponse) response;
doPost(rq,rs);
}
}
最后,web配置文件如下:
<?xmlversion="1.0"encoding="UTF-8"?>
<web-appversion="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>servlet.LoginServlet</servlet-class>
</servlet>
?
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>
?
?
?
?
實驗注意要點
?
?
程序運行結果如下:
總結
以上是生活随笔為你收集整理的JSP+Servlet + JDBC 实现简单的登录验证模块的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [转载]关于request和sessio
- 下一篇: JSP怎么将表单提交到对应的servle