Sunteți pe pagina 1din 11

Introduction to JSP :

==================
1. Java Server Page is a technology based on the java language and enables
the development of DYNAMIC web sites.
2. It was developed by SUN Microsystems to allow server side development.
3. JSP code embedded with html files with special tag that containing the j
ava source code to
provide the Dynamic Content.
4. JSP Code runs on the Web Server in the JSP Servlet Engine. The JSP Servl
et Engine dynamically generates the HTML output and send to the Client s
browser.
Main reasons to use JSP
-----------------------------
multi platform
component reuse by using java beans and EJB

JSP Tags :
Declaration Tag
---------------------
<%!
Variable declaration . . .
Creating new Instance(s) . . .
Class Definition . . .
Method Declaration . . .
%>

Expression Tag :
---------------------
<%= any valid Expression %>
<%= Hello %>
<%=a+b%>
= sign refers that out.print(). Out is an object of JspWriter Class.

Directive Tag :
-----------------
A jsp directive gives special meaning about the current page to the JSP Engine b
efore process.
There is 3 types of Directives.They are:
1. Page - Processing information for this page
2. Include - files to be included
3. Tag Library - tag library to be used in this page
Page :
------
<%@ page language= java %>
<%@ page extends= parent class %>
<%@ page import= java.util.* %>
java.lang.*;
javax.servlet.*;
javax.servlet.jsp.*;
javax.servlet.http.*; (all are implicit)
<%@ page session= true/false %> (Default session= true )
<%@ page buffer= 8kb %> ( buffer= none send immediately )
<%@ page autoflush= true/false %> (def is true.)
<%@ page isThreadSafe= true/false %>
if it is true that Thread Instance can manage multiple client reqs, or o
therwise.
<%@ page info= Comments %>
<%@ page errorpage= url %>
<%@ page iserrorpage= true/false %>
set true current page make as a Error Page
<%@ page contentType= text/html %>
Predefined Variables
----------------------------
request
response
out
session
application
config
pageContext
page

The jsp:useBean Action


---------------------------------
<jsp:useBean att=val*/> or
<jsp:useBean att=val*>
...
</jsp:useBean>
Find or build a Java Bean.
Possible attributes are:
------------------------------
id="name"
scope="page|request|session|application"
class="package.class"
type="package.class"
beanName="package.class"
The jsp:setProperty Action
-----------------------------------
<jsp:setProperty att=val*/>
Set bean properties, either explicitly or by designating that value comes from
a request parameter.
Legal attributes are
name="beanName"
property="propertyName|*"
param="parameterName"
value="val"
The jsp:getProperty Action
------------------------------------
<jsp:getProperty
name="propertyName"/>
Retrieve and output bean properties.
===================================================================
<html>
<title>
JSP Site
</title>
<body>
<center>
<h2>This is from JSP Content</h2>
Page Requested Date is : <%=new java.util.Date() %>
<br>
Expression:<%=2*110%>
<br>
Sqrt of 81=<%=Math.sqrt(81)%>
</center>
</body> </html>
===============================================================
<html>
<title>
JSP Site
</title>
<body>
<center>
<h2>This is from JSP Content</h2>
<h3>Variable Declaration</h3>
<%!
// Declaration section
int a=81;
String n="WelCome";
%>
<br>
Expression:<%=a*10%>
<br>
Sqrt of 81=<%=Math.sqrt(a)%>
<br>
Upper String=<%=n.toUpperCase()%>
<br>
Sub String=<%=n.substring(0,4)%>
<br>
</center></body></html>
===============================================================
<html>
<title>
JSP Site
</title>
<body>
<center>
<h2>This is from JSP Content</h2>
<h3>Method Declaration & Calling </h3>
<%!
int sqrt(int x)
{
int c;
for(c=1;c<=x;c++)
{
if (c*c==x)
break;
}
return c;
}
%>
<br>
sqrt of 144=<%=sqrt(144)%>
<br>
sqrt of 441=<%=sqrt(441)%>
</center>
</body>
</html>
===============================================================
<html>
<title>
JSP Site
</title>
<body>
<center>
<h2>This is from JSP Content</h2>
<!-- out is the object of JSPWriter class -->
<%
out.println("<h3>Scriptlet Tag</h3>");
out.println("Line Cont1 ...");
out.println("Line Cont2 ...\n");
out.print("Line <br>Cont3 ...");
out.print("Line Cont4 ...");
%>
</center>
</body>
</html>
================================================
<html>
<title>
JSP Site
</title>
<body>
<center>
<h2>This is from JSP Content</h2>
<%!
int x=10;
%>

