JSP+Servlet + JDBC 实现简单的登录验证模块

数据库设计+编码+运行调试

 

数据库准备:

二话不说,上图

JSP+Servlet + JDBC 实现简单的登录验证模块_第1张图片

文件组织如下:

JSP+Servlet + JDBC 实现简单的登录验证模块_第2张图片

 

 

 

首先写出三个JSP页面文件

login.jsp

<%@ page language="java" contentType="text/html;charset=gb2312"
pageEncoding="gb2312"%>




登陆页面


登陆界面



用户名

  
密码 





back.jsp

<%@ page language="java" contentType="text/html;charset=gb2312"
pageEncoding="gb2312"%>




登陆失败


用户密码错误,单击
这里
返回

welcome.jsp

<%@ page import ="java.util.*"contentType="text/html; charset=gb2312"
pageEncoding="gb2312"%>




登陆成功



欢迎你!登陆成功!

<%
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]);
%>



接着,写一个数据库处理类以及一个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配置文件如下:

"1.0"encoding="UTF-8"?>

"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">

LoginServlet

servlet.LoginServlet

 

LoginServlet

/login

login.jsp

 

 

 


 

实验注意要点
JSP+Servlet + JDBC 实现简单的登录验证模块_第3张图片

 

 

程序运行结果如下:

JSP+Servlet + JDBC 实现简单的登录验证模块_第4张图片

JSP+Servlet + JDBC 实现简单的登录验证模块_第5张图片

JSP+Servlet + JDBC 实现简单的登录验证模块_第6张图片


你可能感兴趣的:(js与前端)