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