Tomcat 7.0: The requested resource (servlet) is not available
I am desperately trying to execute a servlet from an HTML action form and get 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've looked at a few questions here and tutorials, but I can't find what I'm missing (also I'm not very familiar with servlets and web programming).
I am using Eclipse with Tomcat 7.0.12.
My action form:
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:
source to share
If your application context /WSE_Web
, your application is correct and should work.
But if /WSE_Web
not your application context, change the url pattern to:
@WebServlet("/WSE_Web/QueryServlet")
To make sure you can also 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>
source to share
Make sure you have the correct 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 value of the method to get
as you are only overridingdoGet()
<form method='get' action='QueryServlet'>
...
</form>
source to share