Forums Register Login

ServletContext's getContext method

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Try ServletContext sc = sg.getServletContext(String webAppContext);

You are getting the default web app context, which is the same web app context.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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>
You ridiculous clown, did you think you could get away with it? This is my favorite tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1761 times.
Similar Threads
Tomcat 5.0.12 context setting
RequestDispatcher foward cross web application error
passing session values between two web application
ServletContext's getContext() method
Where to deploy JSP and Servlet class files..
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 01:03:58.