JAN
JAN

Reputation: 21885

HTTP Status 404 - Servlet [ServletName] is not available

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

Upvotes: 5

Views: 51397

Answers (4)

ramesh
ramesh

Reputation: 1

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.

Upvotes: 0

BalusC
BalusC

Reputation: 1109462

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

Upvotes: 5

Biju Kunjummen
Biju Kunjummen

Reputation: 49935

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.

Upvotes: 0

Joseph Ottinger
Joseph Ottinger

Reputation: 4951

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.)

Upvotes: 1

Related Questions