11

In my jsp if I call <form action="/sampleServlet" method="get" name="form1">, I get the following exception :

http 404 error--sampleServlet is not found.I set sampleServlet in web.xml file and url-pattern also set to /sampleServlet.

Why I am getting 404 (not found servlet.)?

Patrick D'Souza
  • 3,491
  • 2
  • 22
  • 39
user2365917
  • 1,095
  • 5
  • 15
  • 21

3 Answers3

37

When you are using URL in HTML, without leading / they are relative to the current URL (ie current page displayed). With leading / they are relative to the website root :

<form action="/context-path/sampleServlet">

or

<form action="sampleServlet">

will do what you want.

I suggest you to add the context inside the action path dynamically. Example (in JSP) :

<form action="${pageContext.request.contextPath}/sampleServlet">

With this you will never have to change the path, for example, if you move your file or copy your code, or rename your context!

Alexandre Lavoie
  • 8,711
  • 3
  • 31
  • 72
  • This is giving me a 404 error with the following URL: http://127.0.0.1:8888/${pageContext.request.contextPath}/fileUpload – fIwJlxSzApHEZIl Aug 20 '14 at 22:43
  • @advocate You can't use this URL directly into the browser, the `${}` content should be rendered by the server before. Also, the port is not common, is your server really running on your computer on port 8888 ? You are better to post a new question if your setup is different of the OP. – Alexandre Lavoie Aug 21 '14 at 00:56
  • I didn't type directly into the browser, that's what I ended up with after taking the link. I believe the issue was I pasted that into an HTML file and I needed to paste it into a .JSP file. – fIwJlxSzApHEZIl Aug 21 '14 at 06:18
  • @advocate sure, the file extension will be filtered by the server to know if it need to process it or not. – Alexandre Lavoie Aug 24 '14 at 02:31
  • as a side remark, an URL starting with a slash isn't absolute as such, it is relative, but relative to the site root. – Laurent S. Aug 31 '15 at 12:18
  • @LaurentS. Yes you are right, an absolute URL will include the protocol and domain name, i'll update the answer. – Alexandre Lavoie Aug 31 '15 at 15:00
5

might help you

servlet configuration

<servlet>
    <servlet-name>sampleServlet</servlet-name>
    <servlet-class>test.sampleServlet</servlet-class>
  </servlet>
<servlet-mapping>
    <servlet-name>sampleServlet</servlet-name>
    <url-pattern>/sampleServlet/</url-pattern>
  </servlet-mapping>

Servlet Code :

package test;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class sampleServlet extends HttpServlet{

    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws IOException{
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<body>");
        out.println("<h1>Hello Servlet Get</h1>");
        out.println("</body>");
        out.println("</html>"); 
    }
}

JSP code :

<html>
  <body>
     <form action="/sampleServlet/" method="GET">
      <input type="submit" value="Submit form "/>
     </form>
  </body>
</html>

you can click on submit button and after you can see servlet out put

Ravi Kavaiya
  • 829
  • 6
  • 16
0

Just use action="sampleServlet"

It will work for you.

Syam
  • 19
  • 2