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








Integrating Servlets and JSP: The Model View Controller (MVC) Architecture

Implementing MVC with RequestDispatcher

1. Define beans to represent the data

2. Use a servlet to handle requests
   – Servlet reads request parameters, checks for missing and malformed data, etc.

3. Populate the beans
 – The servlet invokes business logic (application-specific code) or data-access code to obtain the results. Results are placed in the beans that were defined in step 1.

4. Store the bean in the request, session, or servlet context
 – The servlet calls setAttribute on the request, session, or servlet context objects to store a reference to the beans
that represent the results of the request.

5. Forward the request to a JSP page.
 – The servlet determines which JSP page is appropriate to the situation and uses the forward method of RequestDispatcher to transfer control to that page
.
6. Extract the data from the beans.
 – The JSP page accesses beans with jsp:useBean and a scope matching the location of step 4. The page then uses jsp:getProperty to output the bean properties.

 – The JSP page does not create or modify the bean; it merely extracts and displays data that the servlet created.

public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
String operation = request.getParameter("operation");
if (operation == null) {
operation = "unknown";
}
String address;
if (operation.equals("order")) {
address = "/WEB-INF/Order.jsp";
else if (operation.equals("cancel")) {
address = "/WEB-INF/Cancel.jsp";
else {
address = "/WEB-INF/UnknownOperation.jsp";
}
RequestDispatcher dispatcher =
request.getRequestDispatcher(address);
dispatcher.forward(request, response);
}


jsp:useBean in MVC vs.in Standalone JSP Pages

• The JSP page should not create the objects
  •  The servlet, not the JSP page, should create all the data objects. So, to guarantee that the JSP page will not create
    objects, you should use.
<jsp:useBean ... type="package.Class" />

instead of

<jsp:useBean ... class="package.Class" />

• The JSP page should not modify the objects
 
 So, you should use jsp:getProperty but not

jsp:setProperty.
Reminder: jsp:useBean Scope Alternatives

request

– <jsp:useBean id="..." type="..." scope="request" />

• session
– <jsp:useBean id="..." type="..." scope="session" />

• application
– <jsp:useBean id="..." type="..." scope="application" />

• page
– <jsp:useBean id="..." type="..." scope="page" />

or just

<jsp:useBean id="..." type="..." />

This scope is not used in MVC (Model 2) architecture


Session-Based Data Sharing
• Servlet
ValueObject value = new ValueObject(...);
HttpSession session = request.getSession();
session.setAttribute("key", value);
RequestDispatcher dispatcher =
request.getRequestDispatcher
("/WEB-INF/SomePage.jsp");
dispatcher.forward(request, response);
• JSP 1.2
<jsp:useBean id="key" type="somePackage.ValueObject"
scope="session" />
<jsp:getProperty name="key" property="someProperty" />
• JSP 2.0
${key.someProperty}


Session-Based Data Sharing: Variation

• Use response.sendRedirect instead of RequestDispatcher.forward

• Distinctions: with sendRedirect:

– User sees JSP URL (user sees only servlet URL with RequestDispatcher.forward)

– Two round trips to client (only one with forward)

Advantage of sendRedirect

– User can visit JSP page separately

• User can bookmark JSP page

Disadvantage of sendRedirect

– Since user can visit JSP page without going through servlet first, JSP data might not be available

• So, JSP page needs code to detect this situation

 
ServletContext-Based Data Sharing
• Servlet
synchronized(this) {
ValueObject value = new ValueObject(...);
getServletContext().setAttribute("key", value);
RequestDispatcher dispatcher =
request.getRequestDispatcher
("/WEB-INF/SomePage.jsp");
dispatcher.forward(request, response);
}
• JSP 1.2
<jsp:useBean id="key" type="somePackage.ValueObject"
scope="application" />
<jsp:getProperty name="key" property="someProperty" />
• JSP 2.0
${key.someProperty}

studbean.java

package pack1;

public class studbean {
  private int roll;
  private String name;
  public studbean(int roll,String name) {
    this.name=name;
    this.roll=roll;
  
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public int getRoll() {
    return roll;
  }
  public void setRoll(int roll) {
    this.roll = roll;
  }

}
download code


index.jsp


<html>

  <body>
  <form action="controll">
  roll<input name="r"><br>
  name<input name="n">
  <br>
  Gender<input type="radio" name="g" value="male">male
     <input type="radio" name="g" value="female">female
     <input type="submit" />
  
  </form>
  
   </body>
</html> 
download code
controll.java


import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import pack1.*;

public class control1 extends HttpServlet {
  protected void doGet(HttpServletRequest request,
HttpServletResponse response
throws ServletException, IOException {
  String g=request.getParameter("g");  
   
  studbean s1=new studbean(Integer.parseInt(request.getParameter("r"))
,request.getParameter
("n"));
   request.setAttribute("stud",s1);
     String add1="aaa";
     
     if (g.equals("male"))
       add1="/male.jsp";
     
     if (g.equals("female"))
       add1="/female.jsp";
      
   RequestDispatcher dis=getServletContext().getRequestDispatcher(add1);
     dis.include(request, response);   
    
  }
}
download code

male.jsp

<%@page import="pack1.studbean"%>
<h1><center>This is male section</center></h1>

<font size=color="blue">

<jsp:useBean id="stud" type="pack1.studbean" scope="request"/>
roll is<jsp:getProperty name="stud" property="roll"/> 
<br>name is  <jsp:getProperty name="stud" property="name"/>

</font>
download code

female.jsp

<%@page import="pack1.studbean"%>
<h1><center>This is female section</center></h1>

<font size=color=blue>
<jsp:useBean id="stud" type="pack1.studbean" scope="request"/>
roll is<jsp:getProperty name="stud" property="roll"/> 
<br>name is  <jsp:getProperty name="stud" property="name"/>
</font>
download code

Download Zip Code

Output



If  it is Male it goes to male.jsp






If it is Female It goes to female.jsp