my dog learned polymorphism*
The moose likes Servlets and the fly likes get servlet uri in init Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "get servlet uri in init" Watch "get servlet uri in init" New topic
Author

get servlet uri in init

Jeff Gaer
Ranch Hand

Joined: Jun 04, 2001
Posts: 99
I would like servlet to know the uri at which is being accessed.

hostname ortnumber
+context-root associated with the servlet in the application.xml
+ urlPattern associated with it in the web xml.
(actually I don't need the url pattern)
The goal would be to decide if a uri being requested from the current is located on the same container instance.
( we are NOT using clustering). I would have thought this information would be available in the servlet context somehow, but I cant find it. I know I can always put the context-root information into the web-xml file but I hate to have the same information in two places, it would make it easy to screw up the deployment.
Is this available either in the servletContext or off the servlet request ?
Thanks in advance


Sun Certified Java Programmer Java 2<P>Jeff Gaer
Anthony Watson
Ranch Hand

Joined: Sep 25, 2003
Posts: 327
If you think about it, a ServletContext object is for all the servlets of a web application to use. It has nothing to do with individual servlets or requests. How could an object which knows no request specific details be able to identify a request URI? However, the HttpServletRequest object was created for this very reason. It's focus/scope is the individual request level. It is thus not surprising to find that the object has methods which will allow you determine what the request uri that caused your servlet/jsp page to be invoked was.
Here are the methods HttpServletRequest:
request.getContextPath() example: /MyWebApplication
request.getServletPath() example: /MyServlet
request.getPathInfo() example: /MyPathInfo
request.getRequestURI() example: /MyWebApplication/MyServlet/MyPathInfo
request.getRequestURL() example: http://www.myHost.com:8080/MyWebApplication/MyServlet/MyPathInfo
Also, the ServletRequest object has a method that will allow you to determine what page your request came from: getRemoteAddr()


Anthony W.<br />MCP, SCJP 1.4, SCJD, SCWCD 1.3, SCWCD 1.4, SCBCD
Anthony Watson
Ranch Hand

Joined: Sep 25, 2003
Posts: 327
BTW: You cannot access the request object in the init method because the request cannot be processed until the init method has completed and the service method is called. The place to put your request object code is in doGet() or doPost().
Jeff Gaer
Ranch Hand

Joined: Jun 04, 2001
Posts: 99
I may have phrased that question wrong. At deployment time the application.xml specifies a context root for the .war file, and the web.xml specifies a servlet name. These combined with the protocol://host ort form the servlets url. These are known by the container at startup and are required to route the request to the servlet. Thats why I assumed they would be availble from the context.
I have an application that is distributed across multiple web apps, with request routed by a single front controller/dispatcher servlet. I would like the servlets to be able to register themselves with the front controller when they intialize. I realize I could put that information in a config file or as a parameter in the web.xml file but then the same information would have to be modified in multiple places each time the applciation is redeployed. In addition I need to build urls for other resources served by the web app like gifs ,etc. If the client uses relative paths it will look for those static resources relative to the front controller which may not be in the same web applciation or even the same server.
Phil Chuang
Ranch Hand

Joined: Feb 15, 2003
Posts: 251
I can't think of any trivial solution for what you described - since you can define different url-mappings in the web.xml, there can be multiple ways to get to the same servlet. Sounds like you want to develop some sort of http-tunnel that is a facade for several different resources, which means you'll need some sort of mapping, and url-rewriting functionality.
Jeff Gaer
Ranch Hand

Joined: Jun 04, 2001
Posts: 99
I'd be happy if I could just get to the context root, since that is defined outside of the web application it would be the same for all servlets within that app.
The problem with url rewriting is that the client side code builds the urls dynamically. I suppose I could send back one super anchor that the java script would pull in, but thats kind of a hack given the architechture. The servlet logic produces xml, which an xsl transformation converts to a combination of xhtml and javascript, that leaves all that information in the control of the presentation team. The would like an xml tag <appDocRoot> that returns the location of the document root for the applciation the initial request was routed to.
I've worked around it by including the context root as an init parameter in the webxml file. It just seems ugly to have to specify the context root in too places. If someone decides to change the deployed location they have to know to change the web.xml files as well, oh well.
 
Don't get me started about those stupid light bulbs.
 
subject: get servlet uri in init
 
Similar Threads
Problem understanding application.xml
Session Attributes and ServletContext
Multiple war in an ear file
Can we define same war with two different context-root?
application.xml /context-root/weblogic.xml question