Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

How to forward a request to a JSP using RequestDispatcher ?.

Program to demonstrate working of RequestDispatcher in a servlet for forwarding the request to the correct JSP


subscribe.html


1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Email Subscription</title>
</head>
<body>
 <h1>Join Hubberspot's Fan Club</h1>
 <p>
  In order to join our fan club, enter you name and email address
  below </br> and Click Submit button.
 </p>

 <form action="CustomerInfoDispatcher" method="post">
  <table cellspacing="5" border="0">
   <tr>
    <td align="right">First Name : </td>
    <td><input type="text" name="firstName"></td>
   </tr>
   <tr>
    <td align="right">Last Name : </td>
    <td><input type="text" name="lastName"></td>
   </tr>
   <tr>
    <td align="right">Email Address : </td>
    <td><input type="text" name="emailAddress"></td>
   </tr>
   
   <tr>
    <td></td>
    <td align="left"></br><input type="submit" value="Submit"></td>
   </tr>

  </table>
 </form>
</body>
</html>


CustomerInfoDispatcher.java

package com.hubberspot.jsp.servlets.examples;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hubberspot.model.Customer;
import com.hubberspot.utility.CustomerService;

@WebServlet("/CustomerInfoDispatcher")
public class CustomerInfoDispatcher extends HttpServlet {



    protected void doGet(
  HttpServletRequest request,
  HttpServletResponse response
  ) throws ServletException, IOException {
 doPost(request,response);
    }


    protected void doPost(
  HttpServletRequest request,
  HttpServletResponse response
  ) throws ServletException, IOException {
 // Get the form requests 
 String firstName = request.getParameter("firstName");
 String lastName = request.getParameter("lastName");
 String emailAddress = request.getParameter("emailAddress");
  
 // Using DataModel in a Servlet
 Customer customer = new Customer(firstName, lastName, emailAddress);
  
 // storing the Customer object in the request object
 request.setAttribute("Customer", customer);

 String url = "/customer_info.jsp";
 // forwarding request and response object to a JSP page 
 // Using RequestDispatcher Object
 RequestDispatcher dispatcher = 
   getServletContext().getRequestDispatcher(url);
 dispatcher.forward(request, response);

   }

}




customer_info.jsp


1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<%@ page import="com.hubberspot.model.Customer" %>

 <h1>Thanks for joining Hubberspot's Fan Club</h1>
 <p>Kindly have a look what you have entered :</p>

<% Customer customer = (Customer)request.getAttribute("Customer"); 
%>

 <table cellspacing="0" cellpadding="5" border="1">
  <tr>
   <td align="right">First Name :</td>
   <%-- Below is the JSP expression used to display string value of an expression --%>
   <td><%=customer.getFirstName()%></td>
  </tr>

  <tr>
   <td align="right">Last Name :</td>
   <td><%= customer.getLastName()%></td>
  </tr>

  <tr>
   <td align="right">Email Address :</td>
   <td><%= customer.getEmailAddress()%></td>
  </tr>
 </table>

 <p>
  To provide correct information, click 'back' on browser window </br>or
  click on Back button below :
 </p>

 <form action="subscribe.html" method="post">
  <input type="submit" value="Back">
 </form>


</body>
</html>


Customer.java

package com.hubberspot.model;

public class Customer {

 private String firstName; 
 private String lastName; 
 private String emailAddress;

 public Customer() {  
  firstName = "";
  lastName = "";
  emailAddress = "";
 }
 
 public Customer(String firstName , String lastName , String emailAddress) {
  this.firstName = firstName;
  this.lastName = lastName;
  this.emailAddress = emailAddress;
 }

 public String getFirstName() {
  return firstName;
 }
 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }
 public String getLastName() {
  return lastName;
 }
 public void setLastName(String lastName) {
  this.lastName = lastName;
 }
 public String getEmailAddress() {
  return emailAddress;
 }
 public void setEmailAddress(String emailAddress) {
  this.emailAddress = emailAddress;
 }
}




Output of the program :


subscribe.html




























customer_info.jsp



 
© 2021 Learn Java by Examples Template by Hubberspot