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();
    }
}

Recommended Answers

All 10 Replies

My first look at the HTML about line 14 writes "indexServlet" but the Java file name is "indexSerlvet". That could be a problem.

indexServlet ≠ indexSerlvet

The attachment is my root directory.
Capture.PNG

When I wanted to press the submit button, i want the servlet to functions, but it doesn't execute.

I wonder if you misplaced your JAVA file?

https://tomcat.apache.org/tomcat-5.5-doc/appdev/deployment.html writes:

/WEB-INF/classes/ - This directory contains any Java class files (and associated resources) required for your application, including both servlet and non-servlet classes, that are not combined into JAR files. If your classes are organized into Java packages, you must reflect this in the directory hierarchy under /WEB-INF/classes/. For example, a Java class named com.mycompany.mypackage.MyServlet would need to be stored in a file named /WEB-INF/classes/com/mycompany/mypackage/MyServlet.class.

I wonder if this is a deployment issue and not a code issue.

I am not using tomcat, but i am using glassfish 4.1 server

every body answer please

Capture.PNG
Capture1.PNG

The images are showing my root directory as well as my form action URL.
I need more advise. Thanks

Member Avatar for Zagga

I've never worked with java but don't you have to specify the file with it's extension in the form tag?

eg:
<form action="/indexServlet.java" method="POST">

I'll have to defer to those that ran that server but the quick looks I did in your first post found a naming error.
You corrected that and then it looked like a deployment error which is of course dependant on what server you run.

Leaving out details like that is your choice so my thought is you have name and deployment errors.

So any advise to resolve the problems?

@gahhon, my thought is to get into the documentation for your server choice. Also since it's Java, you have other possible server setup issues. It wasn't funny last time I looked at a non-working Java item and the server owner/person that set it up did not test that Java was working at all. For that test, back to your server docs.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.