0

I've come to across this issue and spent some time searching here for similar problems, but most cases are related to upper/lower-case mistakes. The thing is, on my computer at home it works just fine, but now that I've imported it on my laptop it just wont function right.

I get HTTP Status 404 - /Web/RegisterServlet.do when I try to run a servlet from my jsp file.

My Jsp:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>Dynamic Example</title>
</head>
<body>
    <form action="RegisterServlet.do" method="post">

        <table>
            <tr>
                <td align="right">Username:</td>
                <td align="left"><input type="text" name="username" /></td>
            </tr>
            <tr>
                <td align="right">Real name:</td>
                <td align="left"><input type="text" name="realname" /></td>
            </tr>
            <tr>
                <td align="right">Password:</td>
                <td align="left"><input type="text" name="password1" /></td>
            </tr>
            <tr>
                <td align="right">Password (again):</td>
                <td align="left"><input type="text" name="password2" /></td>
            </tr>
            <tr>
                <td align="right">Email:</td>
                <td align="left"><input type="text" name="email1" /></td>
            </tr>
            <tr>
                <td align="right">Email (again):</td>
                <td align="left"><input type="text" name="email2" /></td>
            </tr>

            <tr>
                <td align="right">Address</td>
                <td align="left"><input type="text" name="address" /></td>
            </tr>
            <tr>
                <td align="right">Country:</td>
                <td align="left"><input type="text" name="country" /></td>
            </tr>
            <tr>
                <td align="right"><input type="submit" value="Register" /></td>
            </tr>

        </table>

    </form>
</body>
</html>

My servlet:

package web;

import java.io.BufferedWriter;
import java.io.FileWriter;
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;

public class RegisterServlet extends HttpServlet {

    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        boolean good = true;
        String username = req.getParameter("username");
        String realname = req.getParameter("realname");
        String password1 = req.getParameter("password1");
        String password2 = req.getParameter("password2");
        String email1 = req.getParameter("email1");
        String email2 = req.getParameter("email2");
        String address = req.getParameter("address");
        String country = req.getParameter("country");

        if (username == "" || realname == "" || password1 == ""
                || password2 == "" || email1 == "" || email2 == ""
                || address == "" || country == "") {
            good = false;
        }

        if (!password1.equals(password2)) {
            good = false;
        }

        if (!email1.equals(email2)) {
            good = false;
        }

        if (good == false) {
            resp.sendRedirect("Register.jsp");
        } else {

            FileWriter fw = new FileWriter("users.txt", true);

            PrintWriter pw = new PrintWriter(fw);
            pw.println("[" + username + "][" + realname + "][" + password1
                    + "][" + email1 + "][" + address + "][" + country + "]");
            pw.close();

            req.getRequestDispatcher("Login.jsp").forward(req, resp);
        }

    }
}

My xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">
    <servlet>
        <servlet-name>Dynamic Example Servlet</servlet-name>
        <servlet-class>web.LoginServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Dynamic Example Servlet</servlet-name>
        <url-pattern>/LoginServlet.do</url-pattern>
    </servlet-mapping>
    <servlet>
        <servlet-name>Dynamic Example Servlets</servlet-name>
        <servlet-class>web.RegisterServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Dynamic Example Servlets</servlet-name>
        <url-pattern>/RegisterServlet.do</url-pattern>
    </servlet-mapping>
</web-app>

My file setup: https://i.stack.imgur.com/U3Qg5.png

Dmitry Ginzburg
  • 7,391
  • 2
  • 37
  • 48
Jordy
  • 3
  • 1
  • 3
  • Please say what URL you use to display the JSP page first in your computer at home, next in your laptop - and what are the OS and servlet containers – Serge Ballesta May 28 '15 at 09:06

2 Answers2

1

In your RegisterServlet

req.getRequestDispatcher("WEB-INF/Login.jsp").forward(req, resp);

Change the location to WEB-INF/Login.jsp

Add a context path to your action

<form action="${pageContext.request.contextPath}/RegisterServlet.do" method="post">

Context path computes the full path of your resource, so here ${pageContext.request.contextPath} will be http://localhost:8080/AppName or whatever your domain & port number is.

This is an excellent resource on relative paths.

underdog
  • 4,447
  • 9
  • 44
  • 89
  • Thank you for your quick reply! I tried this, but there is no change. http://localhost:8080/Web/RegisterServlet.do Still gives a 404 – Jordy May 28 '15 at 08:44
  • What is your application name or name of the war file? I've updated my answer please retry. – underdog May 28 '15 at 09:04
  • Thanks, tried this, still no changes. I use Eclipse, I posted a link to an image of my file tree if that is what you mean. http://i.stack.imgur.com/U3Qg5.png Perhaps it's an issue with my client? – Jordy May 28 '15 at 09:10
  • Is your loginServlet.do flow working fine? Also just try removing the / in your /RegisterServlet.do Restart the server & try – underdog May 28 '15 at 09:15
  • No, that also gives a 404, tried what you proposed but it doesn't work. I'll try re-"setting up" a fresh Eclipse with a fresh Tomcat I guess. – Jordy May 28 '15 at 09:19
  • I tried executing your code, updated my answer it works fine now. – underdog May 28 '15 at 09:50
  • No need to write this statement req.getRequestDispatcher("WEB-INF/Login.jsp").forward(req, resp); ... your code is working fine. but the thing is you should keep Login.jsp inside Webcontent. That's it – Anil Reddy Yarragonda May 28 '15 at 10:00
  • Thanks again for the add, I will check this tomorrow and will get back to this! – Jordy Jun 03 '15 at 14:44
-1

You should put web.xml file inside WEB-INF folder. If some thing wrong in the deployment descriptor (web.xml) then only we will get this Error 404 in Tomcat.

Please check the deployment descriptor path. see the below screen shot.

enter image description here

I have tried with your code. i am able to run your code successfully. I ll share structure and results belowstructure.

Structure:

enter image description here

Result without error msg:

enter image description here