0% found this document useful (0 votes)
70 views48 pages

JSP Material

The document contains code snippets from JSP files and Java files that are used to demonstrate different JSP concepts like scriptlets, implicit objects, include directive, include action tag. It shows how a JSP page is converted to a Java servlet class during compilation and how the implicit objects can be accessed from the generated servlet class. It also provides examples of including one JSP page into another using include directive and include action tag along with examples demonstrating how variables scopes work during includes.

Uploaded by

Mahesh Chowdary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views48 pages

JSP Material

The document contains code snippets from JSP files and Java files that are used to demonstrate different JSP concepts like scriptlets, implicit objects, include directive, include action tag. It shows how a JSP page is converted to a Java servlet class during compilation and how the implicit objects can be accessed from the generated servlet class. It also provides examples of including one JSP page into another using include directive and include action tag along with examples demonstrating how variables scopes work during includes.

Uploaded by

Mahesh Chowdary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 48

2012

Lara Technologies
JSP
# 102/12, 2nd Main, 5th Cross, Venkateswara College Road, Ramaiah Garden,
Chikka Adugodi, Thavarakere, Bangalore – 560029.
Phone No: 080 – 4131 0124

8/9/2012
LARA TECHNOLOGY LARA TECHNOLOGY

Hello.jsp:
<%@ page import="java.util.Date" %>
<%!
int i = 10;
public void _jspDestroy()
{
}
%>
<%
Date date = new Date();
int j = 20;
%>
Todays date is : <b> <%= date %> </b> <br>
i value is : <%= i %> <br>
j value is : <%= j %> <br>
hello_jsp.java:
package org.apache.jsp;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import java.util.Date;
public final class hello_jsp extends org.apache.jasper.runtime.HttpJspBase
implements org.apache.jasper.runtime.JspSourceDependent {
int i = 10;
public void _jspDestroy()
{
}

private static java.util.List _jspx_dependants;


public Object getDependants() {
return _jspx_dependants;
}
public void _jspService(HttpServletRequest request, HttpServletResponse
response)
throws java.io.IOException, ServletException {
JspFactory _jspxFactory = null;
PageContext pageContext = null;
HttpSession session = null;
ServletContext application = null;
ServletConfig config = null;
JspWriter out = null;
Object page = this;
JspWriter _jspx_out = null;
PageContext _jspx_page_context = null;
try {
_jspxFactory = JspFactory.getDefaultFactory();
response.setContentType("text/html");
pageContext = _jspxFactory.getPageContext(this, request, response,

2
www.laratechnology.com 080-41310124
LARA TECHNOLOGY LARA TECHNOLOGY

null, true, 8192, true);


_jspx_page_context = pageContext;
application = pageContext.getServletContext();
config = pageContext.getServletConfig();
session = pageContext.getSession();
out = pageContext.getOut();
_jspx_out = out;
out.write("\r\n");
out.write("\r\n");
out.write("\r\n");
out.write("\r\n");
Date date = new Date();
int j = 20;
out.write("\r\n");
out.write("\r\n");
out.write("Todays date is : <b> ");
out.print( date );
out.write(" </b> <br>\r\n");
out.write("i value is : ");
out.print( i );
out.write(" <br>\r\n");
out.write("j value is : ");
out.print( j );
out.write(" <br>\r\n");
} catch (Throwable t) {
if (!(t instanceof SkipPageException)){
out = _jspx_out;
if (out != null && out.getBufferSize() != 0)
out.clearBuffer();
if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
}
} finally {
if (_jspxFactory != null)
_jspxFactory.releasePageContext(_jspx_page_context);
}
}
}
HttpJspBase.java:

package org.apache.jasper.runtime;
import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.HttpJspPage;
import javax.servlet.jsp.JspFactory;

3
www.laratechnology.com 080-41310124
LARA TECHNOLOGY LARA TECHNOLOGY

