Expression language (EL) simplifies the process of accessing data from bean properties and from implicit objects.
Syntax
${expression}
1. param
2. paramValues
3. header
4. headerValues
5. cookie
6. initParam
7. pageContext
8. pageScope
9. requestScope
10. sessionScope
11. applicationScope
param and paramValues
These two EL implicit objects are used to collect the request parameters.
When you use param, then internally request.getParameter() method will be called.
When you use paramValues, then internally request.getParameterValues() method will be called.
Usage
${param.email}
${param.phone}
${paramValues.phone[0]}
${paramValues.phone[1]}
header and headerValues
These two EL implicit objects are used to collect the request Headers.
When you use header, then internally request.getHeader() method will be called.
When you use headerValues, then internally request.getHeaderValues() method will be called.
Usage
${header.host}
${header.referer}
Cookie
This EL implicit object is used to collect the request Cookies
Usage
${cookie.<cookieName>.value}
initParam
This EL implicit object is used to collect the context parameters from the ServletContext object.
Usage
${initParam.state}
${initParam.city}
pageContext
This EL implicit object allows access to all other JSP implicit objects and their properties.
Usage
${pageContext.session.id}
${pageContext.session.id.length}
${pageContext.request.remoteAddr}
${pageContext.request.method}
${pageContext.request.requestURI}
${pageContext.response.contentType}
Files Required for Program
1. Lab.jsp
2. show.jsp
3. LabServlet.java
4. web.xml
1. lab.jsp
<!DOCTYPE html>
<html>
<body>
<h3> I am Lab.jsp </h3>
<form action="myLab.jtc" method="post">
<table>
<tr>
<td> Name </td>
<td> <input type="text" name="name"/> </td>
</tr>
<tr>
<td> Email </td>
<td> <input type="text" name="email"/> </td>
</tr>
<tr>
<td> Phone 1</td>
<td> <input type="text" name="phone"/> </td>
</tr>
<tr>
<td> Phone 2</td>
<td> <input type="text" name="phone"/> </td>
</tr>
<tr>
<td> <input type="submit" value="Submit Now"/> </td>
</tr>
</table>
</form>
</body>
</html>
2. show.jsp
<!DOCTYPE html>
<html>
<body>
<h4> 1. Request Parameters </h4>
<h4> Name : ${param.name}</h4>
<h4> Email : ${param.email}</h4>
<h4> Phone 1: ${param.phone}</h4>
<h4> Phone 1 : ${paramValues.phone[0]}</h4>
<h4> Phone 2 : ${paramValues.phone[1]}</h4>
<h4> 2. Request Headers </h4>
<h4> Host : ${header.host}</h4>
<h4> referer : ${header.referer}</h4>
<h4> Connection : ${header.connection}</h4>
<h4> Connection : ${headerValues.connection[0]}</h4>
<h4> 3. Request Cookies </h4>
<h4> JSID : ${cookie.JSESSIONID}</h4>
<h4> JSID : ${cookie.JSESSIONID.value}</h4>
<h4> Email : ${cookie.Email.value}</h4>
<h4> Phone : ${cookie.Phone.value}</h4>
<h4> 4. Context Params</h4>
<h4> City : ${initParam.City}</h4>
<h4> State : ${initParam.State}</h4>
<h4> 5. pageContext to access other Info</h4>
<h4> SID : ${pageContext.session.id}</h4>
<h4> CreationTime : ${pageContext.session.creationTime}</h4>
<h4> lastAccessedTime : ${pageContext.session.lastAccessedTime}</h4>
<h4> maxInactiveInterval : ${pageContext.session.maxInactiveInterval}</h4>
<h4> Request URI : ${pageContext.request.requestURI}</h4>
<h4>Method : ${pageContext.request.method}</h4>
<h4>IP : ${pageContext.request.remoteAddr}</h4>
<h4>IP : ${pageContext.request.contextPath}</h4>
<h4> contentType : ${pageContext.response.contentType}</h4>
<h4>characterEncoding : ${pageContext.response.characterEncoding}</h4>
<h4>bufferSize : ${pageContext.response.bufferSize}</h4>
</body>
</html>
3. LabServlet.java
package com.jtcindia.servlets;
import java.io.IOException;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(name = "myLab", urlPatterns = "/myLab.jtc")
public class LabServlet extends HttpServlet {
public void service(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
//1.Request Params
String name = request.getParameter("name");
System.out.println(name);
String email = request.getParameter("email");
System.out.println(email);
String phone = request.getParameter("phone");
System.out.println(phone);
String myphones[] = request.getParameterValues("phone");
for (String myphone: myphones) {
System.out.println(myphone);
//2.Request Headers
String host = request.getHeader("host");
System.out.println(host);
String referer = request.getHeader("referer");
System.out.println(referer);
String lang = request.getHeader("accept-language");
System.out.println(lang);
Enumeration < String > enms = request.getHeaders("accept-language");
List < String > mylangs = Collections.list(enms);
for (String mylang: mylangs) {
System.out.println(mylang);
}
//3.Request Cookies
Cookie cks[] = request.getCookies();
if (cks != null) {
for (Cookie ck: cks) {
String cname = ck.getName();
if (cname.equals("JSESSIONID")) {
String cvalue = ck.getValue();
System.out.println(cname + " : " + cvalue);
}
}
}
Cookie ck1 = new Cookie("Email", email);
response.addCookie(ck1);
Cookie ck2 = new Cookie("Phone", phone);
response.addCookie(ck2);
//4.Context Params
ServletConfig config = getServletConfig();
ServletContext context = config.getServletContext();
String city = context.getInitParameter("City");
System.out.println(city);
String state = context.getInitParameter("State");
System.out.println(state);
RequestDispatcher rd = request.getRequestDispatcher("show.jsp");
rd.forward(request, response);
}
}
}
4. web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app …>
<display-name>Lab14</display-name>
<context-param>
<param-name>City</param-name>
<param-value>Noida</param-value>
</context-param>
<context-param>
<param-name>State</param-name>
<param-value>KA</param-value>
</context-param>
<welcome-file-list>
<welcome-file>Lab.jsp</welcome-file>
</welcome-file-list>
</web-app>
pageScope,requestScope,sessionScope & applicationScope
In the servlet class, you can store the data as an attribute.
In JSP, you can collect the attribute data and display that data.
We can store the following Types of data as attribute in required Scope.
Simple Types
Ex: Primities , Wrappers, String , Date
User Defined Types
Ex: Customer , Account, Book, Order
Collection of Simple Types (***JSTL***)
Ex: List of Primities
List of Wrappers
List of Strings
Collection of Collection of Simple Types (***JSTL***)
Ex: List of List of Primities
List of List of Wrappers
List of List of Strings
Collection of User Defined Types (***JSTL***)
Ex: List of Account
List of Customer
Collection of Collection of User Defined Types (***JSTL***)
Ex: List of List of Account
List of List of Customer
Map
Collection of Maps (***JSTL***)
Ex: List of Maps
Set of Maps
Strings,Wrappers and Date
In servlet | In JSP |
---|---|
req.setAttribute("EM","som@jtc"); ses.setAttribute("PH",new Long(12345)); ctx.setAttribute("DOB",new Date()); |
${requestScope.EM} ${EM} ${sessionScope.PH} ${PH} ${applicationScope.DOB} ${DOB} |
Collection of Strings, Wrappers and Date
In servlet | In JSP |
---|---|
ArrayList al=new ArrayList(); al.add(new Integer(123)); ... String str[]={"dd","ss",...}; req.setAttribute("AL",al); ses.setAttribute("STR",str); |
${requestScope.AL[0]} ${requestScope.AL[1]} ... ${sessionScope.STR[0]} ${sessionScope.STR[1]} .. |
Note: There is a problem with the above expressions because they use the index directly. You can solve that problem with JSTL tags.
Custom or User defined class object
In servlet | In JSP |
---|---|
class Address{ String street String city } class Customer{ int cid String cname String email Address address; } ses.setAttribute("CUST",cust); |
Cid : ${sessionScope.CUST.cid} Cname : ${sessionScope.CUST.cname} Email : ${sessionScope.CUST.email} Street: ${sessionScope.CUST.address.street} City: ${sessionScope.CUST.address.city} |
Collection of User defined class object
In servlet | In JSP |
---|---|
ArrayList al=new ArrayList(); al.add(cust1); al.add(cust2); ses.setAttribute("AL",al); |
Cid : ${AL[0].cid} Cname : ${sessionScope.AL[0].cname} Email : ${sessionScope.AL[0].email} Street: ${sessionScope.AL[0].address.street} City: ${sessionScope.AL[0].address.city} |
Map object
In servlet | In JSP |
---|---|
HashMap hm=new HashMap(); hm.put("sid","11"); hm.put("sname","som"); ses.setAttribute("HM",hm); |
Sid : ${sessionScope.HM["sid"]} Sname : ${sessionScope.HM["sname"]} |
Files Required For Program
1. Lab.jsp
2. show.jsp
3. test.jsp
4. Address.java
5. Account.java
6. Customer.java
7. LabServlet.java
8. web.xml
1. lab.jsp
<!DOCTYPE html>
<html>
<body>
<h3> I am Lab.jsp </h3>
<form action="myLab.jtc">
<input type="submit" value="Click Here for EL Lab"/>
</form>
</body>
</html>
2. show.jsp
<!DOCTYPE html>
<html>
<body>
<h3> I am show.jsp </h3>
<h4> 1. Simple Types </h4>
<h4> Sid : ${SID}</h4>
<h4> Sname : ${SNAME}</h4>
<h4> Phone : ${PHONE} </h4>
<hr>
<h4> Sid : ${requestScope.SID}</h4>
<h4> Sname : ${sessionScope.SNAME}</h4>
<h4> Phone : ${applicationScope.PHONE} </h4>
<hr>
<h4> 2. User Defined Types </h4>
<h4> Cust Id : ${requestScope.MyCust.cid}</h4>
<h4> Cname : ${requestScope.MyCust.cname}</h4>
<h4> Email : ${requestScope.MyCust.email}</h4>
<h4>Atype : ${requestScope.MyCust.account.atype}</h4>
<h4> Balance : ${requestScope.MyCust.account.balance}</h4>
<h4> Street : ${requestScope.MyCust.account.address.street}</h4>
<h4> City : ${requestScope.MyCust.account.address.city}</h4>
<h4> State : ${requestScope.MyCust.account.address.state}</h4>
<hr>
<h4> 3. Collection of Simple Types (*** JSTL***)</h4>
<h4> ${sessionScope.MyCourseList[0]}</h4>
<h4> ${sessionScope.MyCourseList[1]}</h4>
<h4> ${sessionScope.MyCourseList[2]}</h4>
<h4> ${sessionScope.MyCourseList[3]}</h4>
<h4> ${sessionScope.MyCourseList[4]}</h4>
<h4> 4. Collection of Collection of Simple Types (*** JSTL***)</h4>
<h4> First Batch Phones</h4>
<h4> ${applicationScope.MyPhonesList[0]}</h4>
<h4> ${applicationScope.MyPhonesList[0][0]}</h4>
<h4> ${applicationScope.MyPhonesList[0][1]}</h4>
<h4> ${applicationScope.MyPhonesList[0][2]}</h4>
<h4> Second Batch Phones</h4>
<h4> ${applicationScope.MyPhonesList[1]}</h4>
<h4> ${applicationScope.MyPhonesList[1][0]}</h4>
<h4> ${applicationScope.MyPhonesList[1][1]}</h4>
<h4> ${applicationScope.MyPhonesList[1][2]}</h4>
<hr>
<h4> 5.List of User Defined Types (*** JSTL***) </h4>
<h4> Cust Id : ${requestScope.MyCustList[0].cid}</h4>
<h4> Cname : ${requestScope.MyCustList[0].cname}</h4>
<h4> Email : ${requestScope.MyCustList[0].email}</h4>
<h4> Phone : ${requestScope.MyCustList[0].phone}</h4>
<h4> Accno : ${requestScope.MyCustList[0].account.accno}</h4>
<h4>Atype : ${requestScope.MyCustList[0].account.atype}</h4>
<h4> Balance : ${requestScope.MyCustList[0].account.balance}</h4>
<h4> Street : ${requestScope.MyCustList[0].account.address.street}</h4>
<h4> City : ${requestScope.MyCustList[0].account.address.city}</h4>
<h4> State : ${requestScope.MyCustList[0].account.address.state}</h4>
<hr>
<h4> 7.Map </h4>
<h4> C-101 : ${sessionScope.MyCourseMap["C-101"]}</h4>
<h4> C-102 : ${sessionScope.MyCourseMap["C-102"]}</h4>
<h4> C-103 : ${sessionScope.MyCourseMap["C-103"]}</h4>
<h4> C-104 : ${sessionScope.MyCourseMap["C-104"]}</h4>
<h4> 5.List of User Defined Types (*** JSTL***) </h4>
<h4> Cust Id : ${requestScope.MyCustList[0].cid}</h4>
<h4> Cname : ${requestScope.MyCustList[0].cname}</h4>
<h4> Email : ${requestScope.MyCustList[0].email}</h4>
<h4> Phone : ${requestScope.MyCustList[0].phone}</h4>
<h4> Accno : ${requestScope.MyCustList[0].account.accno}</h4>
<h4>Atype : ${requestScope.MyCustList[0].account.atype}</h4>
<h4> Balance : ${requestScope.MyCustList[0].account.balance}</h4>
<h4> Street : ${requestScope.MyCustList[0].account.address.street}</h4>
<h4> City : ${requestScope.MyCustList[0].account.address.city}</h4>
<h4> State : ${requestScope.MyCustList[0].account.address.state}</h4>
<hr>
<h4> 7.Map </h4>
<h4> C-101 : ${sessionScope.MyCourseMap["C-101"]}</h4>
<h4> C-102 : ${sessionScope.MyCourseMap["C-102"]}</h4>
<h4> C-103 : ${sessionScope.MyCourseMap["C-103"]}</h4>
<h4> C-104 : ${sessionScope.MyCourseMap["C-104"]}</h4>
</body>
</html>
3. test.jsp
<!DOCTYPE html>
<html>
<body>
<h3> I am test.jsp </h3>
<h4> ${10 > 20} </h4>
<h4> ${10 >= 20} </h4>
<h4> ${10 < 20} </h4>
<h4> ${10 <= 20} </h4>
<h4> ${10 == 10} </h4>
<h4> ${10 != 10} </h4>
<hr>
<h4> ${10 gt 20} </h4>
<h4> ${10 ge 20} </h4>
<h4> ${10 lt 20} </h4>
<h4> ${10 le 20} </h4>
<h4> ${10 eq 10} </h4>
<h4> ${10 ne 10} </h4>
</body>
</html>
4. Address.java
package com.jtcindia.servlets;
public class Address {
private String street;
private String city;
private String state;
//Constrcutors
//Setters and Getters
//toString() method
}
5. Account.java
package com.jtcindia.servlets;
public class Account {
private int accno;
private String atype;
private double balance;
private Address address;
//Constrcutors
//Setters and Getters
//toString() method
}
6. Customer.java
package com.jtcindia.servlets;
public class Customer {
private int cid;
private String cname;
private String email;
private long phone;
private Account account;
//Constrcutors
//Setters and Getters
//toString() method
}
7. LabServlet.java
package com.jtcindia.servlets;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.WebServlet;
@WebServlet(name = "myLab", urlPatterns = "/myLab.jtc")
public class LabServlet extends HttpServlet {
public void service(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
HttpSession session = request.getSession();
ServletContext context = session.getServletContext();
//1)Simple Types
int sid = 101;
String sname = "SomPrakash";
Long phone = 12345 L;
request.setAttribute("SID", sid);
session.setAttribute("SNAME", sname);
context.setAttribute("PHONE", phone);
//2)User Defined Types
Address address = new Address("SectorD", "Noida", "U.P");
Account account = new Account(5001, "SA", 25000, address);
Customer mycust = new Customer(101, "SomPrakash", "som@jtc", 12345, account);
request.setAttribute("MyCust", mycust);
session.setAttribute("MyCust", mycust);
context.setAttribute("MyCust", mycust);
//3)Collection of Simple Types
List < String > courses = new ArrayList < > ();
courses.add("Java Full Stack");
courses.add("Java Developer");
courses.add("Spring Boot");
courses.add("Angular");
courses.add("React JS");
request.setAttribute("MyCourseList", courses);
session.setAttribute("MyCourseList", courses);
context.setAttribute("MyCourseList", courses);
//4) Collection of Collection of Simple Types
List < Integer > phones1 = new ArrayList < > ();
phones1.add(111);
phones1.add(222);
phones1.add(333);
List < Integer > phones2 = new ArrayList < > ();
phones2.add(555);
phones2.add(666);
phones2.add(777);
List < List < Integer >> myphones = new ArrayList < > ();
myphones.add(phones1);
myphones.add(phones2);
request.setAttribute("MyPhonesList", myphones);
session.setAttribute("MyPhonesList", myphones);
context.setAttribute("MyPhonesList", myphones);
// 5) Collection of User Defined Types
List < Customer > mycustomers = new ArrayList < > ();
mycustomers.add(mycust);
mycustomers.add(mycust);
request.setAttribute("MyCustList", mycustomers);
session.setAttribute("MyCustList", mycustomers);
context.setAttribute("MyCustList", mycustomers);
//6. map
Map < String, String > mymap = new HashMap < > ();
mymap.put("C-101", "DevOps");
mymap.put("C-102", "Spring Boot MS");
mymap.put("C-103", "Angular");
mymap.put("C-104", "React");
request.setAttribute("MyCourseMap", mymap);
session.setAttribute("MyCourseMap", mymap);
context.setAttribute("MyCourseMap", mymap);
System.out.println("Done !!!");
RequestDispatcher rd = request.getRequestDispatcher("show.jsp");
rd.forward(request, response);
}
}
8. web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app …>
<display-name>Lab15</display-name>
<welcome-file-list>
<welcome-file>Lab.jsp</welcome-file>
</welcome-file-list>
</web-app>