Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

<form action="/sampleServlet" giving me exception [duplicate]

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.)?

like image 898
user2365917 Avatar asked May 22 '13 05:05

user2365917


People also ask

What is HttpServletRequest and HttpServletResponse?

The HttpServletRequest object can be used to retrieve incoming HTTP request headers and form data. The HttpServletResponse object can be used to set the HTTP response headers (e.g., content-type) and the response message body.

How do you invoke a servlet?

Calling a Servlet Programmatically To include another servlet's output, use the include() method from the RequestDispatcher interface. This method calls a servlet by its URI and waits for it to return before continuing to process the interaction. The include() method can be called multiple times within a given servlet.

Which protocol is used to interact with web client by servlet?

The Hypertext Transfer Protocol (HTTP) is application-level protocol for collaborative, distributed, hypermedia information systems. It is the data communication protocol used to establish communication between client and server.

What is HttpServletRequest?

The HttpServletRequest provides methods for accessing parameters of a request. The type of the request determines where the parameters come from. In most implementations, a GET request takes the parameters from the query string, while a POST request takes the parameters from the posted arguments.


2 Answers

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!

like image 79
Alexandre Lavoie Avatar answered Oct 27 '22 15:10

Alexandre Lavoie


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

like image 30
Ravi Kavaiya Avatar answered Oct 27 '22 15:10

Ravi Kavaiya