| Author |
Embedded Tomcat servlet can't forward request to JSP
|
Yuhong Qian
Greenhorn
Joined: Jan 18, 2010
Posts: 4
|
|
Hi all,
I am using Embedded Tomcat7, servlet and jsp work very well separately, for example, http://localhost:8091/index.jsp or http://localhost:8091/hello work fine.
but when I use servlet to forward request to JSP, it can not get dispather for the jsp and throws java.lang.NullPointerException.
here is my code snippets:
tomcat.setBaseDir(".");
tomcat.setPort(port); //defaults to 8080 if not set.
File docBase = new File(".");
Context ctxt = tomcat.addContext("/", docBase.getAbsolutePath());
/**
the home page
* http://192.168.1.32:8091/
*/
try
{
/*
jsp files are located at /webapps/manage
*/
tomcat.addWebapp("", docBase.getAbsolutePath()+"/webapps/manage");
}
catch (ServletException e)
{
e.printStackTrace();
}
/**
* HelloServlet prints a message
* http://192.168.1.32:8091/hello
*/
Tomcat.addServlet(ctxt, "hello", new HelloServlet("Hello World!"));
ctxt.addServletMapping("/hello", "hello");
/**
* TestingServlet forward request to index.jsp
* http://192.168.1.32:8091/test
*/
TestingServlet testingServlet = new TestingServlet();
Tomcat.addServlet(ctxt, "test", testingServlet);
ctxt.addServletMapping("/test/*", "test");
the TestingServlet :
String forward = "/index.jsp";
RequestDispatcher dispatcher = this.getServletContext().getRequestDispatcher(forward);
dispatcher.forward(request, response);
the result throws java.lang.NullPointerException at dispatcher.forward(request, response);
I checked and found the dispatcher is null, it seems that the JSP is not at the same context as Servlet.
How to solve this problem?
yhqian
|
 |
William Brogden
Author and all-around good cowpoke
Rancher
Joined: Mar 22, 2000
Posts: 12265
|
|
If this was my problem the first thing I would do is log or print the String this statement creates:
I suspect it is meaningless.
Bill
|
Java Resources at www.wbrogden.com
|
 |
 |
|
|
subject: Embedded Tomcat servlet can't forward request to JSP
|
|
|