JSP (Udpate1)
JSP (Udpate1)
JSP (Udpate1)
%>
</body>
</html>
2nd request:
request processed: service
3rd request:
JSP page destroyed: destroy
JSP page is loaded-static block
JSP page initialized: non-static block
JSP page instantiated: constructor
JSP page initialized
*******request processed: service
Test.jsp:
<html>
<body bgcolor='yellow'>
<%! public void init(){
System.out.println("this init method");
ServletConfig sc = getServletConfig();
System.out.println("servletconfig: "+sc.hashCode());
System.out.println("currentobject: "+this.hashCode());
String studentName = sc.getInitParameter("sname");
System.out.println("StudentName: "+studentName);
} %>
<%
%>
</body>
</html>
Web.xml:
<web-app>
<servlet>
<servlet-name>ts</servlet-name>
<jsp-file>/Test.jsp</jsp-file>
<init-param>
<param-name>sname</param-name>
<param-value>mani</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>ts</servlet-name>
<url-pattern>/Test.jsp</url-pattern>
</servlet-mapping>
</web-app>
Whenever we configure jsp file information in web.xml
file, meanwhile of deploying our project into server, the
following phases will automatically executing, those are
a. Verification phase
b. Translation phase
c. Compilation phase
d. Configuration phase
e. Loading phase
f. Instantiation phase
g. Initialization phase (executing jspInit method).
and finally waiting from user request for
executing servicing phase.
Without <servlet-mapping>
Test.jsp:
Same as above program
Web.xml:
<web-app>
<servlet>
<servlet-name>ts</servlet-name>
<jsp-file>/Test.jsp</jsp-file>
<init-param>
<param-name>sname</param-name>
<param-value>mani</param-value>
</init-param>
</servlet>
<web-app>
program:
Test2.jsp same as bellow program
web.xml:
----------
<web-app>
<servlet>
<servlet-name>test</servlet-name>
<jsp-file>/Test2.jsp</JSP-file>
</servlet>
<servlet-mapping>
<servlet-name>test</servlet-name>
<url-pattern>/Test2.jsp</url-pattern>
</servlet-mapping>
</web-app>
%>
<%= "program executing successfully:changed" %>
</body>
</html>
static block
non-static block
constructor
JspInit()
config:
org.apache.catalina.core.StandardWrapperFacade@8c0b
5a
this: org.apache.jsp.Test2_JSP@3d60c4
ename: ramchandra
_JSPService()
config:
org.apache.catalina.core.StandardWrapperFacade@8c0b
5a
this: org.apache.jsp.Test2_JSP@3d60c4
ename: ramchandra
_JSPService()
config:
org.apache.catalina.core.StandardWrapperFacade@8c0b
5a
this: org.apache.jsp.Test2_JSP@3d60c4
ename: ramchandra
==============================
if we are not configure .jsp file with the help of
<servlet-mapping>,
for each and every request container will create
different servletobject and servletconfig objects.
<html>
<body>
<%! static{
System.out.println("static block");
}
%>
<%! public Test2_JSP(){
System.out.println("constructor");
}
%>
<%! {
System.out.println("non-static block");
} %>
<%! public void JspInit(){
System.out.println("JspInit()");
ServletConfig cfg = getServletConfig();
System.out.println("config: "+cfg);
System.out.println("this: "+this);
System.out.println("ename:
"+cfg.getInitParameter("ename"));
} %>
<%! public void JSPDestroy(){
System.out.println("JSPDestroy");
} %>
<%
System.out.println("_JSPService()");
ServletConfig cfg = getServletConfig();
System.out.println("config: "+cfg);
System.out.println("this: "+this);
System.out.println("ename:
"+cfg.getInitParameter("ename"));
%>
<%= "program executing successfully:changed" %>
</body>
</html>
static block
non-static block
constructor
JspInit()
config:
org.apache.catalina.core.StandardWrapperFacade@19be
b29
this: org.apache.jsp.Test2_JSP@16c01c8
ename: ramchandra
static block
non-static block
constructor
JspInit()
config:
org.apache.catalina.core.StandardWrapperFacade@8cb4
92
this: org.apache.jsp.Test2_JSP@1a2edef
ename: null
_JSPService()
config:
org.apache.catalina.core.StandardWrapperFacade@8cb4
92
this: org.apache.jsp.Test2_JSP@1a2edef
ename: null
<web-app>
<servlet>
<servlet-name>test</servlet-name>
<jsp-file>/Test2.jsp</JSP-file>
<init-param>
<param-name>ename</param-name>
<param-value>ramchandra</param-value>
</init-param>
</servlet>
<!-- <servlet-mapping>
<servlet-name>test</servlet-name>
<url-pattern>/Test2.jsp</url-pattern>
</servlet-mapping> -->
</web-app>
========================================
JSP Tags:
-----------
We have 5 types of tags
1. Scripting tags
2. Directive tags (page, include, taglib)
3. Standard action tags
4. Custom action tags
5. JSTL
%>
<%--
//int a1 = 111;
/* int b1 = 222;
int c1 = 333;
*/
--%>
<!--
//int a2 = 111;
/* int b2 = 222;
int c2 = 333;
*/
</body>
</html>
<html>
<body bgcolor='yellow'>
<h1>THIS IS JSP PAGE</h1>
<!-- this is html comment -->
<%-- this is jsp comment --%>
//this is java comment
/* this is java ml comment*/
/** this is java d comment*/
<% //this java comment in ST
%>
<%!
//this is declarative tag
%>
</body>
</html>
</body>
</html>
Invalid statement in jsp:
<html>
<body bgcolor='yellow'>
<%
static int a = 111;
%>
<%!
final int b ;
%>
<%
static{
}
%>
<%!
int a =111;
int b;
b=222;
%>
</body>
</html>
Write a program to calcuate add,sub,mul,div by passing
fixed values by using jsp tag and print those values on
browser.
<html>
<body bgcolor='yellow'>
<%!
public int add(int x, int y){
return x+y;
}
public int sub(int x, int y){
return x-y;
}
public int mul(int x, int y){
return x*y;
}
public int div(int x, int y){
return x/y;
}
%>
<%-- <%int a = add(10,2);
int b = sub(10,2);
int c = mul(10,2);
int d = div(10,2);
%>
add: <%= a %><br/>
sub: <%= b %><br/>
mul: <%= c %><br/>
div: <%= d %><br/>
--%>
<h1>add: <%= add(10,2) %><br/>
sub: <%= sub(10,2) %><br/>
mul: <%= mul(10,2) %><br/>
div: <%= div(10,2) %> <br/> </h1>
</body>
</html>
Example on above tags:
<!--THIS IS HTML COMMENT -->
This is html code </br>
//THIS IS JAVA COMMENT
THIS IS JSP COMMENT
<%!
static{
System.out.println("static block");
}
%>
<%!
{
System.out.println("non-static block");
}
%>
<%!
public Test3_JSP(){
System.out.println("constructor");
}
%>
<%!System.out.println("we cannot do this"); %>
<%!
static int m1(){
System.out.println("static m1 method");
return 8989;
}
%>
<%!
int x = 111;
static int y = 333;
private int z = 444;
protected int l = 555;
transient int m = 666;
public int n = 777;
static int o = m1();
%>
<%
int aa1 = 111;
final String aa2 = "ram";
m1();
%>
<%= o %>
<%= "System.out.println"%>
<%= m1() %>
Errors in Jsp:
We have three types of errors in JSP
a. Translation Errors:
The errors which are given by the jsp container
due to bad syntax of tags are called translation errors.
If translation error occurs jsp container not creates
.java file and display 500 error on browser.
<%@ page imports='java.util.ArrayList' %>
<% ArrayList al = new ArrayList(); %>
<html>
<body bgcolor='yellow'>
<%-- <% pageContext.forward("./Test1.jsp"); %> --%>
<%-- <% pageContext.forward("./Test2.html"); %> --%>
<%-- <% pageContext.forward("./Test3.txt"); %> --%>
<% pageContext.forward("./ts"); %>
</body>
</html>
Test1.jsp:
<html>
<body bgcolor='yellow'>
<h1>THIS IS Test1.jsp FILE</h1>
</body>
</html>
Test2.html:
<html>
<body bgcolor='yellow'>
<h1>THIS IS Test.html FILE</h1>
</body>
</html>
Test3.txt:
import java.io.PrintWriter;
import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
@Override
public void service(ServletRequest req, ServletResponse
res)throws ServletException, IOException {
Implicit Objects:
The objects which are created or given by the container
to implement our jsp logic are called Implicit Objects.
</form>
</body>
</html>
<html>
<body bgcolor='yellow'>
<%! int a = 111;
int m1(){
System.out.println("m1 method");
return 222;
}
void m2(){
System.out.println("m2 method");
}
%>
a: <%=a %><br/>
m1():<%=m1() %><br/>
<%-- m2():<%=m2() %> --%>
<%-- a: <%=page.a %>
m1(): <%=page.m1() %> --%>
<%=page %><br/>
<%=page.toString() %><br/>
<%=page.hashCode() %><br/>
<%=page.getClass() %><br/>
<%=page.getClass().getName() %><br/>
</body>
</html>
Web.xml:
<web-app>
<context-param>
<param-name>password</param-name>
<param-value>chandra</param-value>
</context-param>
<servlet>
<servlet-name>ts</servlet-name>
<jsp-file>/Test.jsp</jsp-file>
<init-param>
<param-name>user</param-name>
<param-value>ram</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>ts</servlet-name>
<url-pattern>/Test.jsp</url-pattern>
</servlet-mapping>
</web-app>
Test.jsp:
<html>
<body bgcolor='yellow'>
<form>
<%
ServletConfig sconfig = getServletConfig();
ServletContext scontext = getServletContext();
System.out.println("sconfig: "+sconfig);
System.out.println("user:
"+sconfig.getInitParameter("user"));
System.out.println("scontext:
"+scontext);
System.out.println("password:
"+scontext.getInitParameter("password"));
%>
<%= config.getInitParameter("user") %>
<%=
application.getInitParameter("password") %>
<%! public void jspInit(){
ServletConfig sconfig =
getServletConfig();
ServletContext scontext =
getServletContext();
System.out.println("sconfig: "+sconfig);
System.out.println("scontext: "+scontext);
System.out.println("user:
"+sconfig.getInitParameter("user"));
System.out.println("password:
"+scontext.getInitParameter("password"));
} %>
<%! public void jspDestroy(){
ServletConfig sconfig =
getServletConfig();
ServletContext scontext =
getServletContext();
System.out.println("sconfig: "+sconfig);
System.out.println("user:
"+sconfig.getInitParameter("user"));
System.out.println("scontext:
"+scontext);
System.out.println("password:
"+scontext.getInitParameter("password"));
} %>
</form>
</body>
</html>
Q) Can we able create variable names like confing and
application in jspInit() , jspDestroy(),
_jspService()?
We can able to create variable names like config,
application, page … in jspInit() and jspDestroy(), in
these methods these names are not treated as implicit
object the reason is by default implicit object are
not available to jspInit() and jspDestroy().
We can’t create variable names like config,
application, page … in _jspService(-,-). The on top
of these name container already created implicit
objects and placed into _jspService(-,-). If we are
trying to create we will compiletime error like
duplicate variable.
<html>
<body bgcolor='yellow'>
<form>
<%! public void jspInit(){
System.out.println("jspInit()");
String config = "This is config variable";
String application = "This is application variable";
System.out.println(config);
System.out.println(application);
}
%>
<%! public void jspDestroy(){
System.out.println("jspDestroy");
String config = "This is config
variable";
String application = "This is
application variable";
System.out.println(config);
System.out.println(application);
} %>
//the following code invalid
<%
String config = "this is config vairable";
String application = "This is application variable";
%>
</form>
</body>
</html>
Q) Can we use exception implicit object directly in
jsp?
A) No.
Q) How can we use exception implicit object in jsp?
A) With the help of directive tag.
All 8 implicit objects we can use directly except
“exception” object, the reason by default jsp
container unable recognize the “exception” implicit
object.
If we want to use we need declare one directive tag
like bellow.
<%@ page isErrorPage="true" %>
<html>
<body bgcolor='yellow'>
<form>
<%@ page isErrorPage="true" %>
<%= application %>
<%= config %>
<%= exception %>
</form>
</body>
</html>
IN the above exception implicit object will gives
value like null.
The reason there is no exception in this page.
PageContext:
Usages:
a. Getting implicit objects.
b. Storing/ retrieving / deleting the data from
scope objects.
c. Making communication with other .jsp file,.html
file, .java file,.txt file.
a. Getting implicit objects:
<html>
<body bgcolor='yellow'>
<form>
<%!
public void m1(PageContext pc){
ServletRequest sreq = pc.getRequest();
ServletResponse sres = pc.getResponse();
ServletContext scon = pc.getServletContext();
ServletConfig scfg = pc.getServletConfig();
HttpSession hse = pc.getSession();
java.lang.Object obj = pc.getPage();
java.lang.Throwable thr = pc.getException();
JspWriter jw = pc.getOut();
}
%>
<%
ServletRequest sreq = pageContext.getRequest();
ServletResponse sres = pageContext.getResponse();
ServletContext scon =pageContext.getServletContext();
ServletConfig scfg = pageContext.getServletConfig();
HttpSession hse = pageContext.getSession();
java.lang.Object obj = pageContext.getPage();
java.lang.Throwable thr = pageContext.getException();
JspWriter jw = pageContext.getOut();
m1(pageContext);
%>
</form>
</body>
</html>
b. Storing/ retrieving / deleting the data from scope
objects.
1. setAttribute(-,-):
To place the data page scope object
2. setAttribute(-,-,-):
<form>
<%pageContext.setAttribute("sno", 101); %>
<%pageContext.setAttribute("sname", "ram");%>
<%pageContext.setAttribute("sfee",5000); %>
sno: <%=pageContext.getAttribute("sno") %><br/>
sname: <%=pageContext.getAttribute("sname") %><br/>
sfee: <%=pageContext.getAttribute("sfee") %><br/>
<%pageContext.setAttribute("eno", 5678,
PageContext.REQUEST_SCOPE); %>
eno:
<%=pageContext.getAttribute("eno",PageContext.REQUEST
_SCOPE) %><br/>
<%pageContext.removeAttribute("sno"); %>
sno: <%=pageContext.getAttribute("sno") %><br/>
</form>
</body>
</html>
<%pageContext.setAttribute("sno", 101);
%>
<!--IN the above statement sno will be placed into page
scope object -->
<%pageContext.setAttribute("sname",
"ram"); %>
<%pageContext.setAttribute("sfee",5000);
%>
sno: <%=pageContext.getAttribute("sno")
%><br/>
<!-- In the above statement sno will be read from page
scope object -->
sname:
<%=pageContext.getAttribute("sname") %><br/>
sfee: <%=pageContext.getAttribute("sfee")
%><br/>
<%pageContext.setAttribute("eno",
5678,PageContext.REQUEST_SCOPE); %>
eno: <%=pageContext.getAttribute("eno")
%><br/>
eno:
<%=pageContext.getAttribute("eno",PageContext.REQU
EST_SCOPE) %><br/>
<%pageContext.setAttribute("sno",
201,pageContext.REQUEST_SCOPE); %>
<%pageContext.removeAttribute("sno");
%>
<!-- IN the above statement sno will be deleted from all
scope object -->
sno from page:
<%=pageContext.getAttribute("sno") %><br/>
sno from request:
<%=pageContext.getAttribute("sno",PageContext.REQU
EST_SCOPE) %><br/>
<% pageContext.setAttribute("cname",
"mca",PageContext.APPLICATION_SCOPE); %>
cname from page:
<%=pageContext.getAttribute("cname") %><br/>
cname from request:
<%=pageContext.getAttribute("cname",PageContext.RE
QUEST_SCOPE) %><br/>
cname from application:
<%=pageContext.getAttribute("cname",PageContext.APP
LICATION_SCOPE) %><br/>
<%=pageContext.findAttribute(") %>
<html>
<body bgcolor='yellow'>
<form>
<%pageContext.setAttribute("cname", "mca4",
PageContext.APPLICATION_SCOPE); %>
<%-- <%pageContext.removeAttribute("cname"); %> -
-%>
<%pageContext.removeAttribute("cname",PageContext
.REQUEST_SCOPE); %>
CINEMA NAME p:<%=pageContext.getAttribute("cname")
%><br/>
CINEMA NAME r:<%=pageContext.getAttribute("cname",
PageContext.REQUEST_SCOPE) %><br/>
CINEMA NAME s:<%=pageContext.getAttribute("cname",
PageContext.SESSION_SCOPE) %><br/>
CINEMA NAME a:<%=pageContext.getAttribute("cname",
PageContext.APPLICATION_SCOPE) %><br/>
</form>
</body>
</html>
WARNING:
The basic mistake doing by the programmer is placing
the data in one scope object and reading that data from
another scope object.
First.jsp:
<html>
<body bgcolor='yellow'>
THIS IS FIRST.JSP FILE
<form>
<%-- <%pageContext.forward("./Second.jsp"); %> --%>
<%-- <%pageContext.forward("./Third.html"); %> --%>
<%-- <%pageContext.forward("./Fourth.txt"); %> --%>
<%-- <%pageContext.forward("./ms"); %> --%>
<%-- <%pageContext.include("./Second.jsp"); %>
<%pageContext.include("./Third.html"); %>
<%pageContext.include("./Fourth.txt"); %> --%>
<%pageContext.include("./ms"); %>
</form>
</body>
</html>
Second.jsp:
<html>
<body bgcolor='yellow'>
THIS IS SECOND.JSP FILE <br/>
</body>
</html>
Third.html:
<html>
<body bgcolor='yellow'>
THIS IS THIRD.HTML FILE <br/>
</body>
</html>
Fourth.txt:
<html>
<body bgcolor='yellow'>
THIS IS FOURTH.TXT FILE
</body>
</html>
MyServlet.java:
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
@Override
public void service(ServletRequest request,
ServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<body bgcolor='yellow'>");
out.println("THIS IS SERVLET CLASS");
out.println("</body>");
}
}
web.xml:
<web-app>
<servlet>
<servlet-name>ms</servlet-name>
<servlet-class>MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ms</servlet-name>
<url-pattern>/ms</url-pattern>
</servlet-mapping>
</web-app>
Scope Objects:
We have 4 scope objects.
These are useful carry the information from .jsp file to
another .jsp file.
page
request
application (context)
session
In all the scope object application scope object having
highest accessibility that means we can that scope
object information from all browser as well windows
also.
Session scope object data can be accessible within the
same browser but all windows.
Request scope object data can be accessible only one
window, once response handover to browser request
scope object data will be destroyed.
Page scope object data can be accessible only within
the same page.
<%!
public void jSPDestroy(){
//System.out.println(config);
System.out.println("this is JSPdestroy()");
ServletConfig cfg = getServletConfig();
System.out.println("cfg: "+cfg);
}
%>
For strong and retrieving and removing attributes from
JSP 4 scope objects.
hi
<%
pageContext.setAttribute("eno1", 7279);
%>
<%
pageContext.setAttribute("eno2", 7280);
%>
<%
pageContext.setAttribute("eno3", 7281);
%>
<%
pageContext.setAttribute("eno4", 7282);
%>
eno1:<%=pageContext.getAttribute("eno")%><br/>
eno1:<%=pageContext.getAttributesScope("eno1")%><br/>
eno2:<%=pageContext.getAttributesScope("eno2")%><br/>
eno3:<%=pageContext.getAttributesScope("eno3")%><br/>
eno4:<%=pageContext.getAttributesScope("eno4")%><br/>
eno4:<%=pageContext.getAttributesScope("eno5")%><br/>
<%pageContext.setAttribute("ename1", "ram1",PageContext.PAGE_SCOPE);
%>
<%pageContext.setAttribute("ename2",
"ram2",PageContext.REQUEST_SCOPE); %>
<%pageContext.setAttribute("ename3",
"ram3",PageContext.SESSION_SCOPE); %>
<%pageContext.setAttribute("ename4", "ram4",PageContext.
APPLICATION_SCOPE); %>
ename1:<%=pageContext.getAttribute("ename1") %><br/>
ename1:<%=pageContext.getAttribute("ename1",PageContext.PAGE_SCOPE)
%><br/>
ename1:<%=pageContext.getAttribute("ename1",PageContext.REQUEST_SCOPE)
%><br/>
ename1:<%=pageContext.getAttribute("ename1",PageContext.SESSION_SCOPE)
%><br/>
ename1:<%=pageContext.getAttribute("ename1",PageContext.APPLICATION_SC
OPE) %><br/>
ename2:<%=pageContext.getAttribute("ename2",PageContext.REQUEST_SCOPE)
%><br/>
ename3:<%=pageContext.getAttribute("ename3",PageContext.SESSION_SCOPE)
%><br/>
ename4:<%=pageContext.getAttribute("ename4",PageContext.APPLICATION_SC
OPE) %><br/>
<%pageContext.removeAttribute("ename1"); %>
ename1:<%=pageContext.getAttribute("ename1") %><br/>
ename1:<%=pageContext.getAttribute("ename1",PageContext.PAGE_SCOPE)
%><br/>
bye
Page scope object only within the same JSP file, it is not sharable
between other JSP files.
Test4.jsp:
<% application.setAttribute("eno",1234);%>
<% session.setAttribute("ename","ram");%>
<% request.setAttribute("dept","faculty");%>
<% pageContext.forward("./Test5.jsp");%>
Test5.jsp:
THIS IS TEST5.jsp FILE......
<%= application.getAttribute("eno")%><BR/>
<%= session.getAttribute("eno")%><BR/>
<%= request.getAttribute("eno")%><BR/>
<%-- <%= page.getAttribute("eno")%> --%>
<%= session.getAttribute("ename")%><BR/>
<%= request.getAttribute("dept")%><BR/>
<%= application.getAttribute("dept")%><BR/>
<%= request.getAttribute("ename")%><BR/>
END OF TEST5.jsp FILE
<% pageContext.forward("./Test5.jsp");%>
Test5.jsp:
THIS IS TEST5.jsp FILE......
<%= application.getAttribute("eno")%><BR/>
<% pageContext.forward("./Test5.jsp");%>
Test5.jsp:
THIS IS TEST5.jsp FILE......
<%= session.getAttribute("eno")%><BR/>
Call Test5.jsp file from different browser directly first we will get
null value.
Again call Test4.jsp from that browser then we will get 12345 values.
Call Test5.jsp file from in the browser directly first we will get
12345 values.
This scope object will share only one end user (browser).
<% pageContext.forward("./Test5.jsp");%>
Test5.jsp:
THIS IS TEST5.jsp FILE......
<%= request.getAttribute("eno")%><BR/>
Call Test5.jsp file from different browser directly first we will get
null value.
Whenever we making a request Test5.jsp file servlet container will
create neq request object with empty values.
Again call Test4.jsp from that browser then we will get 1234567
values.
Again open one more tab in the browser and call Test5.jsp we will get
null only.
This scope object only shareable between one request and response
cycles.
<% pageContext.forward("./Test5.jsp");%>
Test5.jsp:
<%= pageContext.getAttribute("eno")%><BR/>
</form>
</body>
</html>
Ud.jsp:
<html>
<body bgcolor='gold'>
<%
String uname =
request.getParameter("uname");
String uage =
request.getParameter("uage");
String ucourse =
request.getParameter("ucourse");
%>
<%= uname %>
<% out.println(uage); %>
<%= ucourse %>
</body>
</html>
Test.html:
<html>
<body bgcolor='yellow'>
<h1>THIS IS HTML FILE</h1>
<form action='./Test4.jsp'>
Enter your name: <input type='text' name='name'/>
<input type='submit' name='reqtype' value='submit'/>
</form>
</body>
</html>
Test4.jsp:
<body bgcolor='yellow'>
<%String name = request.getParameter("name"); %>
Hi...<%=name %>
</body>
In the above program only response like Hi… ramchandra committed but
html text field not committed as response.
So end user needs to click on back button, then only get the html text
field for entering new value.
<body bgcolor='yellow'>
<%String name = request.getParameter("name"); %>
Hi...<%=name %>
<form action='./Test4.jsp'>
Enter your name: <input type='text' name='name'/>
<input type='submit' name='reqtype' value='submit'/>
</form>
</body>
To avoiding the above problem delete user.html file and make request
to jsp directly by sending values from browser URL.
<html>
<body bgcolor='yellow'>
<h2> THIS IS TEST.JSP FILE </h2>
<%
String username =
request.getParameter("username");
int age =
Integer.parseInt(request.getParameter("age"));
out.println("<h1>Your Name Is:
"+username+"</h1><br/>");
out.println("<h1>Your age Is:
"+age+"</h1><br/>");
%>
<form action='./Test.jsp'>
<h2>Enter Your Name: <input type = 'text'
name = 'username'/><br/>
Enter Your Age : <input type = 'text'
name = 'age' /><br/>
<input type = 'submit'
value = 'LOGIN' />
</h2>
</form>
</body>
</html>
http://localhost:4016/MyJspProject/Test.jsp?username=
manu&age=24
To overcome the above problem write html code in .jsp file send the
request to .jsp directly.
Test4.jsp:
<html>
<body bgcolor='yellow'>
<h1>THIS IS HTML FILE</h1>
<%String name = request.getParameter("name"); %>
Hi...<%=name %>
<form action='./Test4.jsp'>
Enter your name: <input type='text' name='name'/>
<input type='submit' name='reqtype' value='submit'/>
</form>
</body>
</html>
In the above approach we are facing one more problem like, in the
first request we are getting null by request. getParameter().
Test4.jsp:
<html>
<body bgcolor='yellow'>
<form action='./Test4.jsp'>
Enter your name: <input type='text' name='name'/>
<input type='submit' name='reqtype' value='submit'/>
</form>
<%String name = request.getParameter("name");
if(name==null){
return;
}
%>
HI...<%= name %>
</body>
</html>
<html>
<body bgcolor='yellow'>
<form action='./Test4.jsp'>
Enter First Value: <input type='text'
name='fno'/><br/>
Enter Second Value: <input type='text'
name='sno'/><br/>
<input type='submit' name='add' value='ADD'/>
</form>
<%String fno = request.getParameter("fno");
String sno = request.getParameter("sno");
if(fno==null || sno ==null){
return;
}
%>
<% int no1 = Integer.parseInt(fno);
int no2 = Integer.parseInt(sno);
%>
Addition is:<%= no1+no2 %>
</body>
</html>
<html>
<body bgcolor='yellow'>
<form action='./Test4.jsp'>
Enter First Value: <input type='text'
name='fno'/><br/>
Enter First Value: <input type='text'
name='sno'/><br/>
<input type='submit' name='reqtype' value='ADD'/>
<input type='submit' name='reqtype' value='SUB'/>
<input type='submit' name='reqtype' value='MUL'/>
<input type='submit' name='reqtype' value='DIV'/>
</form>
<%String result="";
String fno = request.getParameter("fno");
String sno = request.getParameter("sno");
if(fno==null || sno ==null){
System.out.println("if block");
fno="";
sno="";
}
else{
String type = request.getParameter("reqtype");
</body>
</html>
Directive tags:
If we want to provide information to jsp container
about PIC creation according to our requirement (jsp
author) we should go for directive tags.
a. page
b. taglib
c. include
page is used for in the following places
o Import any predefine classes
o Extends PIC from our own class
o Create JSP as thread safe
o Disable the session
o Set the content other than "text/html"
o Handle the exceptions by enable exception
implicit object.
If we are not using page directive tag, jsp container will
create PIC with default code that is
a. Import only jsp and Servlet import statements.
b. PIC Extends server vendor given super class
(HttpJspBase).
c. With no thread safety.
d. With session
e. With default context type “text/html”
f. By default exception implicit object disable.
We can declare directive tags anywhere in .jsp file,
if we declare any where then jsp container will
place those directive tags in some required places.
Better to use directive tags in the top of the jsp.
Page directive having the following attributes:
<%@ page
language
import
extends
isThreadSafe
contentType
pageEncoding
session
errorPage
isErrorPage
autoFlush
buffer
info
isELIgnored
%>
<html>
<body bgcolor='yellow'>
<form>
<%@ page
import="java.util.ArrayList,java.util.Vector" %>
<%@ page import='java.util.Scanner' %>
<%-- <%@ page import='java.util.*' %> --
%>
<%@ page session='false' %>
<%@ page
contentType="application/msword" %>
<%
ArrayList al = new ArrayList();
Scanner scan = new
Scanner(System.in);
Vector v = new Vector();
%>
<%= "good" %>
<%-- <%= session %> --%>
</form>
</body>
</html>
****************************************************
By default container will processing multiple requests
at a time. If want to processing the single request at a
time we need to implements SingleThreadModel
interface we can do this with the support of the
following tag.
<html>
<body bgcolor='yellow'>
<form>
<%= session %>
<%= exception %>
<%@ page isErrorPage="true" %>
</form>
</body>
</html>
If we want use exception implicit object in .jsp we
should write this attribute, that is
isErrorPage=”true”
We can write this directive before or after implicit
object usage statement.
Three.jsp:
<html>
<body bgcolor='gold'>
<form>
<%
try{
out.println("ram".charAt(3));
}catch(StringIndexOutOfBoundsException e){
out.println("<h2>There is no
sufficient index character</h2>");
}
%>
</form>
</body>
</html>
In the above program in all .jsp we are writing
exception handling code by using try and catch
blocks.
To avoiding exception handling and maintain exception
message in separate jsp we should use isErrorPage
attribute and errorPage attributes..
One.jsp
<html>
<body bgcolor='gold'>
<form>
<%@ page errorPage="Error.jsp" %>
<% int a = 10/0;%>
</form>
</body>
</html>
Two.jsp:
<html>
<body bgcolor='gold'>
<form>
<%@ page errorPage="Error.jsp" %>
<% int a=10;
int b = 0;
int c = a/b;
%>
</form>
</body>
</html>
Whenever we using errorPage attribute jsp container
will create separate .java files and .class files for
the .jsp files like bellow.
One_jsp.java
One_jsp.class
Error_jsp.java
Error_jsp.class
Error.jsp:
<h1>DONT WRITE ZERO AS DENOMINATOR</h1>
Three.jsp:
<html>
<body bgcolor='gold'>
<form>
<%@ page errorPage="Error.jsp" %>
<% out.println("ram".charAt(3));%>
</form>
</body>
</html>
In the One.jsp and Two.jsp one exception object is
raises that are ArithmeticException. We are forwarding
that exception object to Error.jsp and print “don’t
enter zero denominators.
In above case exception object and message are
suitable but Three.jsp exception and Error.jsp file
exception message are not match. That means in above
we are unable to handle multiple exceptions by using
one .jsp file to achieve this requirement we should go
for “isErrorPage” attribute.
One.jsp:
<html>
<body bgcolor='gold'>
<form>
<%@ page errorPage="Error.jsp" %>
<% int a = 10/0;%>
</form>
</body>
</html>
Two.jsp:
<html>
<body bgcolor='gold'>
<form>
<%@ page errorPage="Error.jsp" %>
<% int a = Integer.parseInt("a");
%>
</form>
</body>
</html>
Three.jsp:
<html>
<body bgcolor='gold'>
<form>
<%@ page errorPage="Error.jsp" %>
<% out.println("ram".charAt(3));%>
</form>
</body>
</html>
Error.jsp:
<body bgcolor='gold'>
<%@ page isErrorPage="true"%>
<%
try{
throw exception;
}
catch(ArithmeticException ae){
out.println("<h2>dont enter zero as
denominator</h2>");
}
catch(StringIndexOutOfBoundsException ae){
out.println("<h2>there is no sufficient
character to print</h2>");
}
catch(NumberFormatException e){
out.println("<h2>please enter numeric type
data</h2>");
}
%>
</body>
l1.jsp:
<%@ page isThreadSafe='false' %>
<%= this is JSP file %>
------------------------------------------------------------------------
<%@ page session='false' %>
With the support above tag we can avoid session object
in JSP.
<html>
<body bgcolor='yellow'>
<%-- <%@ page isThreadSafe="false" session="true"
import='java.util.*' %> --%>
<%@ page import= 'java.io.*'%>
<%@ page import= 'java.util.*'
extends='javax.servlet.GenericServlet'%>
<h1>THIS IS TEST1 JSP FILE-WITH SESSION
OBJECT</h1>
<%= session %><br/>
<%= new Date() %><br/>
<%ArrayList al = new ArrayList();
al.add(10);
%>
<%= al %>
<%= new FileOutputStream("ram.txt") %>
<%! public void service(ServletRequest
req,ServletResponse res)
throws ServletException,IOException{
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("<body bgcolor='yellow'>THIS IS
SERVICE METHOD </body>");
HttpServletRequest hreq =
(HttpServletRequest)req;
HttpServletResponse hres =
(HttpServletResponse)res;
_jspService(hreq,hres);
} %>
</body>
</html>
<html>
<body bgcolor='yellow'>
<h1>THIS IS TEST2.JSP FILE</h1>
<%@ page info="this is my test2.jsp file" %>
<h1><%= getServletInfo() %></h1>
</body>
</html>
%>
<%="good"%><br/>
----------------------------------------------
by default response object always committed on
browser.
the reason we have attribute like autoFlush='true'
once we mention autoFlush='false' response object not
commited automatically we need to call out.flush()
<html>
<body bgcolor='yellow'>
<h1>THIS IS TEST2.JSP FILE</h1>
<img src=C:\Users\Ramchandar\Desktop\\Koala.jpg
/>
</body>
</html>
<html>
<body bgcolor='yellow'>
<%@ page isThreadSafe="false" %>
<%!StringBuilder sb = new StringBuilder(); %>
<%sb.append("ram");
Thread.sleep(10000);
%>
name: <%= sb %>
</body>
</html>
<html>
<body bgcolor='yellow'>
<%@ page contentType="application/msword" %>
<% out.print("this msword result"); %>
</body>
</html>
<html>
<body bgcolor='yellow'>
<%= 10/0 %>
</body>
</html>
Error1.jsp:
<html>
<body bgcolor='yellow'>
<%@ page errorPage="error2.jsp" %>
<%= 10/0 %>
</body>
</html>
Error2.jsp
<html>
<body bgcolor='yellow'>
<h1>DONT ENTER ZERO AS DENOMINATOR</h1>
</body>
</html>
Error1.jsp:
<html>
<body bgcolor='yellow'>
<%@ page isErrorPage="true" errorPage="error2.jsp" %>
<% try{
int a = 10/0;
}catch(ArithmeticException e){
out.println("dont enter zero as denominator");
throw e;
}
catch(NegativeArraySizeException e){
out.println("declare array size");
}
catch(ArrayIndexOutOfBoundsException e){
out.println("there is no sufficient memeory
location");
}
%>
</body>
</html>
Error2.jsp:
<html>
<body bgcolor='yellow'>
<h1>DONT ENTER ZERO AS
DENOMINATOR...................</h1>
</body>
</html>
</form>
</body>
<%
int x =
Integer.parseInt(request.getParameter("fno"));
int y =
Integer.parseInt(request.getParameter("sno"));
%>
Addition is: <%= (x+y) %>
<form action='./r1.jsp'>
<input type='text' name='name'/><br/>
<input type='submit' value='submit'/>
</form>
<%= request.getParameter("name") %>
in the above program there is one drawback that is
enduser click on backbutton everytime.
add html code directly in JSP and callto JSP directly.
<form action='./r1.jsp'>
<input type='text' name='name'/><br/>
<input type='submit' value='submit'/>
</form>
<html>
<body bgcolor='yellow'>
<form>
<h1>THIS IS TEST.JSP FILE STARTING
PLACE</h1>
<%@ include file="One.jsp" %>
<%@ include file="Two.jsp" %>
<h1>THIS IS TEST.JSP FILE ENDING
PLACE</h1>
</form>
</body>
</html>
One.jsp:
<html>
<body bgcolor='gold'>
<form>
<H1>THIS IS ONE.JSP FILE</H1>
</form>
</body>
</html>
Two.jsp:
<html>
<body bgcolor='gold'>
<form><H1>THIS IS TWO.JSP FILE</H1>
</form>
</body>
</html>
Three.jsp:
<html>
<body bgcolor='gold'>
<form>
<H1>THIS IS THREE.JSP FILE</H1>
</form>
</body>
</html>
t1.jsp
--------
<% out.println("t1 page starting"); %><br/>
<%@ include file='t2.jsp' %>
<% out.println("t1 page ending"); %><br/>
t2.jsp:
<%out.println("this t2.jsp page"); %><br/>
Test.jsp:
<html>
<body bgcolor='yellow'>
<form>
<h1>THIS IS TEST.JSP FILE STARTING PLACE</h1>
<%@ include file="One.jsp" %>
<%@ include file="Two.jsp" %>
<h1>THIS IS TEST.JSP FILE ENDING PLACE</h1>
</form>
</body>
</html>
Two.jsp
<html>
<body bgcolor='gold'>
<form><H1>THIS IS TWO.JSP FILE</H1>
<%! int a = 555; %>
</form>
</body>
</html>
One.jsp:
<html>
<body bgcolor='gold'>
<form>
<H1>THIS IS ONE.JSP FILE</H1>
<% int a = 444; %>
</form>
</body>
</html>
t1.jsp
--------
<% out.println("t1 page starting"); %><br/>
<%@ include file='t2.jsp' %>
<%@ include file='t3.jsp' %>
<%@ include file='t4.jsp' %>
<%= x+y %>
<% out.println("t1 page ending"); %><br/>
t2.jsp
<%out.println("this t2.jsp page"); %><br/>
t3.jsp
<% int x = 10; %>
t4.jsp
<% int y = 10; %>
We must be take both t3.jsp and t4.jsp file variable
names different otherwise we will get duplicate variable
error.
In the above program container will create only one
Page Implementation Class for all JSP's.
Four.html:
<html>
<body bgcolor='gold'>
<form>
<H1>THIS IS FOUR.HTML FILE</H1>
<%
int x = 5;
int y = 15;
%>
<h1> The Addtion Is: <%= (x+y) %> </h1>
</form>
</body>
</html>
In the above .html file we feel the bellow code
normal text. But once this code copied into Test.jsp
by @include tag this code will be treated as jsp
code. That’s why we will output like “The Addition
Is: 20”.
Standard actions tags:
<jsp:include>
It will used for dynamic inclusion. Code is not
copy, only result will be copying. It is equal to
rd.include() in servlet. For each including web resource
jsp container will create a separate page
implementation class.
t1.jsp
test.html
<form >
<input type='text' name='name'/><br/>
<input type='submit' value='submit'/>
</form>
t2.jsp
from t2.jsp <br/>
t3.jsp
from t3.jsp <br/>
t4.jsp
from t4.jsp <br/>
<html>
<body bgcolor="yellow">
<h1>THIS IS DEMO.JSP FILE STARTS </h1>
<jsp:include page="R1.jsp"></jsp:include>
<jsp:include page="R2.jsp"></jsp:include>
<% out.println("The Result is: "+(x+y)); %>
<H1>THIS IS DEMO.JSP FILE ENDS</H1>
</body>
</html>
R1.jsp
<html>
<body bgcolor="yellow">
<h1>THIS IS R1.JSP FILE RESPONSE </h1>
<%int x=100; %>
</body>
</html>
R2.jsp:
<html>
<body bgcolor="yellow">
<h1>THIS IS R2.JSP FILE RESPONSE</h1>
<% int y=200; %>
</body>
</html>
test.html
<%
int x = 5;
int y = 6;
%>
addition: <%= x+y %>
test.html
<%
int x = 5;
int y = 6;
%>
addition: <%= x+y %>
AddServlet.java:
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;
public class AddServlet extends HttpServlet {
@Override
public void doGet(HttpServletRequest
req,HttpServletResponse res)
throws ServletException, IOException{
int x =10;
int y = 20;
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("x+y: "+(x+y));
}
}
web.xml:
<web-app>
<servlet>
<servlet-name>add</servlet-name>
<servlet-class>AddServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>add</servlet-name>
<url-pattern>/add</url-pattern>
</servlet-mapping>
</web-app>
test.html:
<form action='./t1.jsp'>
<input type='text' name='name'/>
<input type='submit' value='submit'/>
</form>
t1.jsp:
t2.jsp:
web.xml:
<web-app>
<servlet>
<servlet-name>add</servlet-name>
<jsp-file>/t1.jsp</JSP-file>
</servlet>
<servlet-mapping>
<servlet-name>add</servlet-name>
<url-pattern>/t1.jsp</url-pattern>
</servlet-mapping>
</web-app>
<jsp:forward>
This tag is useful for making communication between
.jsp file to other web components (Servlet, .jsp, .html,
.txt).
Destination web component response only added to
browser.
We can able to use only one <jsp:forward>
Maximum time we are always writing this tag end of
.jsp file.
Whenever we are writing other line of code after this
tag, those lines of code are unable to execute.
Communication between .jsp file to Servlet by using
<jsp:forward>
With the help of this tag we are not only forwarding
control but also we can forward data by using
<jsp:param> tag
One.jsp:
<html>
<body bgcolor='yellow'>
<form>
<h1>TEST.JSP FILE STARTS</h1>
<jsp:forward page='./hs'/>
<%-- <jsp:forward page='./one.jsp'/> --
%>
<%-- <jsp:forward page='./three.html'/>
--%>
<jsp:forward page='./my.txt'/>
<h1>TEST.JSP FILE ENDS</h1>
</form>
</body>
</html>
HelloServlet.java:
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class HelloServlet extends GenericServlet{
@Override
public void service(ServletRequest request,
ServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("THIS IS HELLO SERVLET
RESPONSE");
}
}
Web.xml:
<web-app>
<servlet>
<servlet-name>test</servlet-name>
<servlet-class>HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>test</servlet-name>
<url-pattern>/hs</url-pattern>
</servlet-mapping>
</web-app>
Test2.html:
<html>
<body bgcolor='RED'>
<h1>THIS IS Test.html FILE</h1>
<form action='./t1.jsp'>
Enter first number: <input type='text' name='fno'/>
Enter second number: <input type='text'
name='sno'/>
<input type='submit' value='add'/>
</form>
</body>
</html>
t1.jsp:
<%
int i = Integer.parseInt(request.getParameter("fno"));
int j = Integer.parseInt(request.getParameter("sno"));
int result = i+j;
%>
<jsp:forward page="t2.jsp">
<jsp:param value="<%=result %>" name="res"/>
</jsp:forward>
T2.jsp:
<html>
<body bgcolor="yellow">
<h1>THIS IS R2.JSP FILE RESPONSE</h1>
<%= request.getParameter("res") %>
</body>
</html>
UserInfo.html:
<html>
<body bgcolor='gold'>
<form action='./Read.jsp'>
Enter UserName: <input type='text' name='uname'/>
<input type='submit' value='SUBMIT'/>
</form>
</body>
</html>
Read.jsp:
<html>
<body bgcolor='gold'>
<form>
<%String username = request.getParameter("uname");%>
<jsp:forward page="Print.jsp">
<jsp:param value="<%= username %>" name="uname"/>
</jsp:forward>
</form>
</body>
</html>
Print.jsp:
<html>
<body bgcolor='yellow'>
<form>
<%String username = request.getParameter("uname"); %>
<%= username %>
</form>
</body>
</html>
Add.html:
<html>
<body bgcolor='gold'>
<form action='./Read.jsp'>
Enter First Number:<input type='text'
ame='fno'/><br/>
Enter Second Number: <input type='text'
name='sno'/><br/>
<input type='submit' value='ADD'/>
</form>
</body>
</html>
Read.jsp:
<html>
<body bgcolor='gold'>
<form>
<%String fno = request.getParameter("fno");
String sno = request.getParameter("sno");
int i= Integer.parseInt(fno);
int j= Integer.parseInt(sno);
int result=i+j;
%>
<jsp:forward page="Print.jsp">
<jsp:param value="<%= result %>" name="result"/>
</jsp:forward>
</form>
</body>
</html>
Print.jsp:
<html>
<body bgcolor='yellow'>
<form>
<%int result=
Integer.parseInt(request.getParameter("result"));%>
The Result Is: <%= result %>
</form>
</body>
</html>
Print.jsp:
<html>
<body bgcolor='yellow'>
<form>
<%int i =
Integer.parseInt(request.getParameter("fno"));
int j =
Integer.parseInt(request.getParameter("sno"));%>
The Result Is: <%= (i+j) %>
</form>
</body>
</html>
Program on <jsp:useBean>,<jsp:setProperty> and
<jsp:getProperty>
public CalculationProj(){
System.out.println("constructor executing");
}
public int add(int a, int b){
System.out.println("add method executing");
return (a+b);
}
public int sub(int a, int b){
System.out.println("sub method executing");
return (a-b);
}
public int mul(int a, int b){
System.out.println("mul method executing");
return (a*b);
}
public int div(int a, int b){
System.out.println("div method executing");
return (a/b);
}
}
Test.jsp:
<html>
<body bgcolor='yellow'>
<form>
<h1>TEST.JSP FILE STARTS</h1>
<jsp:useBean id="cal"
class="kit.aj.jsp.CalculationProj"></jsp:useBean>
<h1>Addition: <%=cal.add(10,20) %><br/>
Subtraction: <%= cal.sub(20,10) %><br/>
Multiplication:<%=cal.mul(10,6) %><br/>
Division:<%=cal.div(10,2) %><br/></h1>
</form>
</body>
</html>
package kit.aj.jsp;
import java.io.Serializable;
public class Employee implements Serializable{
public Employee(){
System.out.println("constructor executing");
}
private int eid;
private String ename;
private int esal;
private int eage;
public int getEage() {
System.out.println("getEage");
return eage;
}
public void setEage(int eage) {
System.out.println("setEage\t: "+eage);
this.eage = eage;
}
public String getEname() {
System.out.println("getEname");
return ename;
}
public void setEname(String ename) {
System.out.println("setEname\t: "+ename);
this.ename = ename;
}
public int getEsal() {
System.out.println("getEsal");
return esal;
}
public void setEsal(int esal) {
System.out.println("setEsal\t: "+esal);
this.esal = esal;
}
public int getEid() {
System.out.println("getEid");
return eid;
}
public void setEid(int eid) {
System.out.println("setEid: "+eid);
this.eid = eid;
}
}
Test.jsp:
<html>
<body bgcolor='yellow'>
<form>
<h1>TEST.JSP FILE STARTS
<jsp:useBean id="emp"
class="kit.aj.jsp.Employee"></jsp:useBean>
<%-- <%emp.setEid(101);
emp.setEname("Annie");
emp.setEage(25);
emp.setEsal(8000);
%>
Eid: <%=emp.getEid() %><br/>
Ename: <%=emp.getEname() %><br/>
Esal: <%=emp.getEsal() %><br/>
Eage: <%=emp.getEage() %><br/> --%>
<jsp:setProperty property="eid"
name="emp" value="101"/>
<jsp:setProperty property="ename"
name="emp" value="ram"/>
<jsp:setProperty property="esal"
name="emp" value="7000"/>
<jsp:setProperty property="eage"
name="emp" value="30"/>
<br/>
Eid:<jsp:getProperty property="eid"
name="emp"/><br/>
Ename:<jsp:getProperty property="ename"
name="emp"/><br/>
Esal:<jsp:getProperty property="esal"
name="emp"/><br/>
Eage:<jsp:getProperty property="eage"
name="emp"/>
</h1>
</form>
</body>
</html>
Test.jsp:
<html>
<body bgcolor='yellow'>
<form>
<h1>TEST.JSP FILE STARTS
<%@ page import="java.sql.ResultSet" %>
<jsp:useBean id="gs"
class="kit.aj.jsp.GetResults">
</jsp:useBean>
<% ResultSet rs = gs.getResults();
while(rs.next()){
out.println(rs.getInt(1)+"\t"+rs.getString(2)+"\t
"+
rs.getInt(3)+"\t"+rs.getInt(4)+"<br/>");
}
%>
</h1>
</form>
</body>
</html>
GetResults.java:
package kit.aj.jsp;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con =
DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:xe","system","m
anager");
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select *
from sbaj6to8");
return rs;
}
catch(Exception e){
System.out.println("*************");
e.printStackTrace();
}
return null;
}
}
package com.ram;
test.html:
<form action='./t1.jsp'>
<input type='text' name='name'/>
<input type='text' name='age'/>
<input type='submit' value='submit'/>
</form>
t1.jsp:
--------
<jsp:useBean id="my"
class="com.ram.MyBean"></jsp:useBean>
<%-- <jsp:setProperty property="*" name="my"/> --%>
<jsp:setProperty property="name" name="my"/>
<jsp:setProperty property="age" name="my"/>
<jsp:getProperty property="name" name="my"/>
<jsp:getProperty property="age" name="my"/>
<web-app>
<servlet>
<servlet-name>add</servlet-name>
<jsp-file>/t1.jsp</JSP-file>
</servlet>
<servlet-mapping>
<servlet-name>add</servlet-name>
<url-pattern>/t1.jsp</url-pattern>
</servlet-mapping>
</web-app>
======================================
li.jsp
<jsp:forward page="date.jsp" />
date.jsp
<%= new java.util.Date()
%>
===================================
l1.jsp:
<jsp:forward page="date.jsp" >
<jsp:param value="ramchandra" name="name"/>
</JSP:forward>
date.jsp:
<%= new java.util.Date()
%>
<%= request.getParameter("name")
%>
--------------------------------------------------------------
<jsp:useBean>
l1.jsp:
<jsp:useBean id="obj" class="com.ram.Calculation"/>
<%
int x = obj.cal(5);
out.print(x);
%>
Calculation.java
package com.ram;
<html>
<body bgcolor='yellow'>
<form action='./two.jsp'>
Enter your name: <input type='text'
name='sid'/>
Etner your course:<input type='text'
name='sname'/>
<input type='submit' value='submit'/>
</form>
</body>
</html>
Student.java:
package com.ram;
Two.jsp:
<html>
<body bgcolor='yellow'>
<form>
<h1>TWO.JSP FILE STARTS</h1>
<jsp:useBean id="std"
class="com.ram.Student" />
<%-- <jsp:setProperty property="sid"
name="std" value="101"/>
<jsp:setProperty property="sname"
name="std" value="ram"/> --%>
<jsp:setProperty name='std'
property='*'/>
<jsp:getProperty property="sid"
name="std"/>
<jsp:getProperty property="sname"
name="std"/>
<h1>TWO.JSP FILE ENDS</h1>
</form>
</body>
</html>
Special case in two.jsp:
<html>
<body bgcolor='yellow'>
<form>
<h1>TWO.JSP FILE STARTS</h1>
<jsp:useBean id="std"
class="com.ram.Student" />
<jsp:setProperty name='std'
property='*'/>
<jsp:getProperty property="sid"
name="std"/>
<jsp:getProperty property="sname"
name="std"/>
<jsp:setProperty property="sid"
name="std" value="101"/>
<jsp:setProperty property="sname"
name="std" value="ram"/>
<jsp:getProperty property="sid"
name="std"/>
<jsp:getProperty property="sname"
name="std"/>
<h1>TWO.JSP FILE ENDS</h1>
</form>
</body>
</html>
In the program we will get output like
800 suji
101 ram
date.jsp:
<html>
<body bgcolor='red'>
<%@ taglib prefix="nit" uri="customtags" %>
<h1>Today date is: <nit:date/> </h1>
</body>
web.xml:
<web-app>
<jsp-config>
<taglib>
<taglib-uri>customtags</taglib-uri>
<taglib-location>/WEB-
INF/ourtaglib.tld</taglib-location>
</taglib>
</jsp-config>
</web-app>
</html>
ourtaglib.tld
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</JSP-version>
<short-name>nit</short-name>
<tag>
<name>date</name>
<tag-class>com.nit.DateHandler</tag-class>
</tag>
</taglib>
DateHandler:
package com.nit;
import java.io.IOException;
import java.text.DateFormat;
import java.util.Date;
import javax.servlet.jsp.jspException;
import javax.servlet.jsp.jspWriter;
import javax.servlet.jsp.tagext.TagSupport;
<web-app>
<jsp-config>
<taglib>
<taglib-uri>customtags</taglib-uri>
<taglib-location>/WEB-
INF/ourtaglib.tld</taglib-location>
</taglib>
</jsp-config>
</web-app>
ourtaglib.tld :
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>nit</short-name>
<tag>
<name>greet</name>
<tag-class>com.nit.GreetingHandler</tag-
class>
<body-content>empty</body-content>
<attribute>
<name>name</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>age</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
GreetingHandler.java:
package com.nit;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
<html>
<body>
<%@ taglib prefix = "ex" uri = "customtags"%>
<ex:Hello >
This is my own tag body content
</ex:Hello>
</body>
</html>
web.xml:
<web-app>
<jsp-config>
<taglib>
<taglib-uri>customtags</taglib-uri>
<taglib-location>/WEB-
INF/ourtaglib.tld</taglib-location>
</taglib>
</jsp-config>
</web-app>
ourtaglib.tld:
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>ex</short-name>
<tag>
<name>Hello</name>
<tag-class>com.nit.HelloTag</tag-class>
<body-content>scriptless</body-content>
</tag>
</taglib>
HelloTag.java:
package com.nit;
import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.*;
import java.io.*;
a. Model-1:
The bellow logics are always writing
within the one web component like
either Servlet or jsp.
1.Presentation logic
a. User input data
b. User output data
2.Data gathering logic
3.Validation logic
4.Session Tracking
5.Control logic.
6.Connection Pooling logic
7.Business logic
8.Persistence logic
9.Middle ware logics.
b. Model-2(MVC-1)
mvc1:
emp.html
<html>
<body bgcolor="yellow">
<form action='./mvc'>
eno: <input type='text' name="eid"/>
<input type='submit' value='submit'/>
</form>
</body>
</html>
web.xml:
<web-app>
<servlet>
<servlet-name>mvc</servlet-name>
<servlet-class>ControleServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>mvc</servlet-name>
<url-pattern>/mvc</url-pattern>
</servlet-mapping>
</web-app>
ControleServlet
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.nit.Employee;
System.out.println("from doGet");
}
Employee:
package com.nit;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
Class.forName("oracle.jdbc.driver.OracleDriver");
con =
DriverManager.getConnection("jdbc:oracle:thin:@localh
ost:1521:xe","system","manager");
}catch(Exception e){
System.out.println("problem in empliyee
class");
e.printStackTrace();
}
}
public int getEid() {
return eid;
}
public void setEid(int eid) {
this.eid = eid;
result();
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public int getEsal() {
return esal;
}
public void setEsal(int esal) {
this.esal = esal;
}
public void result(){
Statement st;
try {
System.out.println("con"+con);
st = con.createStatement();
ResultSet rs = st.executeQuery("select *
from employee");
rs.next();
ename=rs.getString(2);
esal = rs.getInt(3);
System.out.println("ename: "+ename);
System.out.println("esal: "+esal);
} catch (SQLException e) {
System.out.println("problem in result");
e.printStackTrace();
}
}
}
view.jsp
<html>
<body bgcolor='green'>
<%@ page import="com.nit.Employee" %>
<jsp:useBean id="ebean" class="com.nit.Employee"
scope="application"></JSP:useBean>
eno: <jsp:getProperty property="eid" name="ebean"/>
ename: <jsp:getProperty property="ename"
name="ebean"/>
esal: <jsp:getProperty property="esal" name="ebean"/>
<%= "controle comes here" %>
</body>
</html>
================================================
================
mvc:
login.html:
<body bgcolor='yellow'>
<form action='./login' method='post'>
<input type='text' name='name'/>
<input type='password' name='password'/>
<input type='submit' value='submit'/>
</form>
</body>
web.xml:
<web-app>
<servlet>
<servlet-name>logic</servlet-name>
<servlet-class>LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>logic</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
</web-app>
LoginServlet.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.nit.LoginBean;
req.setAttribute("bean", bean);
boolean flag = bean.validate(password);
if(flag){
RequestDispatcher rd =
req.getRequestDispatcher("./sucess.jsp");
rd.forward(req, res);
}
else
{
RequestDispatcher rd =
req.getRequestDispatcher("./failure.jsp");
rd.forward(req, res);
}
}
LoginBean.java
package com.nit;
Model1:
In this approach we are using either Servlet or jsp
as web component. In this model we are always writing
the logic within the either one Servlet or one JSP.
Disadvantages:
1. No clean separation between logics.
2. If we are doing modifications on one logic, those
modifications are effected to other logics.
3. Maintenance and enhancement is very complex.
4. Parallel development is not possible.
5. Project development is increases.
6. Productivity will be decreases.
7. It is not a layered architecture.
8. We (Programmer) must be aware about all
technologies.
9. It is not industry recommended approach
Advantages:
No parallel development no needs to use more
programmers. (Less human resources).
Model2:
We have types of architectures.
MVC-1:
Controller: The component which is behavior like
taking the request, validate the request, and to
destination resource and gathering the response
and finally handover to browser is called controller.
Model:
This is one java object, which is useful for
interacting with database and gathering the response
and handover to controller.
View:
It is useful for holding logic related view or
representation.
Disadvantages:
1. No Clear separation between logics
2. If we are doing modifications on one logic some
of the remaining logics also affected.
3. The problem here either Servlet or jsp will takes
all request and responses.
Advantages:
Compare model-1 this approach is some provides
modularity.
The drawbacks which we have in mvc-1 and model-1 we
can avoid all these things through MVC-2 architecture.
Expression Language:
It is simplifies the reading the data from bean
object and other implicit objects.
And also provides arithmetic,logical,relational
operations.
output
true 6
---------------------
Test.jsp
<html>
<head>
<title>Expression language example2</title>
</head>
<body>
<form action="display.jsp">
Student Name: <input type="text" name="stuname"
/><br>
Student RollNum:<input type="text" name="rollno"
/><br>
<input type="submit" value="Submit Details!!"/>
</form>
</body>
</html>
display.jsp
<html>
<head>
<title>Display Page</title>
</head>
<body>
Student name is ${ param.stuname } <br>
Student Roll No is ${ param.rollno }
</body>
</html>
--------------------------------------------
reading the data from application scope:
Test.jsp
<html>
<head>
<title>EL example3</title>
</head>
<body>
<%
application.setAttribute("author", "ramchandra");
application.setAttribute("Site", "Nareshit.com");
%>
<a href="display.jsp">Click</a>
</body>
</html>
display.jsp
<html>
<head>
<title>Display Page</title>
</head>
<body>
${applicationScope.author}<br>
${applicationScope.Site}
</body>
</html>
------------------------
<html>
<head>
<title>EL example3</title>
</head>
<body>
<%
session.setAttribute("author", "ramchandra1");
session.setAttribute("Site", "Nareshit1.com");
%>
<a href="display.jsp">Click</a>
</body>
</html>
<html>
<head>
<title>Display Page</title>
</head>
<body>
${sessionScope.author}<br>
${sessionScope.Site}
</body>
</html>
---------------------------------------
<html>
<head>
<title>EL example3</title>
</head>
<body>
<%
request.setAttribute("author", "ramchandra2");
request.setAttribute("Site", "Nareshit2.com");
%>
${requestScope.author}<br>
${requestScope.Site}
<a href="display.jsp">Click</a>
</body>
</html>
<html>
<head>
<title>Display Page</title>
</head>
<body>
${requestScope.author}<br>
${requestScope.Site}
</body>
</html>
------------------------------------------------
<html>
<head>
<title>EL example3</title>
</head>
<body>
<%
request.setAttribute("author", "ramchandra2");
request.setAttribute("Site", "Nareshit2.com");
pageContext.forward("/display.jsp");
%>
</body>
</html>
display.jsp:
Fast Development
Code reusability
No need to Scriptlet tag in JSP file
It will provides following tags like:
o core tags
o functional tags
o sql tags
o formating tags
o xml tags
Core Tags:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core"
prefix="c" %>
c:out:
c:set
<%@ taglib uri="http://java.sun.com/JSP/jstl/core"
prefix="c" %>
<html>
<head>
<title>Tag Example</title>
</head>
<body>
<c:set var="x" scope="session" value="${1000*5}"/>
<c:out value="${x}"/>
</body>
</html>
c:remove
<%@ taglib uri="http://java.sun.com/JSP/jstl/core"
prefix="c" %>
<html>
<head>
<title>Core Tag Example</title>
</head>
<body>
<c:set var="money" scope="session"
value="${4000*4}"/>
<p>Before Remove Value is: <c:out
value="${money}"/></p>
<c:remove var="money"/>
<p>After Remove Value is: <c:out
value="${money}"/></p>
</body>
</html>
c:catch
<%@ taglib uri="http://java.sun.com/JSP/jstl/core"
prefix="c" %>
<html>
<head>
<title>Core Tag Example</title>
</head>
<body>
</body>
</html>
c:if
<%@ taglib uri="http://java.sun.com/JSP/jstl/core"
prefix="c" %>
<html>
<head>
<title>Core Tag Example</title>
</head>
<body>
<c:set var="income" scope="session"
value="${4000*4}"/>
<c:if test="${income > 8000}" >
<p>My income is: <c:out value="${income}"/><p>
</c:if>
</body>
</html>
c:forEach
<%@ taglib uri="http://java.sun.com/JSP/jstl/core"
prefix="c" %>
<html>
<head>
<title>Core Tag Example</title>
</head>
<body>
<c:forEach var="j" begin="1" end="3">
count <c:out value="${j}"/><p>
</c:forEach>
</body>
</html>
:c:forTokens:
<%@ taglib uri="http://java.sun.com/JSP/jstl/core"
prefix="c" %>
<html>
<head>
<title>Core Tag Example</title>
</head>
<body>
<c:forTokens items="ram chandra rao" delims=" "
var="name">
<c:out value="${name}"/><p>
</c:forTokens>
</body>
</html>
c:url
<%@ taglib uri="http://java.sun.com/JSP/jstl/core"
prefix="c" %>
<html>
<head>
<title>Core Tag Example</title>
</head>
<body>
<c:url value="/index1.jsp" var="url">
<c:param name="a" value="10"/>
<c:param name="b" value="20"/>
</c:url>
${url}
</body>
</html>
c:redirect
<%@ taglib uri="http://java.sun.com/JSP/jstl/core"
prefix="c" %>
<html>
<head>
<title>Core Tag Example</title>
</head>
<body>
<c:set var="url" value="0" scope="request"/>
<c:if test="${url<1}">
<c:redirect url="http://www.google.com"/>
</c:if>
<c:if test="${url>1}">
<c:redirect url="http://www.facebook.com"/>
</c:if>
</body>
</html>
Functional Tags:
contains:
</body>
</html>
containsIgnoreCase
<%@ taglib uri="http://java.sun.com/JSP/jstl/core"
prefix="c" %>
<%@ taglib
uri="http://java.sun.com/JSP/jstl/functions"
prefix="fn" %>
<html>
<head>
<title>Using JSTL Functions</title>
</head>
<body>
<c:if test="${fn:containsIgnoreCase(String,
'NARESHIT')}">
<p>Found NARESHIT string<p>
</c:if>
</body>
</html>
endsWith
<%@ taglib uri="http://java.sun.com/JSP/jstl/core"
prefix="c" %>
<%@ taglib
uri="http://java.sun.com/JSP/jstl/functions"
prefix="fn" %>
<html>
<head>
<title>Using JSTL Functions</title>
</head>
<body>
</body>
</html>
indexOf:
<%@ taglib uri="http://java.sun.com/JSP/jstl/core"
prefix="c" %>
<%@ taglib
uri="http://java.sun.com/JSP/jstl/functions"
prefix="fn" %>
<html>
<head>
<title>Using JSTL Functions</title>
</head>
<body>
</body>
</html>
trim:
<%@ taglib uri="http://java.sun.com/JSP/jstl/core"
prefix="c" %>
<%@ taglib
uri="http://java.sun.com/JSP/jstl/functions"
prefix="fn" %>
<html>
<head>
<title>Using JSTL Functions</title>
</head>
<body>
</body>
</html>
startsWith
<%@ taglib uri="http://java.sun.com/JSP/jstl/core"
prefix="c" %>
<%@ taglib
uri="http://java.sun.com/JSP/jstl/functions"
prefix="fn" %>
<html>
<head>
<title>Using JSTL Function</title>
</head>
<body>
<c:set var="msg" value="The Example of JSTL
fn:startsWith() Function"/>
The string starts with "The": ${fn:startsWith(msg, 'The')}
<br>The string starts with "Example":
${fn:startsWith(msg, 'Example')}
</body>
</html>
<p>String-5 : ${str5}</p>
</body>
</html>
toLowerCase:
<%@ taglib uri="http://java.sun.com/JSP/jstl/core"
prefix="c" %>
<%@ taglib
uri="http://java.sun.com/JSP/jstl/functions"
prefix="fn" %>
<html>
<head>
<title> Using JSTL Function </title>
</head>
<body>
<c:set var="string" value="WELCOME TO JSP
PROGRAMMING"/>
${fn:toLowerCase("HELLO,")}
${fn:toLowerCase(string)}
</body>
</html>
toUpperCase:
<%@ taglib uri="http://java.sun.com/JSP/jstl/core"
prefix="c" %>
<%@ taglib
uri="http://java.sun.com/JSP/jstl/functions"
prefix="fn" %>
<html>
<head>
<title> Using JSTL Function </title>
</head>
<body>
<c:set var="string" value="Welcome to JSP
Programming"/>
${fn:toLowerCase("hello,")}
${fn:toUpperCase(string)}
</body>
</html>
othermethods:
<%@ taglib uri="http://java.sun.com/JSP/jstl/core"
prefix="c" %>
<%@ taglib
uri="http://java.sun.com/JSP/jstl/functions"
prefix="fn" %>
<html>
<head>
<title> Using JSTL Function </title>
</head>
<body>
<c:set var="string" value="Welcome to JSP
Programming"/>
${fn:toLowerCase("hello")} <br/>
${fn:toUpperCase(string)} <br/>
${fn:substring(string, 5, 17)} <br/>
${fn:substringAfter(string, "to")}<br/>
${fn:substringBefore(string, "JSP")}<br/>
${fn:substringBefore(string, "JSP")}<br/>
Length of the String is: ${fn:length(str2)}
${fn:replace(string, "JSP", "JAVA SERVER PAGES")}
</body>
</html>
Formatting tags:
These tags are useful represents the Date
information in different format.
<%@ taglib prefix="c"
uri="http://java.sun.com/JSP/jstl/core"%>
<%@ taglib prefix="fmt"
uri="http://java.sun.com/JSP/jstl/fmt"%>
<html>
<head>
<title>fmt:formatDate</title>
</head>
<body>
<h2>Different Formats of the Date</h2>
<c:set var="Date" value="<%=new java.util.Date()%>" />
<p>
Formatted Time :
<fmt:formatDate type="time" value="${Date}" />
</p>
<p>
Formatted Date :
<fmt:formatDate type="date" value="${Date}" />
</p>
<p>
Formatted Date and Time :
<fmt:formatDate type="both" value="${Date}" />
</p>
<p>
Formatted Date and Time in short style :
<fmt:formatDate type="both" dateStyle="short"
timeStyle="short"
value="${Date}" />
</p>
<p>
Formatted Date and Time in medium style :
<fmt:formatDate type="both" dateStyle="medium"
timeStyle="medium"
value="${Date}" />
</p>
<p>
Formatted Date and Time in long style :
<fmt:formatDate type="both" dateStyle="long"
timeStyle="long"
value="${Date}" />
</p>
sqltags:
<%@ taglib uri="http://java.sun.com/JSP/jstl/core"
prefix="c" %>
<%@ taglib uri="http://java.sun.com/JSP/jstl/sql"
prefix="sql"%>
<html>
<head>
<title>sql:setDataSource Tag</title>
</head>
<body>
<sql:setDataSource var="db"
driver="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@localhost:1521"
user="system" password="manager"/>
</c:forEach>
<sql:update dataSource="${db}" var="count">
INSERT INTO employee VALUES (102,'sam',4000)
</sql:update>
</body>
</html>
Xml tags:
X:out
<%@ taglib prefix="c"
uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="x"
uri="http://java.sun.com/jsp/jstl/xml" %>
<html>
<head>
<title>XML Tags</title>
</head>
<body>
<h2>Vegetable Information:</h2>
<c:set var="vegetable">
<vegetables>
<vegetable>
<name>onion</name>
<price>40/kg</price>
</vegetable>
<vegetable>
<name>Potato</name>
<price>30/kg</price>
</vegetable>
<vegetable>
<name>Tomato</name>
<price>90/kg</price>
</vegetable>
</vegetables>
</c:set>
<x:parse xml="${vegetable}" var="output"/>
<b>Name of the vegetable is</b>:
<x:out
select="$output/vegetables/vegetable[1]/name" /><br>
<b>Price of the Potato is</b>:
<x:out
select="$output/vegetables/vegetable[2]/price" />
</body>
</html>
Output:
Vegetable Information:
Name of the vegetable is: onion
Price of the Potato is: 30/kg
X:parse:
Xyx.xml:
<books>
<book>
<name>Three mistakes of my life</name>
<author>Chetan Bhagat</author>
<price>200</price>
</book>
<book>
<name>Tomorrow land</name>
<author>NUHA</author>
<price>2000</price>
</book>
</books>
Test.jsp
<%@ taglib prefix="c"
uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="x"
uri="http://java.sun.com/jsp/jstl/xml" %>
<html>
<head>
<title>x:parse Tag</title>
</head>
<body>
<h2>Books Info:</h2>
<c:import var="bookInfo" url="xyx.xml"/>
</body>
</html>
X:set
<%@ taglib prefix="c"
uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="x"
uri="http://java.sun.com/jsp/jstl/xml" %>
<html>
<head>
<title>x:set Tag</title>
</head>
<body>
<h3>Books Information:</h3>
<c:set var="book">
<books>
<book>
<name>Three mistakes of my life</name>
<author>Chetan Bhagat</author>
<price>200</price>
</book>
<book>
<name>Tomorrow land</name>
<author>Brad Bird</author>
<price>2000</price>
</book>
</books>
</c:set>
<x:parse xml="${book}" var="output"/>
<x:set var="fragment"
select="$output/books/book[2]/price"/>
<b>The price of the Tomorrow land book</b>:
<x:out select="$fragment" />
</body>
</html>
X:choose/x:when/x:otherwise
<%@ taglib prefix="c"
uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="x"
uri="http://java.sun.com/jsp/jstl/xml" %>
<html>
<head>
<title>x:choose Tag</title>
</head>
<body>
<h3>Books Information:</h3>
<c:set var="xmltext">
<books>
<book>
<name>Three mistakes of my life</name>
<author>Chetan Bhagat</author>
<price>200</price>
</book>
<book>
<name>Tomorrow land</name>
<author>Brad Bird</author>
<price>2000</price>
</book>
</books>
</c:set>
</body>
</html>
X:if
<%@ taglib prefix="c"
uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="x"
uri="http://java.sun.com/jsp/jstl/xml" %>
<html>
<head>
<title>x:if Tags</title>
</head>
<body>
<h2>Vegetable Information:</h2>
<c:set var="vegetables">
<vegetables>
<vegetable>
<name>onion</name>
<price>40</price>
</vegetable>
<vegetable>
<name>Potato</name>
<price>30</price>
</vegetable>
<vegetable>
<name>Tomato</name>
<price>90</price>
</vegetable>
</vegetables>
</c:set>
<x:parse xml="${vegetables}" var="output"/>
<x:if select="$output/vegetables/vegetable/price <
100">
Vegetables prices are very low.
</x:if>
</body>
</html>