Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Embedded Tomcat servlet can't forward request to JSP

 
Yuhong Qian
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13056
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic