Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Request Dispatcher include?

 
Rajesh Vijaya
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

I want to call one servlet from another .I'm writing a simple web-application to accomplish the same.

My context root is Test.

I have the following files

Test/test.html

<html>
<body>

<form action=SS method=post>

<input type=submit value=go>

</form>

</body>

</html>

Test/WEB-INF/classes/Test

import javax.servlet.*;

import javax.servlet.http.*;

import java.io.*;

public class Test extends HttpServlet {

public void doPost(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException {

PrintWriter out = resp.getWriter();

out.println("I' m inside calling servlet");

// What should i specifiy in the path ???

getServletContext().getNamedDispatcher("Test2").include(req,resp);

}

}


Test/WEB-INF/classes/Test2


import javax.servlet.*;

import javax.servlet.http.*;

import java.io.*;

public class Test2 extends HttpServlet {

public void doPost(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException {

PrintWriter out = resp.getWriter();

out.println(" I'm inside called servlet");



}

}


Thanks

Rajesh
[ February 11, 2006: Message edited by: Bear Bibeault ]
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out ServletContext.getNamedDispatcher(). You shoudl also see the API description of the method.

Dave
 
Rajesh Vijaya
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi David ,

I did check them and i did check the Spec too...could you please tell me what to specify in the Path .....i'm unable to fix it ....


Thanks

Rajesh
 
Bosun Bello
Ranch Hand
Posts: 1511
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe you have to define and give the jsp a name in web.xml
 
Alec Lee
Ranch Hand
Posts: 569
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The API spec is not very explicit on this. But, I believe it is refering to the <servlet-name> element under <servlet>.
 
Alec Lee
Ranch Hand
Posts: 569
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW, why don't you use getServletContext().getRequestDispatcher() instead? In this way, you just provide a path - instead of servlet name - to the method.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic