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
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()
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().
Joined: Jun 04, 2001
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.
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.
Joined: Jun 04, 2001