How does a servlet communicate with a JSP page?

The following code snippet shows how a servlet instantiates a bean and initializes it with FORM data posted by a browser. The bean is then placed into the request, and the call is then forwarded to the JSP page, Bean1.jsp, by means of a request dispatcher for downstream processing.public void doPost (HttpServletRequest request, HttpServletResponse response){try {govi.FormBean f = new govi.FormBean();String id = request.getParameter("id");f.setName(request.getParameter("name"));f.setAddr(request.getParameter("addr"));f.setAge(request.getParameter("age"));//use the id to compute//additional bean properties like info//maybe perform a db query, etc.// . . .f.setPersonalizationInfo(info);request.setAttribute("fBean",f);getServletConfig().getServletContext().getRequestDispatcher("/jsp/Bean1.jsp").forward(request, response);} catch (Exception ex) {. . .}}The JSP page Bean1.jsp can then process fBean, after first extracting it from the default request scope via the useBean action.jsp:useBean id="fBean" class="govi.FormBean" scope="request"/ jsp:getPropertyname="fBean" property="name" / jsp:getProperty name="fBean" property="addr"/ jsp:getProperty name="fBean" property="age" / jsp:getProperty name="fBean"property="personalizationInfo" /

Showing Answers 1 - 12 of 12 Answers

sudhir nanda

  • Apr 1st, 2007
 

just create a RequestDispatcher Object by the help of ServletConfig.getRequestDispathcher("specify URL") than call RequestDispatcher.forword(request,response) or RequestRequestDispatcher.include(request,response).

  Was this answer useful?  Yes

josekutty K K

  • Jun 20th, 2007
 

I think Mr Nanda forget to mention the servletcontext object which is needed for creating requestdispatcher.

  Was this answer useful?  Yes

Rajkumar

  • Aug 24th, 2007
 

A Servlet can communicate with JSP by using the RequestDispatcher mechanism.RequestDispatching is the process hand overing the request to another web component,and this component takes the response of generating the response.This mechanism or interface having two methods
1)forward()
2)include()
First we need to get the rerference of the component,and then we need to use the corresponding methods according to our requirement...
Ex...
RequestDispatcher rd=request.getRequestDispatcher("/Home.jsp");
rd.forward(request,response);

Note:Before forwarding the request output should be fllushed otherwise it throws illegalStateException.

Hi Jose Kutty,

Mr. Nanda is wrong.

We can create RequestDespatcher object by two ways, either

 by ServletRequest object
       request.getRequestDispatcher("/abc.jsp");// here you need to mention relative path

(or)

  by ServletContext object
          sc.getRequestDispatcher("http://localhost:8080/TestApp/abc.jsp");// here you need to mention absolute path

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions