1

I am trying to call a servlet from my html form. My web.xml file has:

<?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_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>apphome</display-name>
 <servlet>
    <display-name>ControllerServlet</display-name>
    <servlet-name>ControllerServlet</servlet-name>
    <servlet-class>com.main.servlets.Controller</servlet-class>
 </servlet>
 <servlet-mapping>
    <servlet-name>ControllerServlet</servlet-name>
    <url-pattern>/Controller</url-pattern>
 </servlet-mapping>
 <welcome-file-list>
    <welcome-file>home.html</welcome-file>
</welcome-file-list>

My servlet is located under src/com/main/servlets/Controller My form action is "ControllerServlet" I don't have any annotations in my Controller class.

When I submit the form, I get a 404. Any idea what I'm missing?

Scorpion
  • 577
  • 4
  • 21
user1154644
  • 4,491
  • 16
  • 59
  • 102

2 Answers2

3

The servlet is mapped to the url pattern /Controller. The unique URL to reach this servlet is thus http://localhost:8080/apphome/Controller.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • So my form action should be "/Controller"? – user1154644 Mar 01 '14 at 22:49
  • `/Controller` is an absolute path. As a relative path, from `home.html`, for instance, it should be just `Controller` (without the slash). – Martín Schonaker Mar 01 '14 at 22:54
  • Ok, when I submit my form, I get directed to: http://localhost:8080/apphome/Controller (Now I get a 500 error ClassNotFoundException on the Controller class – user1154644 Mar 01 '14 at 22:56
  • 1
    Your form action should use a correct relative path, or an absolute path with the context path prepended (`${pageContext.request.contextPath}/Controller`), or an absolute path using the JSTL (``). the last one being my preferred solution, by far. – JB Nizet Mar 01 '14 at 22:57
  • Regarding the error 500, that's another problem, indicating that a class is missing. You probably forgot to add a library to WEB-INF/lib. The name of the missing class should tell you which one(s). – JB Nizet Mar 01 '14 at 22:59
0

use this:

http://localhost:8080/apphome/servlet/Controller

this extra servlet here before the Controller specifies the servlet container you need to put this up!

Sahai
  • 1