import org.apache.jasper.compiler.Localizer;
public abstract class HttpJspBase extends HttpServlet
implements HttpJspPage
{
protected HttpJspBase() {
}

public final void init(ServletConfig config)


throws ServletException
{
super.init(config);
jspInit();
_jspInit();
}
public String getServletInfo() {
return Localizer.getMessage("jsp.engine.info");
}

public final void destroy() {


jspDestroy();
_jspDestroy();
}
public final void service(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException
{
_jspService(request, response);
}
public void jspInit() {
}
public void _jspInit() {
}
public void jspDestroy() {
}
protected void _jspDestroy() {
}
public abstract void _jspService(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException;
}

4
www.laratechnology.com 080-41310124
LARA TECHNOLOGY LARA TECHNOLOGY

implicit-objects:
IMPLICIT OBJECT DATA TYPE
---------------- ------------
1. request HttpServletRequest
2. response HttpServletResponse
3. out JspWriter
4. session HttpSession
5. application ServletContext
6. config ServletConfig
7. page this (Object)
8. exception Throwable
9. pageContext PageContext

test.jsp:
<%
out.println("from scriplet1<br>");
%>

<%= request.getMethod() %> <br>

<%
out.println(response.getContentType());
out.println("<br>");
%>
<%= session.getId() %>
test_jsp.java:
package org.apache.jsp;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
public final class test_jsp extends org.apache.jasper.runtime.HttpJspBase
implements org.apache.jasper.runtime.JspSourceDependent {
private static java.util.List _jspx_dependants;
public Object getDependants() {
return _jspx_dependants;
}
public void _jspService(HttpServletRequest request, HttpServletResponse
response)
throws java.io.IOException, ServletException {
JspFactory _jspxFactory = null;
PageContext pageContext = null;
HttpSession session = null;
ServletContext application = null;
ServletConfig config = null;
JspWriter out = null;
Object page = this;
JspWriter _jspx_out = null;
PageContext _jspx_page_context = null;

5
www.laratechnology.com 080-41310124
LARA TECHNOLOGY LARA TECHNOLOGY

try {
_jspxFactory = JspFactory.getDefaultFactory();
response.setContentType("text/html");
pageContext = _jspxFactory.getPageContext(this, request, response,
null, true, 8192, true);
_jspx_page_context = pageContext;
application = pageContext.getServletContext();
config = pageContext.getServletConfig();
session = pageContext.getSession();
out = pageContext.getOut();
_jspx_out = out;

out.write("\r\n");
out.write("\r\n");

out.println("from scriplet1<br>");

out.write("\r\n");
out.write("\r\n");
out.print( request.getMethod() );
out.write(" <br>\r\n");
out.write("\r\n");

out.println(response.getContentType());
out.println("<br>");

out.write("\r\n");
out.write("\r\n");
out.print( session.getId() );
} catch (Throwable t) {
if (!(t instanceof SkipPageException)){
out = _jspx_out;
if (out != null && out.getBufferSize() != 0)
out.clearBuffer();
if (_jspx_page_context != null)
_jspx_page_context.handlePageException(t);
}
} finally {
if (_jspxFactory != null)
_jspxFactory.releasePageContext(_jspx_page_context);
}
}
}
a.jsp:
1111111111<br>
<%@ include file="b.jsp" %>
2222222222

6
www.laratechnology.com 080-41310124
LARA TECHNOLOGY LARA TECHNOLOGY

b.jsp:

i am from b.jsp<br>
c.jsp:

1111111<br>
<%@ include file="d.jsp" %>
2222222 : <%= i %>
d.jsp:

i am from d.jsp.
<%
int i = 20;
%>
e.jsp:

111111<br>
<%
int i = 30;
%>
<%@ include file="f.jsp" %>
22222222

f.jsp:
i am from f.jsp : <%= i %> <br>
g.jsp:
<%
String s1 = "h.jsp";
%>
1111111111111<br>
<%@ include file="<%= s1 %>" %>
2222222222222
h.jsp:

i am from h.jsp<br>
i.jsp:

11111111<br>
<jsp:include page="j.jsp"/>
22222222
j.jsp:

i am from j.jsp <br>

7
www.laratechnology.com 080-41310124
LARA TECHNOLOGY LARA TECHNOLOGY

k.jsp:

111111<br>
<%
int i = 0;
%>
<jsp:include page="l.jsp"/>
222222222
l.jsp:
i am from l.jsp <br>
<%= i %>

m.jsp:
111111 <br>
<%
String s1 = "n.jsp";
%>
<jsp:include page="<%= s1 %>"/>
22222222
n.jsp:

i am from n.jsp<br>
a.jsp:
11111111</br>
<jsp:include page="b.jsp">
<jsp:param value="abc" name="param1"/>
<jsp:param value="20" name="param2"/>
</jsp:include>
22222222</br>
b.jsp:

i am from b.jsp</br>
<%= request.getParameter("param1") %> </br>
<%= request.getParameter("param2") %> </br>
c.jsp:

11111</br>
<jsp:include page="Servlet1">
<jsp:param value="10%" name="reduction"/>
</jsp:include>
22222</br>

8
www.laratechnology.com 080-41310124
LARA TECHNOLOGY LARA TECHNOLOGY

Servlet1.java:

package com.lara;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* Servlet implementation class Servlet1
*/
public class Servlet1 extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public Servlet1() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request,
HttpServletResponse response)
*/
protected void doGet(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
String s1 =
request.getParameter("reduction");
PrintWriter out =
response.getWriter();
out.println("from servlet: " + s1);
out.println("</br>");
}
}
e.jsp:
11111</br>
<jsp:forward page="f.jsp"/>
22222</br>

9
www.laratechnology.com 080-41310124
LARA TECHNOLOGY LARA TECHNOLOGY

f.jsp:
i am from f.jsp </br>
g.jsp:

111111111</br>
<jsp:forward page="h.jsp">
<jsp:param value="500" name="test"/>
</jsp:forward>
222222222</br>
h.jsp:

i am from h.jsp</br>
<%
String s1 = request.getParameter("test");
out.println(s1);
%>
Hello.java:

package com.lara;

public class Hello


{
public String sayHello()
{
return "Hello to all";
}
}

test1.jsp:

<%@ page import="com.lara.Hello" %>

<%
Hello h1 = new Hello();
String s1 = h1.sayHello();
out.println(s1);
out.println("<br>");
%>

<%= h1.sayHello() %>


test2.jsp:

<jsp:useBean id="h1"
class="com.lara.Hello"
scope="page"/>

<%
String s1 = h1.sayHello();

10
www.laratechnology.com 080-41310124
LARA TECHNOLOGY LARA TECHNOLOGY

out.println(s1);
%>

</br>

<%= h1.sayHello() %>


Person.java

package com.lara;

public class Person


