0

Consider following form tag in a jsp:

<form action="/sampleServlet" method="get">

what is difference between

<form action="/sampleServlet" method="get">

and

<form action="sampleServlet" method="get"> <!--no leading slash-->
a Learner
  • 4,944
  • 10
  • 53
  • 89
  • the thing is if you use /sampleServlet, then it will use the currentpath + /sampleServlet, so the call will goes to one of the mapped servlet in your application. if you use sampleServlet only, then the url itself will changed to http://sampleServlet which is invalid and result in error – TKV Feb 13 '14 at 11:07
  • your comment contradicts with the answer given by Alexandre in `http://stackoverflow.com/questions/16683877/form-action-sampleservlet-giving-me-exception` – a Learner Feb 13 '14 at 12:00

2 Answers2

0

Code <form action="/sampleServlet" method="get"> will submit form and invoke servlet's doGet() mapped by alias /sampleServlet in web.xml

Where as in later case submit will throw error (possibly 404).

Dark Knight
  • 8,218
  • 4
  • 39
  • 58
0

/sampleServlet - absolute path

this path is absolute to the base url (protocol, ip (or hostname) and port)

current page: http://127.0.0.1:8080/context/test

target page:  http://127.0.0.1:8080/sampleServlet

sampleServlet - Relative path

this path is relative to path of the current page, e.g.

current page: http://127.0.0.1:8080/context/test

target page:  http://127.0.0.1:8080/context/test/sampleServlet

In JSP you should use the absolute path, but remember to add the context path automatically and think about url rewriting (to add the session id to the url if necessary).

When working with JSTL use <c:url value="/sampleServlet"/>:

<form action="<c:url value="/sampleServlet"/>" method="get">
...
</form>