Hello guys,
Currently I am developing a simple task of my University exercise. Is using EJB to develop the webpages to allowed end-users to reset their password based on the records in database.
Unfortunately, I have some issue during the development, which is whenver I press the "Submit" button, it redirecting me to "Page Not Found" error. I have been research all over the source in the internet, but I am still can't find the right answer.
index.html
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<title>MYUSER - Password Reset</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="/indexServlet" method="POST">
<div style="font-size: 20px; font-weight: bold">Password Reset</div>
<br><br>
<div id="divInformation">
<table>
<tr>
<td>User ID </td>
<td>: <input type="text" name="userid" /></td>
</tr>
<tr>
<td>Security Answer </td>
<td>: <input type="text" name="secans" /></td>
</tr>
<tr>
<td><input type="submit" name="btnSubmit" value="Submit" /></td>
</tr>
</table>
</div>
</form>
</body>
</html>
indexSerlvet.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
import java.sql.Statement;
import java.util.Random;
/**
*
* @author Jack Wong
*/
@WebServlet(urlPatterns = {"/indexServlet"})
public class indexServlet extends HttpServlet {
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet indexServlet</title>");
out.println("</head>");
out.println("<body>");
String strID = request.getParameter("userid");
String strAns = request.getParameter("secans");
String newPass = randomString();
try {
String statement = "SELECT * FROM MYUSER WHERE USERID = '" + strID + "' AND SECANS = '" + strAns + "'";
String host = "jdbc:derby://localhost:1527/Week1";
String uName = null, uPass = null;
try (Connection con = DriverManager.getConnection(host, uName, uPass)) {
Statement stmt = con.createStatement();
stmt.execute(statement);
}
} catch (SQLException e) {
System.out.println(e.getMessage());
} finally {
try {
String statement = "UPDATE MYUSER SET PASSWORD='" + newPass + "' WHERE USERID='" + strID + "'";
String host = "jdbc:derby://localhost:1527/Week1";
String uName = null, uPass = null;
try (Connection con = DriverManager.getConnection(host, uName, uPass)) {
Statement stmt = con.createStatement();
stmt.executeUpdate(statement);
}
} catch (SQLException e) {
System.out.println(e.getMessage());
} finally {
out.println("USER ID : " + strID + ", PASSWORD: " + newPass);
}
}
out.println("</body>");
out.println("</html>");
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
public String randomString() {
String subset = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMOPQRSTUVWXYZ";
Random r = new Random();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 6; i++) {
int index = r.nextInt(subset.length());
char c = subset.charAt(index);
sb.append(c);
}
return sb.toString();
}
}