1
votes

I have a project in Eclipse and trying to call a servlet from the web browser. The image shows the structure of my project. Although I set the url in the annotation I still can't get find the resource.

Here is my code:

package java.enablingKeyWordSearch;

import java.io.IOException;
import java.io.PrintWriter;

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

@WebServlet(asyncSupported = false, name = "HelloServlet", urlPatterns = {"/hello"})
public class TestServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html");
        PrintWriter out = resp.getWriter();
        out.write("<h2>Hello Servlet One </h2>");
        out.close();
    }
}

project hierarchy

I have tried calling the servlet using the following: http://localhost:8080/MultiKeywordSearch/hello http://localhost:8080/MultiKeywordSearch/src/java/enablingKeyWordSearch/hello

... and so on. Yet, I get an HTTP Status 404 Error.

This is the content of my web.xml file:

<?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>MultiKeywordSearch</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>

I am using Apache Tomcat v8.0 if that makes a difference.

UPDATE: showing source listing in the new screenshot; removed java namespace updated src listing

EDIT 2.0 web.xml:

<?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>MultiKeywordSearch</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>
        <servlet-name>HelloServlet</servlet-name>
        <servlet-class>enablingKeyWordSearch.TestServlet</servlet-class>
    </servlet>

<servlet-mapping>
    <servlet-name>HelloServlet</servlet-name>
    <url-pattern>/multiKeywordSearch</url-pattern>
</servlet-mapping>
</web-app>

The enablingKeyWordSearch.TestServlet.java file:

package enablingKeyWordSearch;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//@WebServlet(asyncSupported = false, name = "HelloServlet", urlPatterns = {"/hello"})
@WebServlet(name = "HelloServlet", urlPatterns = {"/multiKeywordSearch"})
@MultipartConfig
public class TestServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html");
        PrintWriter out = resp.getWriter();
        out.write("<h2>Hello Servlet One </h2>");
        out.close();
    }
}

Still getting a 404 error sadly.

2
What's the warning it's showing on the type name? Also, change your package names so that they do not use a reserved namespace like "java." - nitind
It just says the serializable class does not declare a static final serialVersionUID field of type long. Fixing the namespace issue now - i_use_the_internet
fixed namespace issue, did not fix the problem - i_use_the_internet
Please update your source listing, and if you are using a web.xml file, include its contents. - nitind
@nitind done, see above - i_use_the_internet

2 Answers

2
votes
@WebServlet("/multiKeywordSearch")

@MultipartConfig


and then try this.
http://localhost:8080/MultiKeyewordSearch/multiKeywordSearch
0
votes

Try updating your web.xml

 <servlet>
    <servlet-name>helloServlet</servlet-name>
    <servlet-class>java.enablingKeyWordSearch.TestServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>helloServlet</servlet-name>
    <url-pattern>/hello</url-pattern>
</servlet-mapping>