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