April 12, 2011

Expression Language (EL) Code Example

Write a Servlet - MyServlet.java. From this servlet, dispatch the request to a JSP - MyJsp.jsp.
Write a JavaBean - Person.java with 2 properties - name (String) and age (int).
Before dispatching, set an attribute on the request object with an instance of the bean as the attribute value.
In the JSP, use EL constructs/expressions to use the bean. Add a new property, address, to the Person bean. The address property is itself a bean with properties city (String), street (String), state (String). In the JSP retrieve the person's name, age, city and state.


In MyServlet.java set an application attribute "customers" which is an ArrayList. Each element of the ArrayList is a Person object. Add 2 different persons to the Arraylist.
In the MyJsp.jsp retrieve the states of person-2


Solution


1) Create the MyServlet.java servlet class in the src folder of the web application

package info.icontraining.servlets;

import java.io.IOException;
import java.util.*;

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

public class MyServlet extends HttpServlet {
 
   protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  
      Person person = new Person();
      person.setName("Dinesh");
      person.setAge(32);
  
      Address address = new Address();
      address.setCity("Mumbai");
      address.setState("MH");
      address.setStreet("Main Street");
  
      person.setAddress(address);
  
      List<Person> list = new ArrayList<Person>();
      list.add(person);
  
      Person person2 = new Person();
      Address address2 = new Address();
      address2.setState("KA");
      person2.setAddress(address2);
  
      list.add(person2);
  
      request.setAttribute("personBean", person);
      getServletContext().setAttribute("customers",list);
  
      RequestDispatcher rd = request.getRequestDispatcher("/MyJsp.jsp");
      rd.forward(request, response); 
   }
}


2) Configure the MyServlet class in the WebContent/WEB-INF/web.xml

<servlet>
    <servlet-name>MyServlet</servlet-name>
    <servlet-class>info.icontraining.servlets.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>MyServlet</servlet-name>
    <url-pattern>/myServlet</url-pattern>
</servlet-mapping>


3) Create the Person.java JavaBean class in the src folder of the web application

package info.icontraining.servlets;

public class Person {
 
   private String name;
   private int age;
   private Address address;
 
   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }
 
   public int getAge() {
      return age;
   } 
   public void setAge(int age) {
      this.age = age;
   }
 
   public Address getAddress() {
      return address;
   }
   public void setAddress(Address address) {
      this.address = address;
   }
}


4) Create the Address.java JavaBean class in the src folder of the web application

package info.icontraining.servlets;

public class Address {

   private String state;
   private String city;
   private String street;
 
   public String getState() {
      return state;
   }
   public void setState(String state) {
      this.state = state;
   }
 
   public String getCity() {
      return city;
   }
   public void setCity(String city) {
      this.city = city;
   }
 
   public String getStreet() {
      return street;
   }
   public void setStreet(String street) {
      this.street = street;
   }
}


5) Create the MyJsp.jsp in the WebContent folder of the web application

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<html>
<head>
<title>MyJsp</title>
</head>
<body>

Name of Person is: ${requestScope.personBean.name} <br/>
Age of Person is: ${requestScope.personBean.age} <br/>

<br/>

City of Person is: ${requestScope.personBean.address.city} <br/>
State of Person is: ${requestScope.personBean.address.state}<br/>
<br/>

State of Person-2 is: ${applicationScope.customers[1].address.state}

</body>
</html>


6) Test the code by typing the following URL in the browser

http://localhost:8080/WebAppName/myServlet


Note: If the EL expression ${ ... } does not print anything, set the isELIgnored attribute of the page directive to false as follows,

<%@ page ... isELIgnored="false" %>

No comments:

Post a Comment