Question

I am currently working on a simple web App project with eclipse.

I use centos server and apache-tomcat-7.0.47.

I have an index.jsp file:

<form action="MyServlet">
   <input type="submit" value="Send" />
</form>

My servlet file MyServlet.java :

package com.srk.pkg;

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;
/**
  * Servlet implementation class MyServlet
*/
public class MyServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

    /**
      * @see HttpServlet#HttpServlet()
    */
    public MyServlet() {
       super();
    }

   /**
       * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
   */
     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        out.print("Hello Everybody..");
   }

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

and my web.xml file:

<?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"  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>Example1</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>MyServlet</display-name>
    <servlet-name>MyServlet</servlet-name>
    <servlet-class>com.srk.pkg.MyServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>MyServlet</servlet-name>
    <url-pattern>/MyServlet</url-pattern>
  </servlet-mapping>
</web-app>  

The problem I have is:

I run the web app from eclipse and everything is ok.

 http://localhost:8080/Example1/index.jsp

and

 http://localhost:8080/Example1/MyServlet?

but when i try accessing it from my centos server i get a

HTTP Status 404 - /Example1/WebContent/MyServlet

type Status report

message /Example1/WebContent/MyServlet

description The requested resource is not available.

Can anyone help me??

The links i use:

.....:8080/Example1/WebContent/index.jsp

and

....:8080/Example1/WebContent/MyServlet?

also

Package explorer

Was it helpful?

Solution

Why are you trying to access

/Example1/WebContent/MyServlet

?

Everything in the WebContent directory in your project will end up being at the root of the generated war file. Everything not in WEB-INF will be accessible, so you can do

/Example1/index.jsp

Otherwise, you have to go through a Servlet. You don't have a url mapping that matches

/Example1/WebContent/MyServlet

and that therefore gives you a 404.

You do have a url mapping for

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

You can access that at

/Example1/MyServlet

Note that submitting this form in a browser

<form action="MyServlet">
   <input type="submit" value="Send" />
</form>

depends on your current URL. If you had made your previous request to

/Example1/index.jsp

then submitting the form will send the request to

/Example1/MyServlet

If you were on

/Example1/some/random/path

then submitting will send the request to

/Example1/some/random/MyServlet

If you want to make your request always go to the same URL, you should make your path absolute

<form action="${pageContext.request.contextPath}/MyServlet">
   <input type="submit" value="Send" />
</form>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top