Issue
I want to change the 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();
}
}
I tried with the above code but its giving me HTTP Status 404 error. Its not giving any error in the netbeans to correct. please tell me how to solve it.
Solution
The 404 error is generated by the web server to indicate that a resource is not found. So, probably there is nothing wrong with the code you posted. Your problem here is that the server cannot map the request to an appropriate servlet.
Solution: Make sure you have the correct mapping in the web.xml
Example code:
<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>
Answered By - MaVRoSCy