File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes ServletContext problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "ServletContext problem" Watch "ServletContext problem" New topic

ServletContext problem

Edward Chen
Ranch Hand

Joined: Dec 23, 2003
Posts: 798
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

Joined: Jan 09, 2001
Posts: 23
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

Joined: Dec 23, 2003
Posts: 798
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 ?

Ben Souther

Joined: Dec 11, 2004
Posts: 13410

It won't compile.
ServletContext servletCtx = "my-web";

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

Just use:

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.

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
I agree. Here's the link:
subject: ServletContext problem
It's not a secret anymore!