• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ServletContext's getContext() method

 
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello friends ,

I am facing one problem related to ServletContext's getContext() method .

I am unable to use this method . I tried to search for examples for this method but in vain.

Can anybody please tell me how to use getContext method .

I refered Servlet API but I didnt understood clearly from it..

So please can anyone help me out ?

Thanks and Regards
Rohit.
 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First u get the reference to servlet context using getServletContext() method of GenericServlet or using ServletConfig's getServletContext() method.
Now when u get the ServletContext reference , u can invoke the getContext using the method getContext(uriPath).
This method allows servlets to gain access to the context for various parts of the server. There may be many servlet applications running on ur server. I mean u can have many entries in ur server.xml, each having a different context path.
By using this method u will get the context reference of the other application's context.
U can try it out by giving ur servlet context path as specified in the server.xml.
Hope this is clear to you
 
Rohit Bhagwat
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your prompt reply..

But can you please specify the parameter that needs to be passed to getContext() method .

I am stuck with that issue.

I gave the following sytax but in vain

getContext("/NewProject");

Here the NewProject is the name of my web application.

Waiting for your reply

Regards
Rohit.
 
Ranch Hand
Posts: 250
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Rohit,

To get the RequestDispatcher you need to set the
crossContext to true.

Set to true if you want calls within this application to ServletContext.getContext() to successfully return a request dispatcher for other web applications running on this virtual host. Set to false (the default) in security conscious environments, to make getContext() always return null.

For more info pls goto this link--->>>

http://jakarta.apache.org/tomcat/tomcat-4.1-doc/config/context.html

Hope that helps.

Cheers
 
Rohit Bhagwat
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello friends ,

I am still unable to get rid of confusion related to getContext() method of ServletContext interface.

I am using Tomcat 5.0

WebApplication : myProject
-- Servlet : NamedDispatcher.java

In this servlet I wrote the following code in the service method

ServletConfig sg=getServletConfig();
ServletContext sc=sg.getServletContext();

if(sc==null)
{
System.out.println("sc is null");
}else
{
System.out.println("sc is not null"); // -------------------------- Line 1
ServletContext newsc=sc.getContext("/NewProject");

if(newsc==null)
System.out.println("newsc is null"); //----------------------------Line 2
else
{
newsc.getRequestDispatcher("/New.jsp").forward(request,response);
return;
}
}


Now I created another Webapplication in webapps directory

WebApplication : NewProject
-- JSP : New.jsp

In this JSP file I wrote the following code

<html>
<body>
<form name=ultra>
<input type=Textbox name=user size=3 maxlength=3>
<input type=button value=Enter>
</form>
</body>
</html>

When I access this JSP file using the following URL then I am able to see the user Interface
http://localhost:8080/NewProject/New.jsp


In the server.xml file present in the conf directory I made the following settings

<Context path="/myProject" docBase="D:\Program Files\Apache Software Foundation\Tomcat 5.0\webapps\myProject" crossContext="true" debug="0" reloadable="true" privileged="true"/>

<Context path="/NewProject" docBase="D:\Program Files\Apache Software Foundation\Tomcat 5.0\webapps\NewProject" crossContext="true" debug="0" reloadable="true" privileged="true"/>


But despite making these changes I am unable to see the JSP file of NewProject webapplication !!

I get the error as

Status 404
message /NewProject/New.jsp
description The requested resource (/NewProject/New.jsp) is not available.

The only output that I can see on the tomcat console is the "Line 1" indicated above.
I dont see "Line 2" on the tomcat console.

Can anyone please help me out of this problem ?

Waiting for reply,

Thanks and Regards
Rohit.
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A number of things to point out:

"Rohit India",
unfortunately your display name is not valid. Please edit your profile and select a valid display name. We require display names to be two words: Your first name, a space, then your last name. Fictitious names are not allowed.

Secondly: do you really need to make requests across contexts? I've tried it in a few containers and the implementation was pretty poor and buggy.

Other than that, it looks OK. I can't think of anything you haven't already tried.

Dave
 
Rohit Bhagwat
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello sir,

Actually I am preparing for SCWCD certification and in the book they mentioned regarding this method of thought of trying it..

But Is it that it depends on the container that is being used ?

Waiting for your reply.

Regards
Rohit.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(no 'sirs' around here, just us )
Drat, I can't find my SCWCD book. I believe the standard response from any vendor is that their implementation of feature X complies to the specification, but the truth is that some parts of the spec are written better than others.

For example, in one container (I forget which, it may have been tomcat) we couldn't get it to work, but in WebSphere we tried to include a JSP from another context, but this happened in another thread so we couldn't control where it got included! It's a long story. Requests in WS come in through a thread pool, and when you make requests across contexts in there implementation, it comes in through the thread pool again.

It's unlikely to be the answer you're looking for, but I'd leave it and go onto something else. I've never heard of anyone using getContext() in a production system.
 
Rohit Bhagwat
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello friends

The above problem doesnt occurs if I specify the html file instead of jsp file..

I am still confused that why it isnt able to access the jsp file if it can access the html file ?

Waiting for your reply

Thanks and Regards
Rohit
 
sawan parihar
Ranch Hand
Posts: 250
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi rohit,
Please see the code. I was able to run it.

package servlets.context;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletContext;
import javax.servlet.RequestDispatcher;


public class ContextServlet extends HttpServlet
{

public void doGet(HttpServletRequest req,HttpServletResponse res) throws IOException,ServletException
{
ServletConfig config = getServletConfig();

ServletContext cntx = config.getServletContext();

ServletContext cntx2 = cntx.getContext("/myotherapp");
//Getting the context of other webapp
System.out.println("Context of other webpp is =="+cntx2);
//Bingo got the request dispatcher
RequestDispatcher dispatch = cntx2.getRequestDispatcher("/hello.jsp");
//VOVO Forwarding
dispatch.forward(req,res);
}

}

Try running this by specifying your webapp paths. I guess in your code there must be some typo that i am not able to find.


Hope this helps.
 
Rohit Bhagwat
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello friend..

Thanks for your reply.. But I am still not able to get it right . I used your code which is very similar to mine but still it failed..

Which container you are using ? I am using Tomcat 5.0

Is it that Tomcat 5.0 doesnt allow me to access JSP from servlets using getContext() method ?

Waiting for reply
 
reply
    Bookmark Topic Watch Topic
  • New Topic