Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Mapping the URL pattern with the action in form

 
Parameswaran Thangavel
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
i confused over mapping the URL in the XML with the one i given in the action attribute of FORM tag.

can any one explain me over that
 
Sravan Kumar
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assume this in web.xml

<servlet>
<servlet-name> Login </servlet-name>
<servlet-class> LoginServlet </servlet-class>
</servlet>
<servlet-mapping>
<servlet-name> Login </servlet-name>
<url-pattern> /login </url-pattern>
</servlet-mapping>

servlet-mapping : You have a servlet-mapping corresponding to every URL pattern that you want your web application to support. The value of the url-pattern tag is the form action. For instance, if your application context is, say, 'shopping', then the form action will be "/shopping/login"

servlet : You have a servlet tag for every servlet of your application.

The container looks for the request URI. In this case, it is "/login" after the context has been stripped out. It takes the servlet name for this URI as "Login" (from <servlet-name> in <servlet-mapping> , goes to all <servlet> tags and looks out for one with this name. It takes the class "LoginServlet" from the <servlet-class> tag of the matching <servlet> tag and executes the request method in it (doGet, doPost ...)
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would add that it's a bad idea to hardcode the context name your HTML.
"/shopping/login".
Rather, use the request.getContextPath() property to read it dynamically.
This way if you ever have to change the context name, or a second instance (shopping_test, shopping_dev) etc, you won't have to work though all of your code to update it.
 
Sravan Kumar
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can i dynamically get the context [request.getContextPath()] in a HTML? If it is a JSP page, yes, i agree.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic