Question

I have a java servelet which is also mapped in web.xml file but when i run it it raises 404 error. Here is my source Code My Form with action is shown below

<form name="productsForm" method="post" action="/Checkout">

Here is 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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>AP Assignment 5</display-name>
  <servlet>
    <description>Called to process any forms on the website</description>
    <display-name>Form Processing Servlet</display-name>
    <servlet-name>Checkout</servlet-name>
    <servlet-class>Servlets.MyServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Checkout</servlet-name>
    <url-pattern>/Checkout</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

Here is my project hierarchy with my servelet code

enter image description here

Was it helpful?

Solution 2

Remove .class from <servlet-class> that is invalid here.


It should be

<servlet-class>Servlets.MyServlet</servlet-class>

instead of

<servlet-class>Servlets/MyServlet.class</servlet-class>

--EDIT--

try with appending Context Path if you are using it in JSP or append context path directly if using it in HTML.

<form name="productsForm" method="post" 
                          action="<%=request.getContextPath() %>/Checkout">

instead of

<form name="productsForm" method="post" action="/Checkout">

Please have a look at similar issue form action=“/sampleServlet” giving me exception.

OTHER TIPS

you have 2 mistakes in your web.xml

  1. you should not use .class
  2. if a class is present inside a package then you should use dot(.) not slash(/)

so change

<servlet-class>Servlets/MyServlet.class</servlet-class>

to

<servlet-class>Servlets.MyServlet</servlet-class>

according to my point of view there are no errors on web.xml as well as servelet page. web.xml should be

<servlet>
        <servlet-name>ServeletName</servlet-name>
        <servlet-class>Package.ServeletName</servlet-class>
    </servlet>
<servlet-mapping>
        <servlet-name>ServeletName</servlet-name>
        <url-pattern>/ServeletName</url-pattern>
    </servlet-mapping>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top