• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Embedded Tomcat servlet can't forward request to JSP

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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




 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic