0

I had Created Dynamic Project eclipse and added package in java resource then added a Servlet in it. Here is my class where i simply getting a hello world out in doGet Method. package com.helloworldserverlet.serverlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class HelloWorld
 */
@WebServlet("/HelloWorld")
public class HelloWorld extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public HelloWorld() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.getWriter().append("Served at: ").append(request.getContextPath());
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

Now while I try to run it i will gives me error 404 that require resources are not found. Do i have to defined the servlet in my web.xml .Currently my web.xml is

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>HelloWorld</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>
</web-app>

Thanks in Advance

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Hassan ALi
  • 1,313
  • 1
  • 23
  • 51

2 Answers2

1

while I try to run it i will gives me error 404

There could be lot of reasons, the most common ones are:

  • Your application context path is "app" and you run it using
    http://localhost:8080/HelloWorld
    the correct way to do is
    http://localhost:8080/app/HelloWorld

  • Your application context path is "/" and you run it using
    http://localhost:8080/app/HelloWorld
    the correct way to do is
    http://localhost:8080/HelloWorld

  • You simply spelled the servlet path wrong, the path mapping is case-sensitive.

Note:
"app" can be any name you specified, I used it just for demo. Please provide us with more info such as your complete expanded project tree and your browser location bar where you have put the url.

Minjun Yu
  • 3,497
  • 4
  • 24
  • 39
0

You need to set more configuration in web.xml. I have a sample from Spring, and in my configuration I have:

<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
      http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">

    ...

    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

Shouldn't be too different. I think you should change org.springframework.web.servlet.DispatcherServlet for com.helloworldserverlet.serverlet.HelloWorld, or whatever your Servlet class is.

CodeLionX
  • 53
  • 1
  • 4
houcros
  • 1,000
  • 1
  • 14
  • 32