JaiswalTraining

Get the online training



Corejava Servlet Jsp  Php  Hibernate  Ajax Web Service   Ejb2.1 Ejb3.0 Struts Struts2  JPA Spring Ibatis   JSF    JSF2.0  CoreJavaDesignPattern    Jquery  Flex J2EE-Design-Patterns  Jboss7  Maven  Contact Me                                                                                                                                                                        
            IGNOU SMU PTU Project                                           Training                                                                                                                              
              

Contact Us  0091- 9210721663         jaiswaltraining@gmail.com





Corejava
Servlet
Jsp
Php
Hibernate
Ajax
Web Service
Ejb2.1
Ejb3.0
Struts  
Struts2
JPA
Spring
Ibatis
JSF
JSF2.0
CoreJavaDesignPattern
Jquery
Flex
J2EE-Design-Patterns
Jboss7
Maven







Request Dispatcher

RequestDispatcher view =  request.getRequestDispatcher("result.jsp");

  • We ask the request object for a dispatcher
  • We supply, as a String, a path to the JSP file
  • If the path begins with a slash, it is relative to the current context root
    Otherwise, it is relative to the servlet location
  • view.forward(request, response);
  • Having added the result information to the HttpRequest object, we forward the whole thing to the JSP

Example:


Default.html

<html>
  <body>
   <h2> Calculation Form</h2>
     <form action="./CalculateServlet">
       <table>
          <tr>
            <td>Input First number :</td>
            <td><input type="Text" name="num1" size="20" tabindex="1"></td>
        </tr>
      
       <tr>
            <td>Input Second Number :</td>
            <td><input type="text" name="num2" size="20" tabindex="1"></td>
        </tr>
        
         <tr>
            <td><input type="Submit" value="Add"></td>
            <td><input type="reset" value="Reset"></td>
        </tr>
    </table>
    
</form>
  </body>
</html>
Download code
CalculateServlet.java

import java.io.*;
import java.net.*;

import javax.servlet.*;
import javax.servlet.http.*;

public class CalculateServlet extends HttpServlet {
   
       protected void doGet(HttpServletRequest request,
 HttpServletResponse response
)
    throws ServletException, IOException {
       response.setContentType("text/html;charset=UTF-8");
         PrintWriter out = response.getWriter();
    
                     
          int n1=Integer.parseInt(request.getParameter("num1"));
          int n2=Integer.parseInt(request.getParameter("num2"));
          int result=n1+n2;
             
         request.setAttribute("result",new Integer(result));
         ServletContext contx=getServletContext();
             
         RequestDispatcher reqDisp=contx.getRequestDispatcher("/DisplayServlet");
         reqDisp.forward(request, response);
    
    }
}
Download code
DisplayServlet.java

import java.io.*;
import java.net.*;

import javax.servlet.*;
import javax.servlet.http.*;

public class DisplayServlet extends HttpServlet {
       
            protected void doGet(HttpServletRequest request,
 HttpServletResponse response
)
    throws ServletException, IOException {
               response.setContentType("text/html");
     PrintWriter out = response.getWriter();
      Integer res=(Integer)request.getAttribute("result");
    out.println("The result of the calculation is : "+res.toString());
                
    

    }
Download code
Download Zip Code

Output







response.sendRedirect(URL)

  • sends a response back to the browser that says, in effect, “I can’t handle this request; you should go to this URL instead.”
  • You cannot use this method if you have already written something to the response
  • The URL can be relative to the location of this servlet