Meaningless Drivel is fun!*
The moose likes Servlets and the fly likes Mapping the URL pattern with the action in form Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Mapping the URL pattern with the action in form " Watch "Mapping the URL pattern with the action in form " New topic
Author

Mapping the URL pattern with the action in form

Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
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

Joined: Sep 11, 2005
Posts: 121
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 ...)


keep smilin :: sravan<br /><a href="http://sravanpens.blogspot.com" target="_blank" rel="nofollow">I scribble here</a>
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Sravan Kumar
Ranch Hand

Joined: Sep 11, 2005
Posts: 121
How can i dynamically get the context [request.getContextPath()] in a HTML? If it is a JSP page, yes, i agree.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Mapping the URL pattern with the action in form
 
Similar Threads
Form Submission without ActionForm in Struts 1.3.
servlet not being called from html
How call an action from Javascript
Difference Between DispatchAction and Action (Not the usual question)
Need Help with Simple Servlet (my first servlet)