1、checkPWD.JSP
<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<%@ page import="bean.Login"%>
<jsp:useBean id="lo" class="dao.DataLogin" scope="page"></jsp:useBean>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>修改密码</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<link rel="stylesheet" type="text/css" href="css/home.css">
<link rel="stylesheet" type="text/css" href="css/common.css"> </head>
<!--如果用户没有登录,则转到index.jsp -->
<%
String name =(String) session.getAttribute("loginname");
if(name == null ){
%>
<script type="text/javascript">
location.href="index.jsp";
</script>
<%
}
%>
<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>
</div>
</body>
</html>
2.CheckPWDServlet.javapackage 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 ChangePWDServlet extends HttpServlet { /**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy();
} /**
* The doGet method of the servlet. <br>
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
} /**
* The doPost method of the servlet. <br>
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setContentType("text/html");
PrintWriter out = response.getWriter();
HttpSession session = request.getSession();
String name =(String)session.getAttribute("name");
String pwd = (String)session.getAttribute("pwd");
DataLogin dl = new DataLogin();
boolean flag = dl.updateLogin(name,pwd);
if(flag){
out.print("<script>alert('修改密码成功!');location.href='checkPWD.jsp'</script?");
}else{
out.print("<script>alert('修改密码失败!');location.href='checkPWD.jsp'</script?");
}
out.flush();
out.close();
} /**
* Initialization of the servlet. <br>
*/
public void init() throws ServletException {
}}
3.checkPWD.javapackage 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
}}
4.DatacheckPWD.java
package dao;import java.sql.PreparedStatement;
import java.sql.ResultSet;import org.DataBaseConn;import bean.Login;public class DataLogin { DataBaseConn dbc = new DataBaseConn();
//根据用户名检查用户是否存在
public boolean stuJudge(String loginName){
boolean flag = true;
String sql = "select * from stu_login where loginName = ?";
PreparedStatement psmt = null;
try {
psmt = dbc.getConn().prepareStatement(sql);
psmt.setString(1,loginName);
ResultSet rs = psmt.executeQuery();
if(rs.next() && rs != null){
flag = true;
}else{
flag = false;
}
psmt.close();
} catch (Exception e) {
e.printStackTrace();
}finally{
dbc.closeConn();
}
return flag;
}
//检查登录用户名和密码是否正确
public boolean loginJudge(String loginName,String loginPWD,int role ){
boolean flag = false;
String sql = "select * from stu_login where roleID =? and loginName=? and loginPWD=? ";
PreparedStatement psmt = null;
try {
psmt = dbc.getConn().prepareStatement(sql);
psmt.setInt(1,role);
psmt.setString(2,loginName);
psmt.setString(3,loginPWD );
ResultSet rs = psmt.executeQuery();
if(rs.next() && rs != null){
flag = true;
}
psmt.close();
} catch (Exception e) {
e.printStackTrace();
}finally{
dbc.closeConn();
}
return flag;
}
//修改密码
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;
}
//添加记录
public boolean addPerson(int role,String lname){
boolean flag = false;
String sql = "insert into stu_login values(null,?,?,'123456')";
PreparedStatement psmt = null;
try {
psmt = dbc.getConn().prepareStatement(sql);
psmt.setInt(1,role);
psmt.setString(2,lname);
psmt.executeUpdate();
flag = true;
psmt.close();
} catch (Exception e) {
flag = false;
System.out.println("DataLogin添加登录信息记录操作失败!");
e.printStackTrace();
}finally{
dbc.closeConn();
}
return flag;
}
<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<%@ page import="bean.Login"%>
<jsp:useBean id="lo" class="dao.DataLogin" scope="page"></jsp:useBean>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>修改密码</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<link rel="stylesheet" type="text/css" href="css/home.css">
<link rel="stylesheet" type="text/css" href="css/common.css"> </head>
<!--如果用户没有登录,则转到index.jsp -->
<%
String name =(String) session.getAttribute("loginname");
if(name == null ){
%>
<script type="text/javascript">
location.href="index.jsp";
</script>
<%
}
%>
<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>
</div>
</body>
</html>
2.CheckPWDServlet.javapackage 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 ChangePWDServlet extends HttpServlet { /**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy();
} /**
* The doGet method of the servlet. <br>
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
} /**
* The doPost method of the servlet. <br>
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { response.setContentType("text/html");
PrintWriter out = response.getWriter();
HttpSession session = request.getSession();
String name =(String)session.getAttribute("name");
String pwd = (String)session.getAttribute("pwd");
DataLogin dl = new DataLogin();
boolean flag = dl.updateLogin(name,pwd);
if(flag){
out.print("<script>alert('修改密码成功!');location.href='checkPWD.jsp'</script?");
}else{
out.print("<script>alert('修改密码失败!');location.href='checkPWD.jsp'</script?");
}
out.flush();
out.close();
} /**
* Initialization of the servlet. <br>
*/
public void init() throws ServletException {
}}
3.checkPWD.javapackage 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
}}
4.DatacheckPWD.java
package dao;import java.sql.PreparedStatement;
import java.sql.ResultSet;import org.DataBaseConn;import bean.Login;public class DataLogin { DataBaseConn dbc = new DataBaseConn();
//根据用户名检查用户是否存在
public boolean stuJudge(String loginName){
boolean flag = true;
String sql = "select * from stu_login where loginName = ?";
PreparedStatement psmt = null;
try {
psmt = dbc.getConn().prepareStatement(sql);
psmt.setString(1,loginName);
ResultSet rs = psmt.executeQuery();
if(rs.next() && rs != null){
flag = true;
}else{
flag = false;
}
psmt.close();
} catch (Exception e) {
e.printStackTrace();
}finally{
dbc.closeConn();
}
return flag;
}
//检查登录用户名和密码是否正确
public boolean loginJudge(String loginName,String loginPWD,int role ){
boolean flag = false;
String sql = "select * from stu_login where roleID =? and loginName=? and loginPWD=? ";
PreparedStatement psmt = null;
try {
psmt = dbc.getConn().prepareStatement(sql);
psmt.setInt(1,role);
psmt.setString(2,loginName);
psmt.setString(3,loginPWD );
ResultSet rs = psmt.executeQuery();
if(rs.next() && rs != null){
flag = true;
}
psmt.close();
} catch (Exception e) {
e.printStackTrace();
}finally{
dbc.closeConn();
}
return flag;
}
//修改密码
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;
}
//添加记录
public boolean addPerson(int role,String lname){
boolean flag = false;
String sql = "insert into stu_login values(null,?,?,'123456')";
PreparedStatement psmt = null;
try {
psmt = dbc.getConn().prepareStatement(sql);
psmt.setInt(1,role);
psmt.setString(2,lname);
psmt.executeUpdate();
flag = true;
psmt.close();
} catch (Exception e) {
flag = false;
System.out.println("DataLogin添加登录信息记录操作失败!");
e.printStackTrace();
}finally{
dbc.closeConn();
}
return flag;
}
var v2 = document.getElementById("pwd2").trim;少了点value
var v1 = document.getElementById("pwd1").value.trim;
var v2 = document.getElementById("pwd2").value.trim;
String loginPWD = request.getParameter("pwd");
这里应该是String loginPWD = request.getParameter("pwd1");
这个有错吗?//修改密码
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;
}
request.getParameter("参数名");来获得.
4楼,session里取参数不是用getParameter()把,是用getAttribute()的.
String loginName = (String)session.getAttribute("name");
String loginPWD = request.getParameter("pwd");
但是我重新编译后还是原来的样子.比如:out.print("<font style='font-size:12px;color:red'>该课程名称已经存在!</font>");修改为:out.print("<font style='font-size:12px;color:red'>该课!</font>");还是显示"该课程名称已经存在!"这几个字.
是否与在项目中lib有几个jar文件关?
var v2 = document.getElementById("pwd2").value;
jsp页面改成了上面的样子后,checksecond()能正常显示提示!
这里由于楼上讲的原因还不会改!我可以用javac - d . *.java编译,并部署好.为什么还是原来的样子?
out.print("<script>alert('修改密码成功!');location.href='checkPWD.jsp'</script?");
}else{out.print("<script>alert('修改密码失败!');location.href='checkPWD.jsp'</script?");
}
这里的String pwd = (String)session.getAttribute("pwd");应该是取参数吧?String pwd = (String)session.getParameter("pwd");