尝试使用jsp servlet和dao更改mysql中的旧密码

时间:2015-11-10 09:30:15

标签: java mysql jsp servlets change-password

我想更改密码 changePassword.jsp:

<form name="ChangePasswordForm" method="post" action="ChangePasswordServlet">

<table id="t01" >
    <tbody class="ui-widget-content">
        <tr>
            <td>Employee ID</td>
            <td><input type="text" name="empid" value="<%=session.getAttribute("name")%>" readonly="readonly" required="required"/></td>
        </tr>
        <tr>
        <td>Old Password</td>
        <td><input name="OldPassword" type="password" id="OLDpwd" required="required"></td>
        </tr>
        <tr>
        <td>NewPassword</td>
        <td><input name="newpassword" type="password" id="newpassword" required="required">
        </td>
        </tr>
        <tr>
        <td>Confirm Password</td>
        <td><input name="conpassword" type="password" id="conpassword" required="required">
        </td>
        </tr>
        <tr>
    </tbody>
</table><br>

        <input type="submit" name="Submit" class="myButton"  value="Save">
        <input type="reset" class="myButton" value="Reset" />
        <a href="logout.jsp" class="myButton">Logout</a>

</form>

</body>

PasswordDao.java:

public class PasswordDao {

public static int Update(PasswordBean pb,String sql) throws Exception {

    int i=0;  
    PreparedStatement ps=null;
    Connection conn=ConnectionProvider.getConn();  
    try{ 
        ps = conn.prepareStatement(sql);

        ps.setString(1,pb.getNewpassword());
        ps.setString(2,pb.getEmpid());
        ps.setString(3,pb.getOldPassword());
        i=ps.executeUpdate();  
    }
    catch(Exception e){
        System.out.println(e);
    }
    finally { 
        if (conn != null) {  
            try {  
                conn.close();  
            } catch (SQLException e) {  
                e.printStackTrace();  
            }  
        }
        if (ps != null) {  
            try {  
                ps.close();  
            } catch (SQLException e) {  
                e.printStackTrace();  
            }  
        }  

    }  
    return i;  

}
}

ChangePasswordServlet.java

public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
  response.setContentType("text/html;charset=UTF-8");
  PrintWriter out = response.getWriter();

  PasswordBean pb = new PasswordBean();
  String Emp_id=request.getParameter("empid");    
  pb.setEmpid(Emp_id);
  String Oldpwd=request.getParameter("OldPassword");   pb.setOldPassword(Oldpwd);
  String Newpwd=request.getParameter("newpassword"); pb.setNewpassword(Newpwd);

   HttpSession session = request.getSession(false); 

   try{if (Emp_id!=null){
        session.setAttribute("Emp_id",Emp_id);}
    }catch(Exception e){e.printStackTrace();}
    try{if (Oldpwd!=null){
        session.setAttribute("Oldpwd",Oldpwd);}
    }catch(Exception e){e.printStackTrace();}
    try{if (Newpwd!=null){
        session.setAttribute("Newpwd",Newpwd);}
    }catch(Exception e){e.printStackTrace();}

   PasswordDao dao = new PasswordDao();
    String sql= "Update employee set Pwd= ? WHERE Emp_id=? and Pwd=?";

     try {

       int status = PasswordDao.Update(pb, sql);

    if(status!=0){
        out.print("<p style=\"color:Green\">Password Changed Successfully!!</p>");  
        RequestDispatcher rd=request.getRequestDispatcher("/changePassword.jsp");    
        rd.include(request,response);
    }
   else{    
        out.print("<p style=\"color:red\">**Password doesnot Change.. Try Again! **</p>");    
        RequestDispatcher rd=request.getRequestDispatcher("/changePassword.jsp");    
        rd.include(request,response);
    }

     }
   catch(Exception e){
       e.printStackTrace();
   }
} 

我尝试使用上面的代码,但它给了我HTTP状态404错误。它没有给netbeans中的任何错误纠正。请告诉我如何解决它。

1 个答案:

答案 0 :(得分:1)

Web服务器生成404错误,指示未找到资源。所以,你发布的代码可能没有任何问题。您的问题是服务器无法将请求映射到适当的servlet。

解决方案:确保您在web.xml中具有正确的映射

示例代码:

<servlet>
  <servlet-name>ChangePasswordServlet</servlet-name>
  <servlet-path>foo.ChangePasswordServlet</servlet-path>
</servlet>
<servlet-mapping>
  <servlet-name>ChangePasswordServlet</servlet-name>
  <url-pattern>/ChangePasswordServlet</url-pattern>
</servlet-mapping>