{
private String firstName;
private String email;
private int age;
private double weight;

public String getFirstName() {


return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
}

test3.jsp:

<%@ page import="com.lara.Person" %>

<%

11
www.laratechnology.com 080-41310124
LARA TECHNOLOGY LARA TECHNOLOGY

Person p1 = new Person();


p1.setFirstName("abc");
p1.setEmail("[email protected]");
p1.setAge(33);
p1.setWeight(55.677);

out.println(p1.getFirstName() + "<br>");
out.println(p1.getEmail() + "<br>");
out.println(p1.getAge() + "<br>");
out.println(p1.getWeight() + "<br>");
%>

<%= p1.getFirstName() %> </br>


<%= p1.getEmail() %> </br>
<%= p1.getAge() %> </br>
<%= p1.getWeight() %> </br>

test4.jsp:
<%@page import="com.lara.Person"%>

<jsp:useBean id = "obj"
class = "com.lara.Person"
scope = "request" />

<%
obj.setFirstName("abc");
obj.setEmail("[email protected]");
obj.setAge(22);
obj.setWeight(66.767);

out.println(obj.getFirstName() + "<br>");
out.println(obj.getEmail() + "<br>");
out.println(obj.getAge() + "<br>");
out.println(obj.getWeight() + "<br>");
%>
<hr>
<%= obj.getFirstName() %>
<%= obj.getEmail() %>
<%= obj.getAge() %>
<%= obj.getWeight() %>
<hr>

<%
Person p1 =
(Person) request.getAttribute("obj");
out.println(p1.getFirstName() + "<br>");
out.println(p1.getAge() + "<br>");
out.println(p1.getEmail() + "<br>");

12
www.laratechnology.com 080-41310124
LARA TECHNOLOGY LARA TECHNOLOGY

out.println(p1.getWeight() + "<br>");
%>
test5.jsp:

<jsp:useBean id="p1"
class="com.lara.Person"
scope="session"/>

First Name:
<jsp:getProperty name="p1"
property="firstName" /> </br>
Email:
<jsp:getProperty name="p1"
property="email" /> </br>

Age:
<jsp:getProperty name="p1"
property="age" /> </br>

Weight:
<jsp:getProperty name="p1"
property="weight" /> </br>

<jsp:setProperty name="p1"
property="firstName"
value="abc" /> </br>

<jsp:setProperty name="p1"
property="email"
value="[email protected]" /> </br>

<jsp:setProperty name="p1"
property="weight"
value="55.09" /> </br>

<jsp:setProperty name="p1"
property="age"
value="52" /> </br>

<hr>
First Name:
<jsp:getProperty name="p1"
property="firstName" /> </br>
Age:
<jsp:getProperty name="p1"
property="age" /> </br>

13
www.laratechnology.com 080-41310124
LARA TECHNOLOGY LARA TECHNOLOGY

Weight:
<jsp:getProperty name="p1"
property="weight" /> </br>

Email:
<jsp:getProperty name="p1"
property="email" /> </br>

test6.jsp:

<%@ page import="com.lara.Person"%>

<%
Person p1 =
(Person) session.getAttribute("p1");
out.println(p1.getFirstName() + "<br>");
out.println(p1.getAge() + "<br>");
out.println(p1.getWeight() + "<br>");
out.println(p1.getEmail() + "<br>");
%>

Servlet1:
package com.lara;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
* Servlet implementation class Servlet1
*/
public class Servlet1 extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public Servlet1() {
super();
// TODO Auto-generated constructor stub
}

/**

14
www.laratechnology.com 080-41310124
LARA TECHNOLOGY LARA TECHNOLOGY

* @see HttpServlet#doGet(HttpServletRequest request,


HttpServletResponse response)
*/
protected void doGet(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
PrintWriter out =
response.getWriter();
HttpSession s1 = request.getSession();
Person obj =
(Person) s1.getAttribute("p1");
out.println(obj.getFirstName() + "<br>");
out.println(obj.getAge() + "<br>");
out.println(obj.getEmail() + "<br>");
out.println(obj.getWeight() + "<br>");
}

The Page Directive's Attributes

Attribute Value Type Default Value


language Scripting language name "java"
info String Depends on the JSP container
contentType MIME type, character set "text/html;charset=ISO-8859-1"
extends Class name None
import Fully qualified class name or package name None
buffer Buffer size or false 8192
autoFlush Boolean "true"
session Boolean "true"
isThreadSafe Boolean "true"
errorPage URL None
isErrorPage Boolean "false"

15
www.laratechnology.com 080-41310124
LARA TECHNOLOGY LARA TECHNOLOGY

HelloTag.java:

package com.lara;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.Tag;

public class HelloTag implements Tag


{
private PageContext pc=null;
public void setPageContext(PageContext arg0)
{
System.out.println("setPageContext");
pc=arg0;
}
public void setParent(Tag arg0)
{
System.out.println("setParent");
}
public Tag getParent()
{
System.out.println("getParent");
return null;
}
public int doStartTag() throws JspException
{
System.out.println("doStartTag");
JspWriter out=pc.getOut();
try
{
out.println("Hello to All");
}
catch(IOException ex)
{
ex.printStackTrace();
}
return EVAL_BODY_INCLUDE;
}
public int doEndTag() throws JspException
{
System.out.println("doEndTag");
return SKIP_PAGE;
}
public void release()

16
www.laratechnology.com 080-41310124
LARA TECHNOLOGY LARA TECHNOLOGY

{
System.out.println("release");
}
}
hello.jsp:

<%@ taglib prefix="tags" uri="/WEB-INF/custom.tld"%>

<tags:hello />

<tags:hello>
<u>Hello</u><br>
</tags:hello>

<tags:hello>
<%="Hello to All"%><br>
</tags:hello>

DateTag.java:

package com.lara;

import java.io.IOException;
import java.util.Date;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.Tag;

public class DateTag implements Tag


{
private PageContext pc=null;
public void setPageContext(PageContext arg0)
{
System.out.println("setPageContext");
pc=arg0;
}
public void setParent(Tag arg0)
{
System.out.println("setParent");
}
public Tag getParent()
{
System.out.println("getParent");
return null;
}
public int doStartTag() throws JspException

17
www.laratechnology.com 080-41310124
LARA TECHNOLOGY LARA TECHNOLOGY

{
System.out.println("doStartTag");
JspWriter out=pc.getOut();
Date date=new Date();
try
{
out.println("Todays Date is:"+date);
}
catch(IOException ex)
{
ex.printStackTrace();
}

return SKIP_BODY;
}
public int doEndTag() throws JspException
{
System.out.println("doEndTag");
return EVAL_PAGE;
}
public void release()
{
System.out.println("release");

}
}
date.jsp:
<%@ taglib prefix="tags" uri="/WEB-INF/custom.tld"%>

<tags:date/>
ParamTag.java:

package com.lara;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.Tag;

public class ParamTag implements Tag


{
private PageContext pc=null;
private String param1;
public void setPageContext(PageContext arg0)
{
System.out.println("setPageContext");

18
www.laratechnology.com 080-41310124
LARA TECHNOLOGY LARA TECHNOLOGY

pc=arg0;
}
public void setParam1(String param1)
{
System.out.println("setParam1");
this.param1 = param1;
}
public void setParent(Tag arg0)
{
System.out.println("setParent");

}
public int doStartTag() throws JspException
{
System.out.println("doStartTag");
JspWriter out=pc.getOut();
try
{
out.println("Parameter Value is:"+param1);
}
catch(IOException ex)
{
ex.printStackTrace();
}
return EVAL_BODY_INCLUDE;
}
public Tag getParent()
{
System.out.println("getParent");
return null;
}
public int doEndTag() throws JspException
{
System.out.println("doEndTag");
return SKIP_PAGE;
}
public void release()
{
System.out.println("release");

}
param.jsp:
<%@taglib prefix="tags" uri="/WEB-INF/custom.tld"%>

<tags:parameter param1="pehu">
Parameter Example

19
www.laratechnology.com 080-41310124
LARA TECHNOLOGY LARA TECHNOLOGY

<%
int j=90;
%>
</tags:parameter>
ReverseTag.java:
package com.lara;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.Tag;

public class ReverseTag implements Tag


{
private PageContext pc=null;
private String parameter;

public void setPageContext(PageContext arg0)


{
System.out.println("setPageContext");
pc=arg0;
}
public void setParameter(String parameter)
{
System.out.println("setParameter");
this.parameter = parameter;
}
public void setParent(Tag arg0)
{
System.out.println("setParent");
}
public Tag getParent()
{
System.out.println("getParent");
return null;
}
public int doStartTag() throws JspException
{
System.out.println("doStartTag");
JspWriter out=pc.getOut();
StringBuilder reverseString=new StringBuilder(parameter).reverse();
try
{
out.println("Reverse Of "+parameter+" is: "+reverseString);
}
catch(IOException ex)

20
www.laratechnology.com 080-41310124
LARA TECHNOLOGY LARA TECHNOLOGY

{
ex.printStackTrace();
}
return EVAL_BODY_INCLUDE;
}
public int doEndTag() throws JspException
{
System.out.println("doEndTag");
return SKIP_PAGE;
}
public void release()
{
System.out.println("release");
}

}
reverse.jsp:

<%@ taglib prefix="tags" uri="/WEB-INF/custom.tld"%>

<tags:revString parameter="LaraTechnology"/>
AdditionTag.java:

package com.lara;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.Tag;

public class AdditionTag implements Tag


{
private PageContext pc=null;
private int num1;
private int num2;
public void setPageContext(PageContext arg0)
{
System.out.println("setPageContext");
pc=arg0;
}
public void setParent(Tag arg0)
{
System.out.println("setParent");
}
public void setNum1(int num1)
{

21
www.laratechnology.com 080-41310124
LARA TECHNOLOGY LARA TECHNOLOGY

System.out.println("setNum1");
this.num1 = num1;
}
public void setNum2(int num2)
{
System.out.println("setNum2");
this.num2 = num2;
}

public Tag getParent()


{
System.out.println("getParent");
return null;
}
public int doStartTag() throws JspException
{
System.out.println("doStartTag");
JspWriter out=pc.getOut();
int result=0;
try {
result=num1+num2;
out.println("Sum of "+num1+" and "+num2+" is:"+result);
}
catch(IOException ex)
{
ex.printStackTrace();
}
return EVAL_BODY_INCLUDE;
}
public int doEndTag() throws JspException
{
System.out.println("doEndTag");
return EVAL_PAGE;
}
public void release()
{
System.out.println("release");

}
}

22
www.laratechnology.com 080-41310124
LARA TECHNOLOGY LARA TECHNOLOGY

add.jsp:

<%@ taglib prefix="tags" uri="/WEB-INF/custom.tld"%>

<tags:add num1="20" num2="40"/> <br>


<tags:add num1="25"/><br>

<%

int i=100;
%>

<tags:add num1="<%=i%>" num2="40"/>


MyIterationTag.java:

package com.lara;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.IterationTag;
import javax.servlet.jsp.tagext.Tag;

public class MyIterationTag implements IterationTag


{
private PageContext pc=null;
private int index;
public void setPageContext(PageContext arg0)
{
System.out.println("setPageContext");
pc=arg0;
}
public void setParent(Tag arg0)
{
System.out.println("setParent");
}
public void setIndex(int index)
{
System.out.println("setIndex");
this.index = index;
}
public Tag getParent()
{
System.out.println("getParent");
return null;
}

23
www.laratechnology.com 080-41310124
LARA TECHNOLOGY LARA TECHNOLOGY

public int doStartTag() throws JspException


{
System.out.println("doStartTag");
return EVAL_BODY_INCLUDE;
}
public int doAfterBody() throws JspException
{

JspWriter out=pc.getOut();
try {
if(index > 0)
{
System.out.println("doAfterBody");
index--;
out.println("doAfterBody</br>");
return EVAL_BODY_AGAIN;
}
}
catch (IOException ex)
{
ex.printStackTrace();
}
return SKIP_BODY;
}
public int doEndTag() throws JspException
{
System.out.println("doEndTag");
return EVAL_PAGE;
}
public void release()
{
System.out.println("release");
}
}
iteration.jsp:
<%@taglib prefix="tags" uri="/WEB-INF/custom.tld" %>

<tags:itTag index="5">
Hello</br>
</tags:itTag>

24
www.laratechnology.com 080-41310124
LARA TECHNOLOGY LARA TECHNOLOGY

BodyTagHandler.java:

package com.lara;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTag;
import javax.servlet.jsp.tagext.Tag;

public class BodyTagHandler implements BodyTag


{
private PageContext pc=null;
private BodyContent bc=null;
public void setPageContext(PageContext arg0)
{
System.out.println("setPageContext");
pc=arg0;
}
public void setParent(Tag arg0)
{
System.out.println("setParent");
}
public void setBodyContent(BodyContent arg0)
{
System.out.println("setBodyContent");
bc=arg0;
}
public Tag getParent()
{
System.out.println("getParent");
return null;
}
public int doStartTag() throws JspException
{
System.out.println("doStartTag");
return EVAL_BODY_BUFFERED;

}
public void doInitBody() throws JspException
{
System.out.println("doInitBody");
}
public int doAfterBody() throws JspException
{

25
www.laratechnology.com 080-41310124
LARA TECHNOLOGY LARA TECHNOLOGY

System.out.println("doAfterBody");
JspWriter out=bc.getEnclosingWriter();
String str=bc.getString();
str=new StringBuffer(str).reverse().toString();
try
{
out.println("Reverse of The Body Content:"+str);
}
catch(IOException ex)
{
ex.printStackTrace();
}

return SKIP_BODY;
}
public int doEndTag() throws JspException
{
System.out.println("doEndTag");

return SKIP_PAGE;
}
public void release()
{
System.out.println("release");

}
}
bodyTag.jsp:

<%@ taglib prefix="tags" uri="/WEB-INF/custom.tld"%>


<tags:bodyTag>
HI TO All
</tags:bodyTag>
MyTag:
package com.lara;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.TagSupport;

public class MyTag extends TagSupport


{
private PageContext pc=null;
@Override
public void setPageContext(PageContext pageContext)

26
www.laratechnology.com 080-41310124
LARA TECHNOLOGY LARA TECHNOLOGY

{
System.out.println("setPageContext");
pc=pageContext;
}
@Override
public int doStartTag() throws JspException
{
System.out.println("doStartTag");
JspWriter out=pc.getOut();
try
{
out.println("Hello");
}
catch(IOException ex)
{
ex.printStackTrace();
}
return SKIP_BODY;
}

}
myTag.jsp:
<%@ taglib prefix="tags" uri="/WEB-INF/custom.tld"%>

<tags:myTag/><br>
<tags:hello><br>
Hello<br>
</tags:hello>
ParentTag.java:

package com.lara;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.TagSupport;

public class ParentTag extends TagSupport


{
private String country;

public String getCountry()


{
return country;
}

public void setCountry(String country)


{
System.out.println("setCountry-parent");

27
www.laratechnology.com 080-41310124
LARA TECHNOLOGY LARA TECHNOLOGY

this.country = country;
}

@Override
public int doStartTag() throws JspException
{
System.out.println("doStartTag-parent");
return EVAL_BODY_INCLUDE;
}
}
ChildTag.java:

package com.lara;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.Tag;
import javax.servlet.jsp.tagext.TagSupport;

public class ChildTag extends TagSupport


{
private String country;
private Tag parent;
public void setCountry(String country)
{
System.out.println("setCountry-child");
this.country = country;
}
public void setParent(Tag parent)
{
this.parent = parent;
}
@Override
public int doStartTag() throws JspException
{
System.out.println("doStartTag-child");
ParentTag p1=(ParentTag)parent;
if(p1.getCountry().equals(country))
{
return EVAL_BODY_INCLUDE;
}
else
{
return SKIP_BODY;
}

}
}

28
www.laratechnology.com 080-41310124
LARA TECHNOLOGY LARA TECHNOLOGY

nestedTag.jsp:

<%@ taglib prefix="tags" uri="/WEB-INF/custom.tld"%>

<tags:parent country="India">

<tags:child country="China">
I am From China
</tags:child>
<tags:child country="US">
I am From US
</tags:child>
<tags:child country="India">
I am From India
</tags:child>
<tags:child country="Japan">
I am From Japan
</tags:child>

</tags:parent>
MyTEITag.java:

package com.lara;

import javax.servlet.jsp.tagext.TagData;
import javax.servlet.jsp.tagext.TagExtraInfo;
import javax.servlet.jsp.tagext.VariableInfo;

public class MyTEITag extends TagExtraInfo


{

@Override
public VariableInfo[] getVariableInfo(TagData data)
{
VariableInfo v1=new VariableInfo("test1", "String", true,
VariableInfo.AT_BEGIN);
VariableInfo v2=new VariableInfo("test2", "String", true,
VariableInfo.AT_BEGIN);
VariableInfo x[]={v1,v2};
return x;
}
}

29
www.laratechnology.com 080-41310124
LARA TECHNOLOGY LARA TECHNOLOGY

TestTag.java:

package com.lara;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.TagSupport;

public class TestTag extends TagSupport


{
private PageContext pc=null;
@Override
public void setPageContext(PageContext pageContext)
{
pc=pageContext;
}
@Override
public int doStartTag() throws JspException
{
pc.setAttribute("test1", "Lara");
pc.setAttribute("test2", "Technologies");
return EVAL_BODY_INCLUDE;
}
}
teiTag.jsp:

<%@ taglib prefix="tags" uri="/WEB-INF/custom.tld"%>

<tags:tTag>
<%=test1%></br>
</tags:tTag>
<%=test2%>
custom.tld:
<taglib>
<jsp-version>2.0</jsp-version>
<tlib-version>1.1</tlib-version>

<tag>
<name>hello</name>
<tag-class>com.lara.HelloTag</tag-class>
<bodycontent>tagdependent</bodycontent>
</tag>
<tag>
<name>date</name>
<tag-class>com.lara.DateTag</tag-class>
<bodycontent>empty</bodycontent>
</tag>
<tag>

30
www.laratechnology.com 080-41310124
LARA TECHNOLOGY LARA TECHNOLOGY

<name>parameter</name>
<tag-class>com.lara.ParamTag</tag-class>
<attribute>
<name>param1</name>
<required>true</required>
</attribute>
<bodycontent>scriptless</bodycontent>
</tag>
<tag>
<name>add</name>
<tag-class>com.lara.AdditionTag</tag-class>
<attribute>
<name>num1</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<bodycontent>jsp</bodycontent>
<attribute>
<name>num2</name>
<required>false</required>
</attribute>
</tag>
<tag>
<name>revString</name>
<tag-class>com.lara.ReverseTag</tag-class>
<attribute>
<name>parameter</name>
<required>true</required>
</attribute>
</tag>
<tag>
<name>itTag</name>
<tag-class>com.lara.MyIterationTag</tag-class>
<attribute>
<name>index</name>
<required>true</required>
</attribute>
</tag>
<tag>
<name>bodyTag</name>
<tag-class>com.lara.BodyTagHandler</tag-class>
</tag>
<tag>
<name>myTag</name>
<tag-class>com.lara.MyTag</tag-class>
</tag>
<tag>
<name>parent</name>

31
www.laratechnology.com 080-41310124
LARA TECHNOLOGY LARA TECHNOLOGY

<tag-class>com.lara.ParentTag</tag-class>
<attribute>
<name>country</name>
<required>true</required>
</attribute>
</tag>
<tag>
<name>child</name>
<tag-class>com.lara.ChildTag</tag-class>
<attribute>
<name>country</name>
<required>true</required>
</attribute>
</tag>
<tag>
<name>tTag</name>
<tag-class>com.lara.TestTag</tag-class>
<tei-class>com.lara.MyTEITag</tei-class>
</tag>
</taglib>
Expression Language:
test.jsp:

${4+9}<br>
${9-4}<br>
${90*2}<br>
${90/2}<br>
${90%2}
test1.jsp:
${100==200}<br>
${300}<br>
${"abc"}
test2.jsp

<%
pageContext.setAttribute("msg","from PC");
request.setAttribute("msg","From Request");
session.setAttribute("msg","From Session");
application.setAttribute("msg","From Application");

%>
${msg}
implicitEl.jsp:
${param.abc}</br>
${param.firstName}</br>

32
www.laratechnology.com 080-41310124
LARA TECHNOLOGY LARA TECHNOLOGY

implicitEl1.jsp:
${paramValues.firstName[0]}</br>
${paramValues.firstName[1]}
implicitEl2.jsp:

${header.accept}
implicitEl3.jsp:
<%
Cookie c1=new Cookie("firstName","abc");

Cookie c2=new Cookie("lastName","xyz");

response.addCookie(c1);
response.addCookie(c2);
%>
implicitEl4.jsp:

${cookie.firstName.value}</br>
${cookie.lastName.value}</br>
implicitEl5.jsp:
${initParam.con1}</br>
${initParam.con2}</br>
implicitEl6.jsp:

${pageContext.session.id}</br>
${pageContext.response.contentType}</br>
${pageContext.request.method}
Servlet1.java:

package com.lara;

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;

public class Servlet1 extends HttpServlet


{
private static final long serialVersionUID = 1L;

public Servlet1()
{
super();
}

33
www.laratechnology.com 080-41310124
LARA TECHNOLOGY LARA TECHNOLOGY

protected void doGet(HttpServletRequest request, HttpServletResponse


response) throws ServletException, IOException
{
request.setAttribute("msg", "This Message is From Servlet1");
RequestDispatcher rd=request.getRequestDispatcher("fromServlet1.jsp");
rd.forward(request, response);
}
}
fromServlet1.jsp:
${msg}<br>

<!--<%
String str=(String)request.getAttribute("msg");
out.println(str);
%>-->
Servlet2.java:
package com.lara;

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;

public class Servlet2 extends HttpServlet


{
private static final long serialVersionUID = 1L;

public Servlet2()
{
super();

}
protected void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException
{
int x[]={10,20,30,40,50};
request.setAttribute("msg", x);
RequestDispatcher rd=request.getRequestDispatcher("fromServlet2.jsp");
rd.forward(request, response);
}

34
www.laratechnology.com 080-41310124
LARA TECHNOLOGY LARA TECHNOLOGY

fromServlet2.jsp:

${msg[0]}</br>
${msg[1]}</br>
${msg[2]}</br>
${msg[3]}</br>
${msg[4]}</br>
${msg[5]}</br>
${msg[6]}</br>
${msg[7]}</br>
${msg[8]}
Servlet3.java:
package com.lara;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Servlet3 extends HttpServlet


{
private static final long serialVersionUID = 1L;
public Servlet3()
{
super();

}
protected void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException
{
ArrayList<String> list=new ArrayList<String>();
list.add("abc");
list.add("xyz");
list.add("hello");
list.add("test");
request.setAttribute("msg", list);
RequestDispatcher
rd=request.getRequestDispatcher("fromServlet3.jsp");
rd.forward(request, response);
}

35
www.laratechnology.com 080-41310124
LARA TECHNOLOGY LARA TECHNOLOGY

fromServlet3.jsp:
${msg[0]}</br>
${msg[1]}</br>
${msg[2]}</br>
${msg[3]}</br>
${msg[4]}</br>
${msg[5]}</br>
${msg[6]}</br>
${msg[7]}</br>
${msg[8]}
Servlet4.java
package com.lara;

import java.io.IOException;
import java.util.HashMap;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Servlet4 extends HttpServlet


{
private static final long serialVersionUID = 1L;

public Servlet4()
{
super();

}
protected void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException
{
HashMap<String, String> map=new HashMap<String, String>();
map.put("name", "test");
map.put("age", "22");
map.put("weight", "56.90");
request.setAttribute("msg", map);
RequestDispatcher
rd=request.getRequestDispatcher("fromSerlet4.jsp");
rd.forward(request, response);

36
www.laratechnology.com 080-41310124
LARA TECHNOLOGY LARA TECHNOLOGY

fromSerlet4.jsp":

${msg.name}</br>
${msg.age}</br>
${msg.weight}
Servlet5.java:

package com.lara;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Servlet5 extends HttpServlet {


private static final long serialVersionUID = 1L;

public Servlet5()
{
super();

}
protected void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException
{
ArrayList list=new ArrayList();
HashMap<String, String> map1=new HashMap<String, String>();
map1.put("name", "kiran");
map1.put("age", "25");
map1.put("weight", "59.90");
list.add(map1);
HashMap<String, String> map2=new HashMap<String, String>();
map2.put("name", "vijay");
map2.put("age", "28");
map2.put("weight", "65.90");
list.add(map2);
request.setAttribute("msg", list);
RequestDispatcher
rd=request.getRequestDispatcher("fromServlet5.jsp");
rd.forward(request, response);
}

37
www.laratechnology.com 080-41310124
LARA TECHNOLOGY LARA TECHNOLOGY

}
fromServlet5.jsp
${msg[0].name}</br>
${msg[0].age}</br>
${msg[0].weight}</br>
${msg[1].name}</br>
${msg[1].age}</br>
${msg[1].weight}
Person.java:
package com.lara;

public class Person


{
private String name;
private int age;
private double weigth;
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 double getWeigth() {
return weigth;
}
public void setWeigth(double weigth) {
this.weigth = weigth;
}

}
PersonServlet.java:

package com.lara;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

38
www.laratechnology.com 080-41310124
LARA TECHNOLOGY LARA TECHNOLOGY

public class PersonServlet extends HttpServlet


{
private static final long serialVersionUID = 1L;

public PersonServlet()
{
super();

protected void doGet(HttpServletRequest request, HttpServletResponse


response) throws ServletException, IOException
{

ArrayList<Person> list=new ArrayList<Person>();


Person p1=new Person();
p1.setName("manu");
p1.setAge(22);
p1.setWeigth(56.90);
list.add(p1);
Person p2=new Person();
p2.setName("sai");
p2.setAge(25);
p2.setWeigth(68.90);
list.add(p2);
request.setAttribute("msg", list);
RequestDispatcher
rd=request.getRequestDispatcher("fromPerson.jsp");
rd.forward(request, response);
}

fromPerson.jsp:
${msg[0].name}</br>
${msg[0].age}</br>
${msg[0]. weigth }</br>
${msg[1].name}</br>
${msg[1].age}</br>
${msg[1]. weigth }

isElIgnore.jsp:
<%@ page isELIgnored="false"%>

${10+90}

39
www.laratechnology.com 080-41310124
LARA TECHNOLOGY LARA TECHNOLOGY

Web.xml:
<context-param>
<param-name>con1</param-name>
<param-value>100%</param-value>
</context-param>
<context-param>
<param-name>con2</param-name>
<param-value>200%</param-value>
</context-param>
<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<el-ignored>true</el-ignored>
<scripting-invalid>true</scripting-invalid>
</jsp-property-group>
</jsp-config>
JSTL:
test.jsp:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<c:out value="Hello"/></br>
<c:out value="${10+90}"/>
test1.jsp:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<%
request.setAttribute("msg","Hello To All");
%>

<c:out value="${msg}"/>
Servlet1:
package com.lara;

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;

public class Servlet1 extends HttpServlet


{
private static final long serialVersionUID = 1L;

public Servlet1()
{
super();

40
www.laratechnology.com 080-41310124
LARA TECHNOLOGY LARA TECHNOLOGY

}
protected void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException
{
request.setAttribute("msg", "From Servlet1");
RequestDispatcher
rd=request.getRequestDispatcher("test2.jsp");//test3.jsp,test4.jsp
rd.forward(request, response);
}

}
test2.jsp:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<c:out value="${msg}">
No Value to The Msg
</c:out></br>

<c:out value="${hello}">
No Value to The Hello
</c:out>
test3.jsp:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<c:out value="${msg}"
default="No Value to The msg"/>

<c:out value="${hello}"
default="No Value to The hello"/>

test4.jsp:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<c:out value="${msg}">

<jsp:include page="noMsg.txt"/>
</c:out></br>
<c:out value="${hello}">

<jsp:include page="noHello.txt"/>
</c:out>
noMsg.txt:
NO Value to The msg
noHello.txt:
No value to the hello

41
www.laratechnology.com 080-41310124
LARA TECHNOLOGY LARA TECHNOLOGY

test5.jsp:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<c:if test="true">
Hello
</c:if></br>

<c:if test="${100==100}" var="con1">


Executed
</c:if></br>

<c:if test="false">
Hi
</c:if></br>

<c:if test="${con1}">
Test
</c:if>
test6.jsp:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<c:catch var="ex">

<%
String s1=null;
out.println(s1.length());
%>

</c:catch>

Exception Message is: ${ex}

test7.jsp:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<c:choose>
<c:when test="false">
11111
</c:when>
<c:when test="false">
33333
</c:when>
<c:otherwise>
44444
</c:otherwise>
</c:choose>
test8.jsp:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

42
www.laratechnology.com 080-41310124
LARA TECHNOLOGY LARA TECHNOLOGY

<c:forTokens items="abc;hello;123;done"
delims=";"
var="element"
varStatus="row">

${row.count}.${element}</br>
<!-- ${row.index}.${element}</br> -->

</c:forTokens>
test9.jsp:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<%
int x[]={100,20,30,50,5};
request.setAttribute("list",x);
%>

<c:forEach items="${list}"
var="element"
varStatus="row">

${row.count}.${element}</br>
<!-- ${row.index}.${element}</br> -->

</c:forEach>
Servlet2.java:
package com.lara;

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;

public class Servlet2 extends HttpServlet


{
private static final long serialVersionUID = 1L;

public Servlet2()
{
super();

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

43
www.laratechnology.com 080-41310124
LARA TECHNOLOGY LARA TECHNOLOGY

String x[]={"abc","done","hello","java"};
request.setAttribute("list", x);
RequestDispatcher rd=request.getRequestDispatcher("test10.jsp");
rd.forward(request, response);
}

}
test10.jsp:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<c:forEach items="${list}"
var="element"
varStatus="row">

${row.count}.${element}</br>
</c:forEach>
Servlet3:
package com.lara;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Servlet3 extends HttpServlet


{
private static final long serialVersionUID = 1L;
public Servlet3()
{
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException
{
ArrayList list=new ArrayList();
list.add("abc");
list.add("xyz");
list.add(90);
list.add(910);
list.add(290.90);
request.setAttribute("list", list);
RequestDispatcher rd=request.getRequestDispatcher("test11.jsp");
rd.forward(request, response);

44
www.laratechnology.com 080-41310124
LARA TECHNOLOGY LARA TECHNOLOGY

test11.jsp:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<c:forEach items="${list}"
var="element"
varStatus="row">

${row.count}.${element}</br>

</c:forEach>

Servlet4.java:
package com.lara;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Servlet4 extends HttpServlet {


private static final long serialVersionUID = 1L;
public Servlet4()
{
super();

}
protected void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException
{
ArrayList list=new ArrayList();
HashMap<String, String> map1=new HashMap<String,String>();
map1.put("name", "sreekanth");
map1.put("age", "17");
map1.put("weight", "50");
list.add(map1);
HashMap<String, String> map2=new HashMap<String, String>();

45
www.laratechnology.com 080-41310124
LARA TECHNOLOGY LARA TECHNOLOGY

map2.put("name", "hari");
map2.put("age","25");
map2.put("weight", "59");
list.add(map2);
request.setAttribute("list", list);
RequestDispatcher rd=request.getRequestDispatcher("test12.jsp");
rd.forward(request, response);

test12.jsp:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<c:forEach items="${list}"
var="element"
varStatus="row">

${row.count}.${element.name}&nbsp;&nbsp;&nbsp;
${element.age}&nbsp;&nbsp;&nbsp;
${element.weight}&nbsp;&nbsp;&nbsp;</br>

</c:forEach>

Person.java:
apackage com.lara;

public class Person


{
private String name;
private int age;
private double weight;
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;

46
www.laratechnology.com 080-41310124
LARA TECHNOLOGY LARA TECHNOLOGY

}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}

}
Servlet5.java:
package com.lara;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Servlet5 extends HttpServlet


{
private static final long serialVersionUID = 1L;

public Servlet5()
{
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException
{
ArrayList<Person> list=new ArrayList<Person>();
Person p1=new Person();
p1.setName("jhon");
p1.setAge(24);
p1.setWeight(59.90);
list.add(p1);

Person p2=new Person();


p2.setName("manu");
p2.setAge(22);
p2.setWeight(56.5);

list.add(p2);

request.setAttribute("list", list);

47
www.laratechnology.com 080-41310124
LARA TECHNOLOGY LARA TECHNOLOGY

RequestDispatcher rd=request.getRequestDispatcher("test13.jsp");
rd.forward(request, response);

}
}

test13.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<c:forEach items="${list}"
var="element"
varStatus="row">

${row.count}. ${element.name}&nbsp;&nbsp;
${element.age}&nbsp;&nbsp;
${element.weight}&nbsp;&nbsp;</br>

</c:forEach>

48
www.laratechnology.com 080-41310124

You might also like