5

The problem : My index.jsp with web.xml keeps going into HTTP 404 and 500

I'm using Tomcat6 .

This is from index.jsp :

  <legend>Registration</legend>
  <form action="./register"> <%-- Address from web.xml  --%>
    First name: <input type="text" name="firstName"><br>
    Last name: <input type="text" name="lastName"><br>
    <input type="submit" value="Register">
  </form>

When I'm in Registration :

enter image description here

and I hit the name and last-name , I go into 404 , the message :

HTTP Status 404 - Servlet RegistrationServlet is not available

type Status report

message Servlet RegistrationServlet is not available

description The requested resource (Servlet RegistrationServlet is not available) is not available.

Apache Tomcat/6.0.35

What do you think it the cause for that error ?

The class RegistrationServlet is under the file RegistrationServlet.java in the folder src/coreservlets/

I checked web.xml but it seems to be okay , but here it is (if it would be helpful):

<?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/j2ee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
  <servlet>
    <servlet-name>ShowBalance</servlet-name>
    <servlet-class>coreservlets.ShowBalance</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>ShowBalance</servlet-name>
    <url-pattern>/show-balance</url-pattern>
  </servlet-mapping>
  <servlet>
    <servlet-name>RandomNumberServlet</servlet-name>
    <servlet-class>coreservlets.RandomNumberServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>RandomNumberServlet</servlet-name>
    <url-pattern>/random-number</url-pattern>
  </servlet-mapping>
  <servlet>
    <servlet-name>RegistrationServlet</servlet-name>
    <servlet-class>coreservlets.RegistrationServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>RegistrationServlet</servlet-name>
    <url-pattern>/register</url-pattern>
  </servlet-mapping>
  <servlet>
    <servlet-name>PrimeServlet</servlet-name>
    <servlet-class>coreservlets.PrimeServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>PrimeServlet</servlet-name>
    <url-pattern>/prime</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
</web-app>

I've been trying to fix this little culprit for the last two days but nothing , any help would be much appreciated .

EDIT:

As requested , here is RegistrationServlet

package coreservlets;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class RegistrationServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession();
        synchronized (session) {
            NameBean nameBean = (NameBean) session.getAttribute("name");
            if (nameBean == null) {
                nameBean = new NameBean();
                session.setAttribute("name", nameBean);
            }
            nameBean.setFirstName(request.getParameter("firstName"));
            nameBean.setLastName(request.getParameter("lastName"));
            String address = "/WEB-INF/mvc-sharing/ShowName.jsp";
            RequestDispatcher dispatcher = request
                    .getRequestDispatcher(address);
            dispatcher.forward(request, response);
        }
    }
}

Also here is the project tree :

enter image description here

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
JAN
  • 21,236
  • 66
  • 181
  • 318

4 Answers4

5

Your URL is completely fine. If the URL was wrong, you would have gotten a 404 message like follows:

Requested resource register is not available

But you instead got

Servlet RegistrationServlet is not available

This means that the servlet was found, but that it couldn't be executed because its construction and initialization failed with an exception. Under Tomcat's covers, basically one of the following steps has failed:

Servlet servlet = new RegistrationServlet();
servlet.init(servletConfig);
servlet.init();

You need to read the server logs for this exception and then fix the code accordingly.


Whilst your URL is completely fine, it's error prone to changes in the context. You'd rather like to specify a domain-relative URL instead:

<form action="${pageContext.request.contextPath}/register">

