0

I am trying to create a simple Java servlet program in myeclipse for spring. When i deploy my program it's gives me the above mentioned "TITLE" error. Can anyone help me.?

package com.example.hello;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

// Extend HttpServlet class
public class Main extends HttpServlet {

private String message;

public void init() throws ServletException
{
  // Do required initialization
  message = "Hello World";
}

public void doGet(HttpServletRequest request,
                HttpServletResponse response)
        throws ServletException, IOException
{
  // Set response content type
  response.setContentType("text/html");

  // Actual logic goes here.
  PrintWriter out = response.getWriter();
  out.println("<h1>" + message + "</h1>");
}

public void destroy()
{
  // do nothing.
}
}

and my web.xml is :

<servlet>
    <servlet-name>Hello</servlet-name>
    <servlet-class>com.example.hello.Main</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>Hello</servlet-name>
    <url-pattern>/Hello</url-pattern>
</servlet-mapping>
  • And what's the resource you are requesting? What server are you using? Any server-log-entries that might give a clue? – piet.t Jul 17 '14 at 06:03
  • Am using myeclipse tomcat server. – user3847683 Jul 17 '14 at 06:09
  • @user3847683 - please post the structure of your project. There can be many reasons for this 404 error. Eg. when you place some content under web-inf, when resource/page does not exist etc. – Erran Morad Jul 17 '14 at 06:13
  • @Borat : I don't know how to post my structure. There is a src with package and class. JRE system library , Java EE 5 library , Web app libraries , Web root --> META-INF --> WEB-INF --> lib -> web.xml and index.jsp – user3847683 Jul 17 '14 at 06:24

1 Answers1

0

It looks as though you have deleted the following text from the generated web.xml file (that was generated by the project creation wizard):

<welcome-file-list>
  <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

If this element is present, the index.jsp file would have been processed, instead of your getting a 404.

When you create a web project and deploy it, it will be deployed to a context root, by default, this is the same as the project name. So, if your project name is "MyProject", the context root will be "MyProject". Your servlet mapping is on top of this context root. So the correct URL to invoke your servlet would be:

http://localhost:8080/MyProject/Hello

Of course, change "MyProject" to your actual project name, or to the web context root, if you've specified that as something other than the default. There is no web application associated with

http://localhost:8080/Hello 

which is why you get the 404 error.

Tony Weddle
  • 2,081
  • 1
  • 11
  • 15