• 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 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

Dec 23, 2004 7:26:00 PM org.apache.jasper.runtime.JspFactoryImpl internalGetPageContext
SEVERE: Exception initializing page context
java.lang.NullPointerException

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.
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try ServletContext sc = sg.getServletContext(String webAppContext);

You are getting the default web app context, which is the same web app context.
 
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..

It is not the default web application context !!

It is the context which is specified in the getContext(String webapp) method.

Otherwise I wouldnt have got the output of servlet which I did in the same way !!

The only problem I am facing is If I specify JSP file name in RequestDispatcher method then I get error as NullPointerException.

The above menthioned above works very fine with servlets and html pages.

I mean I can call servlets and html pages of the web application that I specify in the getContext method..

How do I call JSP pages. I get the above exception ..That my problem
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You said "works very fine with servlets and html pages". Was your html page within WEB-INF?

Looks like a security issue. But at this point no idea what it is.
 
Nitish Bahadur
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rohit, I apologize for overlooking details. I am using "jsp-examples", which comes with Tomcat 5.0.28. Check if the <Context...> is under <Host> or <Engine>

Here is what I have, and it seems to work!
---------------------------------------------
Servlet Code: DispatchServlet.java
--------------------------------------------
package foo;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class DispatchServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
ServletContext sc = getServletContext();
PrintWriter out = response.getWriter();
if (sc != null) {
ServletContext otherContext = sc.getContext("/jsp-examples");
if (otherContext != null) {
otherContext.getRequestDispatcher("/hello.jsp").forward(request,response);
} else {
out.println("otherContext is NULL<br>");
}
} else {
out.println("sc is NULL<br>");
}
}
}

----------------------------------------------------------------
hello.jsp file needs to be added to /jsp-examples under webapps
----------------------------------------------------------------
<html>
<body>
Welcome to our page!
</body>
</html>

-------------------------------------------------
<TOMCAT-HOME>/conf/server.xml
-------------------------------------------------
<server>
...
<host>
...
<Context path="/scwcd"
docBase="C:\Program Files\Apache Software Foundation\Tomcat 5.0\webapps\scwcd"
crossContext="true" debug="0" reloadable="true" privileged="true"/>

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

</host>
...
</server>
 
reply
    Bookmark Topic Watch Topic
  • New Topic