今天一直遇到一个问题,JSP利用JavaScript 函数将密码进行了清空,后台查看数据的时候,没有找到数据。
分析后的原因为:JavaScript 的代码执行的过程为提交表单之前,因为 javaScript的代码执行修改了表单数据,导致后台接收不到数据,代码如下:
<%--
version 1.0
Copyright (C), 2001-2016, yeeku.H.Lee
This program is protected by copyright laws.
Program Name:
Date:
--%>
<%@ 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>
<meta name="website" content="http://www.crazyit.org" />
</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" id="pass" name="pass"/><br/>
<input type="submit" value="登录" id="submitk"/><br/>
</form>
<script type="text/javascript">
/* var clear = function(){
if(document.getElementById("pass").value != null){
//alert("清空密码");
document.getElementById("pass").value = "";
}
}
document.getElementById("submitk").onclick = clear; */
</script>
</body>
</html>
后台Servlet 代码:
package lee;
import java.io.IOException;
import java.sql.ResultSet;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* Description: <br/>
* 网站: <a href="http://www.crazyit.org">疯狂Java联盟</a> <br/>
* Copyright (C), 2001-2016, Yeeku.H.Lee <br/>
* This program is protected by copyright laws. <br/>
* Program Name: <br/>
* Date:
*
* @author Yeeku.H.Lee kongyeeku@163.com
* @version 1.0
*/
// @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");
System.out.println("前台传递的密码:" + pass);
try {
// Servlet本身并不执行任何的业务逻辑处理,它调用JavaBean处理用户请求
DbDao dd = new DbDao("com.mysql.jdbc.Driver",
"jdbc:mysql://localhost:3306/liuyan", "root", "123456");
// 查询结果集
ResultSet rs = dd.query("select pass from user_inf"
+ " where name = ?", username);
if (rs.next()) {
// 用户名和密码匹配
System.out.println("数据库中的密码:" + rs.getString("pass"));
// System.out.println(username + "---ssss");
// System.out.println(pass + "---ssss");
if (rs.getString("pass").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);
}
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
String errMsg = "";
// Servlet本身并不输出响应到客户端,因此必须将请求转发到视图页面
RequestDispatcher rd;
// 获取请求参数
String username = req.getParameter("username");
String pass = req.getParameter("pass");
try {
// Servlet本身并不执行任何的业务逻辑处理,它调用JavaBean处理用户请求
DbDao dd = new DbDao("com.mysql.jdbc.Driver",
"jdbc:mysql://localhost:3306/liuyan", "root", "123456");
// 查询结果集
ResultSet rs = dd.query("select pass from user_inf"
+ " where name = ?", username);
if (rs.next()) {
// 用户名和密码匹配
if (rs.getString("pass").equals(pass)) {
// 获取session对象
HttpSession session = req.getSession(true);
// 设置session属性,跟踪用户会话状态
session.setAttribute("name", username);
// 获取转发对象
rd = req.getRequestDispatcher("/welcome.jsp");
// 转发请求
rd.forward(req, resp);
} else {
// 用户名和密码不匹配时
errMsg += "您的用户名密码不符合,请重新输入";
}
} else {
// 用户名不存在时
errMsg += "您的用户名不存在,请先注册";
}
} catch (Exception e) {
e.printStackTrace();
}
// 如果出错,转发到重新登录
if (errMsg != null && !errMsg.equals("")) {
rd = req.getRequestDispatcher("/login.jsp");
req.setAttribute("err", errMsg);
rd.forward(req, resp);
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doGet(req, resp);
}
}
DAO代码
package lee;
import java.sql.*;
/**
* Description:
* <br/>网站: <a href="http://www.crazyit.org">疯狂Java联盟</a>
* <br/>Copyright (C), 2001-2016, Yeeku.H.Lee
* <br/>This program is protected by copyright laws.
* <br/>Program Name:
* <br/>Date:
* @author Yeeku.H.Lee kongyeeku@163.com
* @version 1.0
*/
public class DbDao
{
private Connection conn;
private String driver;
private String url;
private String username;
private String pass;
public DbDao()
{
}
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();
}
}
}