See also this related answer: Browser can't access/find relative resources like CSS, images and links when calling a Servlet which forwards to a JSP

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I am noob so have mercy but i have stupid question. Is it possible for Tomcat to use jsp files placed in WEB-INF in eclipse? – Pshemo Jul 02 '12 at 13:13
  • @Pshemo: Yes. That's even the recommended practice in MVC. Only a servlet can forward to them by `RequestDispatcher`. This way users are forced to use a front controller servlet to access the JSP and can never access those JSPs individually for which a pre/post processing servlet is required. See also our servlets wiki page to learn about using servlets: http://stackoverflow.com/tags/servlets/info – BalusC Jul 02 '12 at 13:15
  • Thank you very much. Sorry I'm thanking so late but I needed to test what you told me to make sure i understood you correctly :) – Pshemo Jul 02 '12 at 13:28
  • @BalusC: Thank you very much for the help with my last posts . Second ,I've tried to use this `
    ` but I still get `404` page like that `HTTP Status 404 - Servlet RegistrationServlet is not available type Status report message Servlet RegistrationServlet is not available description The requested resource (Servlet RegistrationServlet is not available) is not available.` . Where can I find the logs ?
    – JAN Jul 02 '12 at 13:30
  • @BalusC: Okay , then I got this `HTTP Status 500 - type Exception report message description The server encountered an internal error () that prevented it from fulfilling this request. exception javax.servlet.ServletException: Wrapper cannot find servlet class coreservlets.RegistrationServlet or a class it depends on` , meaning that it can't find the class . How can I fix this ? this is so annoying :) – JAN Jul 02 '12 at 13:34
  • Please read the server startup log, not the HTTP 500 error page. In Eclipse, it's just in the console which appears when you start the server. – BalusC Jul 02 '12 at 13:47
  • @BalusC: Okay ,so here it is : I've moved from `tomcat6` to `tomcat7` so no more `web.xml` . When I run the project from inside of Eclipse , it works great ! But when I create a `WAR` file and try to run using `localhost:8080/mvc` it just go crazy and starts all over again with page `404` & `500` . Then , what might cause this ? why I can run successfully from inside Eclipse but can't run just the same when using `WAR` file ? thanks again , you've helped a lot ! – JAN Jul 02 '12 at 14:16
  • If you create a WAR and manually deploy to Tomcat, you need to manually start Tomcat using its `startup` script instead of doing it by Eclipse, with the simple reason because the manually deployed WAR is not under control of Eclipse. Eclipse does by default not use Tomcat's deploy folder, but instead uses its engine and deploys into workspace. For some background and configuration see also this related answer: http://stackoverflow.com/a/2280798/157882 – BalusC Jul 02 '12 at 14:20
  • @BalusC: This is exactly what I did so far . I'm using tomcat with Xampp , I exported the `WAR` file to `C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps` and then executed from Chrome this line `http://localhost:8080/nameOfMyProject` , but after reaching the `Registration` page , all over again the `404` & `500` are back – JAN Jul 02 '12 at 14:25
  • You need to read server log of that Tomcat instance then in its `/logs` folder (assuming that Xampp hasn't modified its default trim; I have never used Xampp, so I can't tell that). Note that this doesn't appear in Eclipse at all as it's completely out of control of Eclipse. – BalusC Jul 02 '12 at 14:28
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/13310/discussion-between-ron-and-balusc) – JAN Jul 02 '12 at 14:29
1

Err... this is obvious, but is RegistrationServlet.class in /WEB-INF/classes/coreservlets/? Did you compile? (With Tomcat, do you NEED to compile explicitly? It'd be surprising if you did not.)

Joseph Ottinger
  • 4,911
  • 1
  • 22
  • 23
0

It could be that the path to register is not accounting for the context path. A good way to fix this would be to use the jstl core url tag -

<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
...
<c:url value="/register" var="url">
<form action="${url}">

This will ensure that if your context path is added to the url.

Biju Kunjummen
  • 49,138
  • 14
  • 112
  • 125
  • Which `jstl` version should I download ? and after that , just add the line `<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>` at the head (first line) of `index.jsp` ? – JAN Jul 02 '12 at 12:37
  • jstl 1.2, you can download from here - http://search.maven.org/remotecontent?filepath=javax/servlet/jstl/1.2/jstl-1.2.jar, and yes add that declaration at the top of the jsp file – Biju Kunjummen Jul 02 '12 at 13:02
  • You're suggesting a completely outdated JSTL URI. Please read our JSTL wiki page for the proper guide: http://stackoverflow.com/tags/jstl/info Apart from that, this is does not answer the concrete problem in any way. – BalusC Jul 02 '12 at 13:17
  • Oh, thanks did not realize that the jstl link was outdated. Yes the suggestion does not fix the underlying issue, was speculating that this could be one of the reasons why the servlet was not being found. +1 to your answer. – Biju Kunjummen Jul 02 '12 at 13:34
0

If any error occurred while instantiating servlet we will get this type of error, even if libraries missing in WEB-INF\lib folder. Make sure all the libraries are be placed in WEB-INF\lib.

eebbesen
  • 5,070
  • 8
  • 48
  • 70
ramesh
  • 1