您的位置:首页 > Web前端 > JavaScript

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

2016-08-04 18:03 691 查看
数据库设计+编码+运行调试

 

数据库准备:

二话不说,上图





文件组织如下:





 

 

 

首先写出三个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>
 

 

 

 

实验注意要点




 

 

程序运行结果如下:











内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: