servlets - Map with @WebServlet url pattern annotation in JSP -


when trying make simple webservlet stumble upon problem: if use name includes /../ won't find resource.

this have works:

controller.java

@webservlet(urlpatterns = {"/controller"}) public class controller extends httpservlet{ ... } 

and jsp-page:

<form action="controller"> ... </form> 

however, try specify name folder, work more structured. code struggle , doesn't work:

@webservlet(urlpatterns = {"/servletcontroller/controller"}) public class controller extends httpservlet{ ... } 

jsp-page:

<form action="/servletcontroller/controller"> ... </form> 

and tried numerous variations. question, how use folder-structure urlpatters-annotation, or not possible?

it's caused leading slash specified in <form action>. makes specified url domain-relative. i.e. it's resolved relative domain root in url see in browser's address bar, instead of requested path (folder) in url see in browser's address bar.

imagine jsp opened by

http://localhost:8080/contextname/some.jsp

and url has folder /contextname in it. form action have submit

http://localhost:8080/servletcontroller/controller

while servlet per mapping actually @ following url:

http://localhost:8080/contextname/servletcontroller/controller

fix form action accordingly make path-relative instead of domain-relative:

<form action="servletcontroller/controller"> 

or, specifying valid domain-relative url:

<form action="/contextpath/servletcontroller/controller"> 

or, if worry dynamicness of context path , rather not hardcode it:

<form action="${pagecontext.request.contextpath}/servletcontroller/controller"> 

see also:


Comments

Popular posts from this blog

c# - How Configure Devart dotConnect for SQLite Code First? -

java - Copying object fields -

c++ - Clear the memory after returning a vector in a function -