<%
out.print("<h3>Control Statements</h3>");
if (x%2==0)
{ out.print("Even no"); }
else
{out.print("Odd no");}
out.print("<br>");

for(int x=1;x<=5;x++)
{out.print(x);}
out.print("<br>");
while(x>=1)
{
out.print(x);
x--;
}
%>
<table border="1" bgcolor="gold" >
<tr>
<td>Column 1
<td>Column 2
</tr>
<%
for(int t=5;t<=10;t++)
{
out.print("<tr>");
out.print("<td>"+t+"</td><td>"+(t+10)+"</td>" );
out.print("</tr>");
}
%>
</table>
</center>
</body>
</html>
===============================================================
<html>
<title>
JSP Site
</title>
<body>
<center>
<h2>This is from JSP Content</h2>
<%!
int a[]={5,14,23};
String b[][]={
{"Sun","MicroSystems"},
{"Microsoft","Corporation"}
};
%>
<%
out.print("<h1>Array Example</h1>");
for(int i=0;i<a.length;i++)
{
out.print("a["+i+"] = "+a[i]+"<br>");
}
for(int r=0;r<2;r++)
{
out.print("<br>");
for(int c=0;c<2;c++)
{
out.print("b["+r+"]["+c+"] = "+b[r][c]+"\t");
}
}
%>
</center>
</body>
</html>
===============================================================
<%@ page import="java.util.*" %>
<html>
<title>
JSP Site
</title>
<body>
<center>
<h2>This is from JSP Content</h2>
<%!
Vector shop=new Vector();
Enumeration em=shop.elements();
%>
<%
shop.addElement("Lux");
shop.addElement("CloseUP");
shop.addElement("BodyWash");
shop.addElement("Mob");
shop.addElement("Gas Lighter");
shop.addElement("MatchBox");
shop.addElement("Candle");
out.print("<h1>Vector Example</h1>");
while(em.hasMoreElements())
{
out.print("<br><font color='red'>"+em.nextElement()+"</font>");
}
shop.removeElement("Mob");
shop.removeElement("BodyWash");
out.print("<br>After elements removed");
Enumeration em=shop.elements();
while(em.hasMoreElements())
{
out.print("<br><font color='green'>"+em.nextElement()+"</font>");
}
%>
</center>
</body>
</html>
===============================================================
<html>
<body>
<center>
<%! int count=0;
%>
<%
out.print("<h1>This is from JSP</h1>");
out.print("count :"+(count++));
Cookie ck1=new Cookie("password","secret");
Cookie ck2=new Cookie("cardno","12ASD345");
response.addCookie(ck1);
response.addCookie(ck2);
out.print("Cookies added in your system.<br>");
out.print("<a href='http://localhost:8080/jsp-examples/sudha/cook2.jsp'>
Read my Cookies</a>");
%>
</center>
</body>
</html>
===============================================================
<%@ page import="java.lang.*" %>
<html>
<body>
<center>
<%
out.print("<h1>This is from JSP</h1>");
out.print("<h3>Reading Cookies</h3>");
try{
Cookie user[]=request.getCookies();
out.print("Length ="+user.length+"<br>");
for(byte i=0;i<user.length;i++)
{
Cookie ck=user[i];
if (ck.getValue()!=null)
{
out.print("<br>Cookie name="+ck.getName());
out.print(" - Cookie Value="+ck.getValue());
out.print("<br>");
}
}
}
catch(Exception e)
{ out.print("<font color='red'>Cookie has expired.Please add if you need
</font>"); }
out.print("<br><a href='http://localhost:8080/jsp-examples/sudha/cook3.j
sp'>Delete my Cookie</a>");
out.print("<br><a href='http://localhost:8080/jsp-examples/sudha/cook1.j
sp'>Add Cookie</a>");
%>
</center>
</body>
</html>
===============================================================
<html>
<body>
<center>
<%
out.print("<h1>This is from JSP</h1>");
out.print("<h3>Deleting Cookies</h3>");
try{
Cookie c[]=request.getCookies();
for(byte i=0;i<c.length;i++)
{
Cookie ck=c[i];
ck.setMaxAge(0);
}
}catch(Exception e)
{out.println("<h2>"+e+"</h2>");}

out.print("<br><br><a href='http://localhost:8080/jsp-examples/sudha/cook1.js
p'>Read my Cookie</a>");
%>
</center>
</body>
</html>
===============================================================
<%@ page import="java.util.*" %>
<html>
<body>
<center>
<%
out.print("<h1>This is from JSP Engine</h1>");
out.print("<h4>JSP Sessions</h4>");
HttpSession hs=request.getSession(true);
hs.putValue("access",new Date());
hs.putValue("user","sree");
out.print("<br>Session ID :"+hs.getId());
out.print("<br>Session Value 1:"+hs.getValue("user"));
out.print("<br>Session Value 2:"+hs.getValue("access"));
%>
</center>
</body>
</html>
===============================================================
<%@ page import="java.lang.*" %>
<%@ page import="java.util.*" %>
<html>
<body>
<center>
<%!
int count=0;
String val;
%>
<%
out.print("<h1>This is from JSP Engine</h1>");
out.print("<h4>JSP Sessions</h4>");
HttpSession ses=request.getSession(true);
out.print("<br>Session ID : "+ses.getId());
count++;
out.print("<br>"+count);
if (ses.getValue("lastaccess")==null)
{
out.print("<br>New Session started");
ses.putValue("lastaccess","1");
}
else
{
val=""+ses.getValue("lastaccess");
ses.putValue("lastaccess",""+(Integer.parseInt(val)+1));
}
out.print("<br>This page has "+ses.getValue("lastaccess")+" time(s) requested.")
;
out.print("<br>Created Time : "+new Date(ses.getCreationTime()));
out.print("<br>Last Accessed : "+new Date(ses.getLastAccessedTime()));
ses.setMaxInactiveInterval(10); // (10) seconds
out.print("<br>Timeout : "+ses.getMaxInactiveInterval());
%>
</body>
</html>
===============================================================
<%@ page import="java.util.*" %>
<html>
<body bgcolor="steelblue" text="white">
<center>
<%
Calendar cl=Calendar.getInstance();
int hr=cl.get(Calendar.HOUR_OF_DAY);
out.print("<h1>This is from JSP Engine</h1>");
if (hr<12)
out.print("Good Morning Surfer!!");
else if(hr<18)
out.print("Good Afternoon Surfer!!");
else
out.print("Good Evening Surfer!!");
%>
</center>
</body>
</html>
===============================================================
<%! public class student
{
int no;
String name;
public void getdata(int tno,String tname)
{
no=tno;
name=tname;
}
public String display()
{
return("No :"+no+"Name :"+name);
}
}
%>
<%
student s=new student();
s.getdata(1,"sree");
out.println(s.display());
%>
===============================================================
<html>
<body>
<%@ page import="java.sql.*"%>
<table border=2>
<%
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:sudha","sa","");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from stujsp");
%>
<tr><th>number</th>
<th>name</th>
<%
while(rs.next())
{%>
<tr>
<td><%=rs.getInt("no")%></td>
<td><%=rs.getString("name")%></td>
</tr>
<%
}
rs.close();
con.close();
%>
</table>
</body>
</html>
===============================================================

S-ar putea să vă placă și