2

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

André Laszlo
  • 15,169
  • 3
  • 63
  • 81
tasos.kou
  • 35
  • 2
  • 8

1 Answers1

0

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>
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • Thanks for the help!i run the .jsp with action="${pageContext.request.contextPath}/MyServlet"> but again nothing happened.I have 404 error again but now the message is :message /Example1/MyServlet I can't understand why it doesn't work..the servlet now is on the right path – tasos.kou Jan 15 '14 at 08:13
  • @tasos.kou Adding a log statement (system.out.print) in your `Servlet`'s constructor then add `1` to your `` element. When you start the Servlet container, check if the log statement is printed. – Sotirios Delimanolis Jan 15 '14 at 13:45
  • I added the `1` to the servlet and i put a `System.out.println("hello, world");` under `super();` in MyServlet. Eclipse printed out hello,world in the console..nothing happened in my server – tasos.kou Jan 15 '14 at 14:09
  • @tasos.kou You will have to show us how you deploy it on centos server. – Sotirios Delimanolis Jan 15 '14 at 14:32
  • I hope i understand what you want me to show you.I made the changes you told me in eclipse then i passed the folder (Example1) via FileZilla on my server.this is the link (http://83.212.98.109:8080/Example1/WebContent/index.jsp) which leads to (http://83.212.98.109:8080/Example1/MyServlet) error 404 – tasos.kou Jan 15 '14 at 14:41
  • @tasos It's possible you're just deploying it wrong. With eclipse, select your project and export it to a `.war` file. Take that file and put it in your server's `tomcat-installation/webapps` directory, then restart tomcat. – Sotirios Delimanolis Jan 15 '14 at 14:45
  • Yesssss!!!Finally it worked!!many many thanks!!So when i have more than 1 .jsp the .war file will run the index.jsp one??right? – tasos.kou Jan 15 '14 at 14:58
  • @tasos.kou It has nothing to do with how many JSPs you have. The `.war` file contains your whole web application, including servlets and JSPs. – Sotirios Delimanolis Jan 15 '14 at 14:59
  • @tasos.kou Also, consider upvoting/accepting answers if they helped. That's how stackoverflow goes 'round. – Sotirios Delimanolis Jan 15 '14 at 15:01
  • I was trying to up vote but i can't do it. Unfortunately in need 15 Reputation – tasos.kou Jan 15 '14 at 15:03
  • @tasos.kou Don't worry about that. If you have any other questions, don't hesitate. – Sotirios Delimanolis Jan 15 '14 at 15:05