为什么修改密码不成功?
贴出其中的代码:
1、checkPWD.jsp<body>
<div id="stuperbody">
<h4>修改密码</h4>
<font> 用户名:<%= name%></font>
<hr style="width:530px;text-align: left;"/>
<form method="post" action="servlet/CheckPWD" name="checkForm" onsubmit="return checksecond()">
<ul class="checkPWD">
<li class="wtitle"><font>新密码:</font></li>
<li class="wtitle"><input type="password" maxlength="10" id="pwd1" name="pwd"/><div id="divFirst"></div>
</li>
</ul>
<ul class="CheckPWD">
<li class="wtitle"><font>重新输入:</font></li>
<li class="wtitle"><input type="password" maxlength="10" id="pwd2" onblur="checksecond()"/><div id="divSecond"></div>
</li>
</ul>
<ul class="CheckPWD">
<li class="wtitle">
<input type="reset" value="重填"/>
<input type="submit" value="提交" >
</li>
</ul>
</form>
<script type="text/javascript">
var v = document.getElementById("divSecond");
function checksecond(){
var v1 = document.getElementById("pwd1").trim;
var v2 = document.getElementById("pwd2").trim;
if(v1 != v2){
v.innerHTML = "两次密码输入不一致!请重新输入!";
return false;
}else{
v.innerHTML = "√";
return true;
}
}
//去掉空格
String.prototype.trim=function(){
return this.replace(/(^\s*)|(\s*$)/g, '');
}
</script>
2、DataLogin.java文件//修改密码
public boolean updateLogin(String loginName,String loginpwd){
boolean flag = false;
Login login = null;
String sql = "update stu_login set loginPWD = ? where loginName = ?";
PreparedStatement psmt = null;
try {
psmt = dbc.getConn().prepareStatement(sql);
psmt.setString(1,loginpwd);
psmt.setString(2, loginName);
psmt.executeUpdate();
flag = true;
psmt.close();
} catch (Exception e) {
flag = false;
System.out.println("DataLogin更新密码updateLogin操作失败!");
e.printStackTrace();
}finally{
dbc.closeConn();
}
return flag;
}
3、CheckPWD.java文件package servlet;import java.io.IOException;
import java.io.PrintWriter;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;import dao.DataLogin;public class CheckPWD extends HttpServlet { /**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
} /**
* The doGet method of the servlet. <br>
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("gb2312");
response.setCharacterEncoding("gb2312");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
HttpSession session = request.getSession();
String loginName = (String)session.getAttribute("name");
String loginPWD = request.getParameter("pwd");
DataLogin dl = new DataLogin();
boolean flag = dl.updateLogin(loginName,loginPWD);
out.print("<script>Location.href='checkPWD.jsp';alert('密码修改成功!');</script>");
out.flush();
out.close();
} /**
* The doPost method of the servlet. <br>
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
} /**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}}
贴出其中的代码:
1、checkPWD.jsp<body>
<div id="stuperbody">
<h4>修改密码</h4>
<font> 用户名:<%= name%></font>
<hr style="width:530px;text-align: left;"/>
<form method="post" action="servlet/CheckPWD" name="checkForm" onsubmit="return checksecond()">
<ul class="checkPWD">
<li class="wtitle"><font>新密码:</font></li>
<li class="wtitle"><input type="password" maxlength="10" id="pwd1" name="pwd"/><div id="divFirst"></div>
</li>
</ul>
<ul class="CheckPWD">
<li class="wtitle"><font>重新输入:</font></li>
<li class="wtitle"><input type="password" maxlength="10" id="pwd2" onblur="checksecond()"/><div id="divSecond"></div>
</li>
</ul>
<ul class="CheckPWD">
<li class="wtitle">
<input type="reset" value="重填"/>
<input type="submit" value="提交" >
</li>
</ul>
</form>
<script type="text/javascript">
var v = document.getElementById("divSecond");
function checksecond(){
var v1 = document.getElementById("pwd1").trim;
var v2 = document.getElementById("pwd2").trim;
if(v1 != v2){
v.innerHTML = "两次密码输入不一致!请重新输入!";
return false;
}else{
v.innerHTML = "√";
return true;
}
}
//去掉空格
String.prototype.trim=function(){
return this.replace(/(^\s*)|(\s*$)/g, '');
}
</script>
2、DataLogin.java文件//修改密码
public boolean updateLogin(String loginName,String loginpwd){
boolean flag = false;
Login login = null;
String sql = "update stu_login set loginPWD = ? where loginName = ?";
PreparedStatement psmt = null;
try {
psmt = dbc.getConn().prepareStatement(sql);
psmt.setString(1,loginpwd);
psmt.setString(2, loginName);
psmt.executeUpdate();
flag = true;
psmt.close();
} catch (Exception e) {
flag = false;
System.out.println("DataLogin更新密码updateLogin操作失败!");
e.printStackTrace();
}finally{
dbc.closeConn();
}
return flag;
}
3、CheckPWD.java文件package servlet;import java.io.IOException;
import java.io.PrintWriter;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;import dao.DataLogin;public class CheckPWD extends HttpServlet { /**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
} /**
* The doGet method of the servlet. <br>
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("gb2312");
response.setCharacterEncoding("gb2312");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
HttpSession session = request.getSession();
String loginName = (String)session.getAttribute("name");
String loginPWD = request.getParameter("pwd");
DataLogin dl = new DataLogin();
boolean flag = dl.updateLogin(loginName,loginPWD);
out.print("<script>Location.href='checkPWD.jsp';alert('密码修改成功!');</script>");
out.flush();
out.close();
} /**
* The doPost method of the servlet. <br>
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
} /**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}}
第二、那个"√"只是说明2次输入的密码都是一样,不代表更新了
第三、boolean flag = dl.updateLogin(loginName,loginPWD);输出一下,看下是什么东西