下面是应用的登录页面
login.jsp 代码:
<%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> 用户登录 </title>
</head>
<body>
<!-- 输出出错提示 -->
<span style="color:red;font-weight:bold">
<%
if (request.getAttribute("err") != null)
{
out.println(request.getAttribute("err") + "<br/>");
}
%>
</span>
请输入用户名和密码:
<!-- 登录表单,该表单提交到一个Servlet -->
<form id="login" method="post" action="login">
用户名:<input type="text" name="username"/><br/>
密  码:<input type="password" name="pass"/><br/>
<input type="submit" value="登录"/><br/>
</form>
</body>
</html>
注意的是:<span>节点有个EL表达式;
控制器Servlet 代码:
package com.yd.servlet;
import java.sql.ResultSet;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.yd.dao.DBDao;
@WebServlet(name = "login", urlPatterns = { "/login" })
public class LoginServlet extends HttpServlet {
// 响应客户端请求的方法
public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
String errMsg = "";
// Servlet本身并不输出响应到客户端,因此必须将请求转发到视图页面
RequestDispatcher rd;
// 获取请求参数
String username = request.getParameter("username");
String pass = request.getParameter("pass");
try {
// Servlet本身并不执行任何的业务逻辑处理,它调用JavaBean处理用户请求
DBDao dd = new DBDao("com.mysql.jdbc.Driver",
"jdbc:mysql://localhost:3306/test", "root", "123456");
// 查询结果集
ResultSet rs = dd.query("select * from t_user"
+ " where username = ?", username);
if (rs.next()) {
// 用户名和密码匹配
if (rs.getString("password").equals(pass)) {
// 获取session对象
HttpSession session = request.getSession(true);
// 设置session属性,跟踪用户会话状态
session.setAttribute("name", username);
// 获取转发对象
rd = request.getRequestDispatcher("/welcome.jsp");
// 转发请求
rd.forward(request, response);
} else {
// 用户名和密码不匹配时
errMsg += "您的用户名密码不符合,请重新输入";
}
} else {
// 用户名不存在时
errMsg += "您的用户名不存在,请先注册";
}
} catch (Exception e) {
e.printStackTrace();
}
// 如果出错,转发到重新登录
if (errMsg != null && !errMsg.equals("")) {
rd = request.getRequestDispatcher("/login.jsp");
request.setAttribute("err", errMsg);
rd.forward(request, response);
}
}
}
相当于JavaBean 的DAO层 代码:
package com.yd.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class DBDao {
private Connection conn;
private String driver;
private String url;
private String username;
private String pass;
public DBDao(String driver, String url, String username, String pass) {
this.driver = driver;
this.url = url;
this.username = username;
this.pass = pass;
}
// 下面是各个成员属性的setter和getter方法
public void setDriver(String driver) {
this.driver = driver;
}
public void setUrl(String url) {
this.url = url;
}
public void setUsername(String username) {
this.username = username;
}
public void setPass(String pass) {
this.pass = pass;
}
public String getDriver() {
return (this.driver);
}
public String getUrl() {
return (this.url);
}
public String getUsername() {
return (this.username);
}
public String getPass() {
return (this.pass);
}
// 获取数据库连接
public Connection getConnection() throws Exception {
if (conn == null) {
Class.forName(this.driver);
conn = DriverManager.getConnection(url, username, this.pass);
}
return conn;
}
// 插入记录
public boolean insert(String sql, Object... args) throws Exception {
PreparedStatement pstmt = getConnection().prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
pstmt.setObject(i + 1, args[i]);
}
if (pstmt.executeUpdate() != 1) {
return false;
}
pstmt.close();
return true;
}
// 执行查询
public ResultSet query(String sql, Object... args) throws Exception {
PreparedStatement pstmt = getConnection().prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
pstmt.setObject(i + 1, args[i]);
}
return pstmt.executeQuery();
}
// 执行修改
public void modify(String sql, Object... args) throws Exception {
PreparedStatement pstmt = getConnection().prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
pstmt.setObject(i + 1, args[i]);
}
pstmt.executeUpdate();
pstmt.close();
}
// 关闭数据库连接的方法
public void closeConn() throws Exception {
if (conn != null && !conn.isClosed()) {
conn.close();
}
}
}
使用JSP/Servlet 一种简单的MVC模式,以上是简单的登录验证模式;
非常期待各位的指导!