Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ServletContext's getContext method

 
Rohit Bhagwat
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
  • 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.
 
Nitish Bahadur
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Deb Sadhukhan
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • 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
  • 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>
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic