JSP Material
JSP Material
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()
{
}
2
www.laratechnology.com 080-41310124
LARA TECHNOLOGY LARA TECHNOLOGY
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() {
}
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>");
%>
<%
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:
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;
test1.jsp:
<%
Hello h1 = new Hello();
String s1 = h1.sayHello();
out.println(s1);
out.println("<br>");
%>
<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>
package com.lara;
test3.jsp:
<%
11
www.laratechnology.com 080-41310124
LARA TECHNOLOGY LARA TECHNOLOGY
out.println(p1.getFirstName() + "<br>");
out.println(p1.getEmail() + "<br>");
out.println(p1.getAge() + "<br>");
out.println(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:
<%
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
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;
16
www.laratechnology.com 080-41310124
LARA TECHNOLOGY LARA TECHNOLOGY
{
System.out.println("release");
}
}
hello.jsp:
<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;
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;
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;
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:
<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;
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;
}
}
}
22
www.laratechnology.com 080-41310124
LARA TECHNOLOGY LARA TECHNOLOGY
add.jsp:
<%
int i=100;
%>
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;
23
www.laratechnology.com 080-41310124
LARA TECHNOLOGY LARA TECHNOLOGY
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 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:
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;
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;
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;
}
}
28
www.laratechnology.com 080-41310124
LARA TECHNOLOGY LARA TECHNOLOGY
nestedTag.jsp:
<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;
@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;
<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");
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 Servlet1()
{
super();
}
33
www.laratechnology.com 080-41310124
LARA TECHNOLOGY LARA TECHNOLOGY
<!--<%
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 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;
}
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 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 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;
}
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 PersonServlet()
{
super();
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 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="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>
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 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;
44
www.laratechnology.com 080-41310124
LARA TECHNOLOGY LARA TECHNOLOGY
test11.jsp:
<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;
}
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}
${element.age}
${element.weight} </br>
</c:forEach>
Person.java:
apackage com.lara;
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 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);
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}
${element.age}
${element.weight} </br>
</c:forEach>
48
www.laratechnology.com 080-41310124