Emil Smęt
Emil Smęt

Reputation: 899

No servlet name in URL when trying to run Eclipse project

Hi I'm running my web app, and when I choose "run on server" I get this error in the browser:

HTTP Status 404 - /ServletsJSPExample/

--------------------------------------------------------------------------------

type Status report

message /ServletsJSPExample/

description The requested resource is not available.


--------------------------------------------------------------------------------

Apache Tomcat/7.0.47

The URL should be like:

http://localhost:8080/ServletsJSPExample/Serv1

but it is

http://localhost:8080/ServletsJSPExample/

Just to be clear, everything is fine when I write down the address myself, but I want it to be already there when I run my app.

And here is my web.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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>ServletsJSPExample</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <description></description>
    <display-name>ServletExample</display-name>
    <servlet-name>ServletExample</servlet-name>
    <servlet-class>com.example.tutorial.ServletExample</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>ServletExample</servlet-name>
    <url-pattern>/Serv1</url-pattern>
  </servlet-mapping>
</web-app>


And here is servlet class file:

package com.example.tutorial;

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 ServletExample extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        out.println("Hello Java");
    }


}

I'm using Tomcat 7 and Eclipse Juno.

Upvotes: 1

Views: 2843

Answers (2)

Luiggi Mendoza
Luiggi Mendoza

Reputation: 85779

Two steps:

  1. Define a welcome file in your web.xml file:

    <welcome-file-list>
        <welcome-file>Serv1</welcome-file>
    </welcome-file-list>
    
  2. Create an empty file called Serv1 (no extension) in the root context of your web application project. This is because the welcome file must be a physical file in the folder structure. The application server will process the GET request for http://localhost:8080/ServletsJSPExample/Serv1 against your servlet. Folder structure example:

    + ServletsJSPExample
      + src
        <your source packages...>
      + webapp
        - Serv1
        <other files and folders...>
    

Apart from this, it would be better to implement doGet and doPost methods instead of service so you could handle different business logic for each HTTP method.

Upvotes: 1

Nikos Paraskevopoulos
Nikos Paraskevopoulos

Reputation: 40296

1) You can add a welcome file:

<welcome-file>Serv1</welcome-file>

This approach will use the welcome file for all directories, e.g. if you have a directory foo/bar, the user requests http://server/ServletsJSPExample/foo/bar/ and there is nothing mapped directly to that, the container will also try http://server/ServletsJSPExample/foo/bar/Serv1.

2) You can also add a mapping from / to ServletExample, in addition to the mapping form Serv1:

<servlet-mapping>
    <servlet-name>ServletExample</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>

But this will make it the "default servlet", i.e. it will serve all URLs not explicitly mapped to another resource or servlet. It is OK if take account of it in the code.

3) You can create an index.jsp in the root folder that does a redirection:

<% response.sendRedirect("Serv1"); %>

Please do note there is no leading slash, i.e. the URL is relative. In this case there will be 2 requests (the original and the redirection) and the URL in the browser will change accordingly.

Or the JSP can do a forward:

<jsp:forward page="Serv1" />

In this case there is a single request and the URL in the browser remains the same.

Upvotes: 2

Related Questions