How to specify servlet in the action attribute of a <form>?

I have a form in jsp file that is processed in a servlet

The servlet looks like this

@WebServlet("/hello")
public class Test extends HttpServlet 
    //////////////

The jsp file looks like this

<form action="/Project/hello" method="GET">
  <input type="submit" value="Submit form "/>
 </form>

I need this servlet to forward request to a different jsp So i modify this code as

// I remove @WebService  The Test class is in the test folder
public class Test extends HttpServlet 
    ////////////// 
 forward  blah blah


<form action="/test/Test" method="GET">
  <input type="submit" value="Submit form "/>

And i get 404

How can I specify my servlet in the action attribute of the form ?

Upvotes: 0

Views: 3956

Answers (3)

Gundamaiah
Gundamaiah

Reputation: 786

First you need to map the servlet with url in web.xml like below

<servlet>
 <servlet-name>testServlet</servlet-name>
 <servlet-class>com.company.Test</servlet-class>
</servlet>
<servlet-mapping>
 <servlet-name>testServlet</servlet-name>
 <url-pattern>/test</url-pattern>
</servlet-mapping>

Then you need to give the url in the form action as below:

<form action="test" method="get">

your method in servlet(doGet or doPost) should match the method that you are specifying in your jsp.

Upvotes: 1

jeremyjjbrown
jeremyjjbrown

Reputation: 8009

@WebService("/test/Test")
public class Test extends HttpServlet 

You need the annotation to map the servlet to a url or an entry for the servlet in your web.xml

Upvotes: 1

Kiran
Kiran

Reputation: 20313

Map it in web.xml on an <url-pattern> like /test.

<servlet>
    <servlet-name>testServlet</servlet-name>
    <servlet-class>com.example.Test</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>testServlet</servlet-name>
    <url-pattern>/test</url-pattern>
</servlet-mapping>

And in your jsp, form action point to this URL.

<form action="test"/>

Upvotes: 1

Related Questions