Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ServletContext problem

 
Edward Chen
Ranch Hand
Posts: 798
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't understand the ServletContext.

In the javadoc, it said "one context per 'web application' per Java Virtual Machine ".

but in its public ServletContext getContext(java.lang.String uripath), it said "Returns a ServletContext object that corresponds to a specified URL on the server.".

Here, if we say one servlet context per web application, that mean, it is unique , no matter what different URLs. But this point is against the getContext(URL).

Could you help me out ? give me some examples.

I really appreciate your help.
 
Anil Sadi
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Edward,

You can have different web applications deployed in same web server. In that case, there are more than one ServletContext(s) exist in same web server(for each web application). If you want to access the servlet context of other applciation deployed in same web server from your application, you can use this method.

Anil Sadineni.
 
Edward Chen
Ranch Hand
Posts: 798
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Anil .

Furthermore, if I have
ServletContext servletCtx = "my-web" ;
RequestDispatcher rd = servletCtx.getRequestDispatcher("/login.jsp");
rd.forward(req, res);

What will happen ?

In my understanding, it will be same as the user rquest "my-web/login.jsp" in his web browser. but this time, the URL will not show up in browser address bar.

Is my understanding correct ?

Thanks
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It won't compile.
ServletContext servletCtx = "my-web";

You're trying to assign a string to a ServletContext variable.

Just use:
getServletContext().getRequestDispatcher("/login.jsp");

This will get the context object for the current app.

And yes, the user will see the login.jsp in their browser.
The browser's address window, however, will still show the original address for this servlet. This all takes place on the server without the browser's knowledge.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic