3


I'm desperately trying to execute a Servlet from an HTML action form and getting the following error message:

HTTP Status 404 - /WSE_Web/QueryServlet

type: Status report

message: /WSE_Web/QueryServlet

description: The requested resource (/WSE_Web/QueryServlet) is not available.

I looked through several questions here and tutorials but I cannot find what I'm missing (also I'm not very familiar with Servlets and Web Programming).
I'm using Eclipse with Tomcat 7.0.12.

My Action form:

enter image description here

My Servlet class:

package servlet;

import java.io.*;
import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.*;

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

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

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" metadata-complete="true" version="3.0">
  <display-name>WSE_Web</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
</web-app>

Project Structure:

enter image description here

Frank Wittich
  • 139
  • 3
  • 11
  • Try changing the action to `/QueryServlet` with a slash. – RealSkeptic Dec 22 '14 at 16:18
  • Look at http://stackoverflow.com/questions/6535676/webservlet-annotation-with-tomcat7 – rickz Dec 22 '14 at 16:32
  • Not sure it is correct to make reference to http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd **and** http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd on the same `web.xml` file. Is this correct?. AFAIK, You need to stick to 3.0 for tomcat to know and process your annotations – blurfus Dec 22 '14 at 18:56

2 Answers2

0

If your application context is /WSE_Web your application is right and should work.

But if /WSE_Web is not your application context change url-pattern to:

@WebServlet("/WSE_Web/QueryServlet")

To be sure you also can use web.xml file:

<web-app>
    ...
    <servlet>
        <servlet-name>QueryServlet</servlet-name>
        <servlet-class>servlet.QueryServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>QueryServlet</servlet-name>
        <url-pattern>/QueryServlet</url-pattern>
    </servlet-mapping>
</web-app>
Ali Sepehri.Kh
  • 2,468
  • 2
  • 18
  • 27
0

Make sure to you have proper html:

<input type='text' name='query' size='96'/><!-- your missing the `/` at the end -->
<input type='submit' name='subButton' value='Search!'/><!-- your missing the `/` at the end -->

Also change the method value to get, since you are overriding only doGet()

<form method='get' action='QueryServlet'>
...
</form>