6.0
-Comman used demain name
7.0
-Indual user domain name.
8.0
-Multi user domain name.
tags are case sensitive.
a=10;
out.println(a);
%>
-------------------------------------------------------------------------------------------------------------------------------Function:
------------------------------------------------------------------------------------------------------------------------------Example:
<%!
public int display(int x)
{
return(x);
}
%>
<%
out.println(display(10));
%>
-------------------------------------------------------------------------------------------------------------------------1.Jsp Expression:
-------------------------------------------------------------------------------------------------------------------------Contains an expression valied in the page scripting language.
Jsp expression is used to insert java values directly into the output.
Syn:
<%= Java Expression %>
Example:
Curent Time :<%= new java util.Date() %>
Importtant Expression:
*request
-The HttpServletRequest
*response
-The HttpServletResponse
*session
-The HttpSession associated with the request
*out
-The PrintWriter used to send output to the client.
Example :
Your HostName :<%= request.getRemoteHost() %>
Example:
The map file <font color="blue"><%= map.getCount() %></font>Entries
Good guess,but hope,Try<b><%= numguess.getHint() %></b>
------------------------------------------------------------------------------------------------------------------------------2.Jsp Scriptlets:
-------------------------------------------------------------------------------------------------------------------------------Contains a code fragment valid in the page scripting language.
Scriptlets code is inserted in service method. or
Scriptlets let you insert arbitrary code into the servlet method that wi
ll be to generated the page.
Syntax:
<% code fragment %>
<% Java Code %>
Example:
<%
String queryData=request.getQueryString();
out.println("Attached GET data :+queryData);
%>
Example:
<%
String name=null;
if(request.getParameter("name")==null)
{
%>
<%@ include file="error.html" %>
<%
} else {
foo.setName(request.getParameter("name"));
if(foo.getName().equalsIgnoreCase("integra"))
name="acura";
if(name.equalsIgnoreCase("acura")){
%>
------------------------------------------------------------------------------------------------------------------------------Jsp Fragment:
-------------------------------------------------------------------------------------------------------------------------------Example for Mixed template text and Scriptlets.
<% if(Math.random() <0.5) { %>
Have a <b>nice </b>day!
<% } else {%>
Have a <b>lousy</b>day!
<% } %>
or
Get converted to something like:
if(Math.random() <0.5)
{
out.println("Have a<b>nice/<b>day!");
}else
{
out.println("Have a<b>nice/<b>lousy!");
}
------------------------------------------------------------------------------------------------------------------------------Jsp Declarations: (jsp engine :jsp 9.16.10)
------------------------------------------------------------------------------------------------------------------------------Declares a variable or method valied in the page scripting language.
A jsp declartions lets you define methods or fields that get inserted in
to main body of the servlet
class.(outside of the service method processing the request)
Syn:
<%!
Example:
<%!
<%!
<%!
declarations %>
int i=0; %>
int a,b,c; %>
Circle a=new Circle(2.0); %>
%>
iable
<%! Circle a=new Circle(2.0); %>
2.The Expression syntax:
<%=
%>
Result as string
<%= Math.sqrt(2) %>
<%= items[i] %>
Without semicolon.
<%= new java.util.Date() %>
3.The Scriptlets syntax :
<%
%>
Declarations and expression
Allow you to write any number of valid scripting language statement.
<%
String name=null;
if(request.getParameter("name")==null){
%>
Example:
Used in jsp expression,Seriptlets,Declaration and Directive.
<html>
<body>
<ul>
<li><b>Expression :</b><br>
Your HostName :<%= request.getRemoteHost() %>
<li><b>Scriptlet
:</b><br>
<% out.println("Attached Get data :"+request.getQueryString()); %>
<li><b>Declaration (plus expression) :</b><br>
<%! private int accessCount=0; %>
Accesses to page since server reboot :<%= ++ accessCount %>
<li><b>Directive (plus expression)
:</b><br>
<%@ page import="java.util.*" %>
Current date :<%= new Date() %>
</ul>
</body>
</html>
Output :
Expression :
Your HostName :localhost
Scriptlet :
Attached Get data :null
Declaration (plus expression) :
Accesses to page since server reboot :1
Directive (plus expression) :
Current date :Tue Dec 07 15:55:14 IST 2004
Jsp Directves:
-------------------------------------------------------------------------------------------------------------------------------A jsp directives effects the overall structure of the servlet class.It u
sually has the following form
<%@ directive attribute1="value1" attribute2="value2 .............%>
Jsp Page Directive:
Defines attribute that apply to an entire jsp page.
The page directive lets you define one or more of the following case sen
sitive attribute
Syntax:
<% page [language="java"] [extends="package.class|package.*},........."]
import ="package.class" or
import ="package.class1,.............packahe.classN"
Example:
<%@
<%@
<%@
<%@
page
page
page
page
import="java.util.*,java.long.*" %>
buffer="5kb" autoFlush="false" %>
errorPage="error.jsp " %>
import="java.util.*" %>
Import Attribute:
contentType="MIME-Type" or charSet="Character-Set"
Default:
<%@ page contentType="text/plain" %>
<% response.setContentType("text/plain"); %>
isthreadSafe="true/false"
*session="true/false"
*buffer="sizekb/none"
*autoflush="true/false"
*extends="package.class"
*info="message"
*errorPage="url"
*isErrorpage="true/false"
*language="java"
------------------------------------------------------------------------------------------------------------------------------Taglib Directive:
-------------------------------------------------------------------------------------------------------------------------------Defines a tag library and prefix for the custom tags used in the jsp pag
e.
Syntax:
<%@ taglib url="UrlToTaglibrary" prefix="tagprofix" %>
Example:
<@% taglib uri="http://www.jspcontrol.com/tags" prefix="public" %>
<public:loop>
--------------</public:loop>
------------------------------------------------------------------------------------------------------------------------------The Jsp include Directive:
--------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------Action:
-------------------------------------------------------------------------------------------------------------------------------You can dynamically insert a files,reuse javaBeans components,forward th
e user to another page,
or generate html for the java plugin.
Available actions include:
jsp:include
-Include a file at the time the page is requested.
jsp:useBean
-Find or instantiate a java bean
Note : UseBean - Callin
g java class file.
jsp:setProperty -Set the prop of a javabean
jsp:getProperty -Insert the prop of a javabean into the output
jsp:forward
-Forward the requested to a new page
jsp:plugin
-Generate browser-specific code that makes an object or
embed tag for the java plugin.
Plugin: Including Applet file.
-------------------------------------------------------------------------------------------------------------------------------The Jsp:include Action:
-------------------------------------------------------------------------------------------------------------------------------Include either a static or dynamic file in a jsp file.
This action lets you insert files into the page being generated.
Syn:
<jsp:include page="relative url" flush="true/false" />
<jsp:include page="{relativeUrl|<%= expression %>}" flush="true" />
Example:
<jsp:include page="scripts/login.jsp" />
<jsp:include page="copyright.html" />
<jsp:include page="/index.html" />
Example:
WhatNew.jsp
<html>
<body>
<ol>
<li><jsp:include page="news/item1.html" flush="true" />
<li><jsp:include page="news/item2.html" flush="true" />
<li><jsp:include page="news/item3.html" flush="true" />
<li><jsp:include page="news/item4.html" flush="true" />
</ol>
</body>
</html>
-------------------------------------------------------------------------------------------------------------------------------jsp:useBean Action:
-------------------------------------------------------------------------------------------------------------------------------Syn:
<jsp:useBean id="name" class="package.class" />
Example:
BeanTest.jsp
<html>
<body>
<jsp:useBean id="text" class="hall.SimpleBean" />
<jsp:setProperty name="test" property="message" value="Hello www" />
>h1><i>Message<i>
<jsp:getProperty name="text" property="message" />
</i></h1>
</body>
</html>
SimpleBean.java :
package hall;
public class SimpleBean
{
private String message="No Message Specified";
public String getMessage()
{
return(message);
}
public void setMessage(String message)
{
this.message=message;
}
}
More jsp:useBean Details:
<jsp:useBean id="name" class="package.class" />
jsp:setProperty
jsp:getproperty
<jsp:useBean.........>
Body
</jsp:useBean>
------------------------------------------------------------------------------------------------------------------------------The jsp:setProperty Actions:
------------------------------------------------------------------------------------------------------------------------------Syn:
<jsp:useBean id="myname"................................./>
-------------------------------------<jsp:setProperty name="myname" prpperty="someProperty".............../>
or
</jsp:useBean>
Attribute of jsp:setProperty:
*name
-Before the jsp prop
*property
-All request match parameters passed
*value
-Optional attribute (auto string conversted all variable)
*param
-Optional attribute (override param)
Ex:
<jsp:setProperty name="orderBean" prpperty="numberOfItems" param=numItem
s" />
Example:
<html>
<body>
<p>
<jsp:useBean id="primeTable" class="hall.numberedPrimes" />
<jsp:setProperty name="primeTable" property="numDigits" />
<jsp:setProperty name="primeTable" property="numPrimes" />
Same <jsp:getproperty name=PrimeTable" property="numDigits" />
Digit Primes:
<jsp:getProperty name="primeTable" property="numberedList" />
</body>
</html>
------------------------------------------------------------------------------------------------------------------------------The jsp:getProperty Action:
-------------------------------------------------------------------------------------------------------------------------------Gets the values of a bean property so that you can display it in a resul
t page.
Retives the values of a bean property.
Syntax:
<jsp:getProperty name="BeanInsertanceName" property="propertyName" />
Example:
<jsp:useBean id="calender" scope="page" class="employee.Calender" />
<h2>
Calender of <jsp:getProperty name="Calender" property="username" />
</h2>
Eg:
<jsp:useBean id="itemBean".........../>
-------------------------<li>
<li>Number of items:
<jsp:getproperty name="itemBean" property="numitems" />
<li>Cost of each:
<jsp:getProperty name="itemBean" property="unitcost" />
</ul>
------------------------------------------------------------------------------------------------------------------------------The jsp:forward Action:
-------------------------------------------------------------------------------------------------------------------------------Forward : Same to Redirect
Forwards a client request to an html file,jsp file or servlet for proces
sing.
This action lets you forward the request to another page.
Syntax:
<jsp:forward page="{relativeUrl | <%= expression %>}" />
Eg:
<jsp:include page="url"/> or <jsp:forward page="url"/>
<jsp:forward page="/gan/user.jsp" />
<jsp:forward page="<%= SomeJavaExpression %>" />
------------------------------------------------------------------------------------------------------------------------------The jsp:plugin Action:
-------------------------------------------------------------------------------------------------------------------------------This action lets you insert the browser specifies object or embed elemen
t needed the specify
that browser run an applet using the java plugin.
<tr>
<td height="150" width="150"> </td>
<td width="250"> <td>
</tr>
<td width="150"> </td>
<td align="right" width="250">
<img src="Arose15.gif"></td>
</td>
</table>
<br>
</body>
<html>
helloworld.jsp
<%@ page info="A Hello Wod Example " %>
<html>
<body bgcolor="#ffffff" background="bg14.gif">
<%@ include file="banner.htm" %>
<table>
<tr>
<td width=150> </td>
<td width=250 align=right><h1> Hello World!</h1></td>
</tr>
</table>
</body>
</html>
---------------------------------------------------------Example:2
Client and Server side Programming:
1.Input.html
<html>
<form action="http://localhost:7001/display.jsp">
<pre>
Enter The Sno : <input type="text" name="t1">
Enter The Name : <input type="text" name="t2">
Enter The Salary: <input type="text" name="t3">
<input type="submit" value="Ok">
</form>
</html>
2.display.jsp
<%
String s1=request.getParameter("t1");
String s2=request.getParameter("t2");
String s3=request.getParameter("t3");
out.println(s1);
out.println(s2);
out.println(s3);
%>
------------------------------------------------------------------------------------------------------------------------Example: 3
color.html
<html>
<body>
<form action="http://localhost:7001/value.jsp" method="post" >
<br>
<input type="radio" name="red" value="red" checked >RED
<br>
<br>
<input type="radio" name="blue" value="blue" >BLUE
<br>
<br>
<input type="radio" name="green" value="green" >GREEN
<input type="submit" name="submit" value="enter" >
</BODY>
</HTML>
value.jsp
<html>
<BODY bgColor="red" >
<font face="arial, Courier"><font size= +2>
<br>
<b>
well hai
<%= request.getParameter("red")%>
!!!
</b>
</body>
</html>
------------------------------------------------------------------------------------------------------------------------Example :4
Html File
:GetjspOrder.html
<html>
<h2>Enter an Order</h2>
<body>
<form action=getOrder.jsp method=get>
Order :<input type=text name="Order" size=20>
<input type=submit>
</form>
</body>
</html>
Jsp File :getOrder.jsp
<html>
<body>
<h2><strong>
I'd like to order<br>
<%-- Get a Parameter --%>
<%
out.println(" "+request.getParameter("Order"));
%>
</strong></h2>
</body>
</html>
-------------------------------------------------------------------------------------------------------------------------
:PostjspOrder.html
<html>
<body>
<form action="http://localhost:7001/PostOrder.jsp" method=post>
<strong>Name
:</strong>
<input type=text name=name size=20><br>
<strong>Password :</strong>
<input type=password name=password size=12><p
>
<strong>Books
:</strong>
<select name=flaver size=3>
<option>Vanilla
<option>Checolate
<option>Strewberry
</select><p>
<strong>Toppings :</strong><p>
<input type=checkbox name=topping
<input type=checkbox name=topping
<input type=checkbox name=topping
<input type=checkbox name=topping
:PostOrder.jsp
String result=request.getParameter("flaver");
String[] kinds=request.getParameterValues("topping");
int i;
%>
<html>
<body>
<h1><strong>Hi <%= message %>
</strong></h1>
<h2>
Get your Order for <%= result %> Ice Cream <br>
<% if(kinds==null) %>
with no topping.
<% else { %>
with <ul>
<% for(i=0;i<kinds.length;i++) { %>
<li> <%= kinds[i] %>
<% } %>
</ul>
<% }
result=request.getParameter("place");
if(result.equals("Eat here"))
%>
to Eat Here.
<% else %>
to go.
</h2>
</body>
</html>
----------------------------------------------------------Form Data
----------------------------------------------------------request
jsp engine & web server
request |
|
|
jsp file
client
|
|
|
|
request|
<-request
component
request ----------------object---->
Object
--------------------------------------------------------Simple user Applications
--------------------------------------------------------1.hellouser.jsp
<%@ page import="hello.NameHandler" %>
<jsp:useBean id="mybean" scope="page" class="hello.NameHandler" />
<jsp:setProperty name="mybean" property="*" />
<html>
<head><title>Hello user</title></head>
<body bgcolor="#ffffff" background="bg14.gif">
<%@ include file="banner.htm" %>
<table border="0" width="700">
<tr>
<td width="150"> </td>
<td width="550">
<h1>My name is Rose.What is your's ?</h1>
</td>
</tr>
<tr>
<td width="150"> </td>
<td width="550">
<form method="get">
<input type="text" name="username" size="25">
<br>
<input type="Submit" value="Submit">
<input type="Reset" value="Reset">
</td></tr></form></table>
<%
if(request.getParameter("username")!=null){
%>
<%@ include file="response.jsp" %>
<%
}
%>
</body>
</html>
2.response.jsp
<table border="0" width="700">
<tr>
<td width="150"> </td>
<td width="550">
<h1>Hello,<jsp:getProperty name="mybean" property="username" />
</h1>
</td>
</tr>
</table>
3.NameHandler.java
package hello;
public class NameHandler
{
private String username;
public NameHandler()
{
username=null;
}
public void setUsername(String name)
{
username=name;
}
public String getUsername()
{
return username;
}
}
1.Two file (jsp file) Save a folder :Default web application
2.Package file :web-inf - classess-hello (folder)-NameHandler.class
Save a programs :
1.Compile the java prg
2.Save a package
bea-default web app-web-inf-classess-hello(package name)
-NameHandler.class
------------------------------------------------------------------------------------------------------------------------------------2.Order Processing used for JavaBean
-----------------------------------------------------------------------------------------------------------------------------------1.postorder.html
<html>
<body>
<form action="http://localhost:7001/PostOrderBean.jsp" method=post>
<strong>Name
:</strong>
<input type=text name=name size=20><br>
<strong>Password :</strong>
<input type=password name=password size=12><p
>
<strong>Flavor
:</strong>
<select name=flavor size=3>
<option>Vanilla
<option>Checolate
<option>Strewberry
<option>Flavor
</select><p>
<strong>Toppings :</strong><p>
<input type=checkbox name=toppings
<input type=checkbox name=toppings
<input type=checkbox name=toppings
<input type=checkbox name=toppings
}
public void setToppings(String[] t)
{
toppings =t;
}
public String[] getToppings()
{
return toppings;
}
public void setFlavor(String f)
{
flavor=f;
}
public String getFlavor()
{
return flavor;
}
public void setPlace(String p)
{
place=p;
}
public String getPlace()
{
return place;
}
}
PostOrderBean.jsp
<!--Echose the user's order made in a post request-->
<jsp:useBean id="sundae" class="order.OrderBean" />
<jsp:setProperty name="sundae" property="*" />
<%
String[] kinds;
int i;
%>
<html>
<body><h2><stong>Hi :<jsp:getProperty name="sundae" property="name" />
</strong></h2>
Got your order for :<jsp:getProperty name="sundae" property ="flavor"
/>Ice Cream<br>
<% kinds=sundae.getToppings();
if(kinds==null) %>
with no toppings.
<% else { %>
with<ul>
<% for(i=0;i<kinds.length;i++) { %>
<li><%= kinds[i] %>
<% } %>
</ul>
<%}
String res=sundae.getPlace();
if(res.equals("Eat Here"))
%>
To eat Here.
<% else %>
To go.
</body>
</html>
============================================================
Data Base Example:
-----------------------------------------------------------Create a database Name: (odbc)
Create a User Dsn Name.(dsn Name :gan)
Database Name :emp
Table Name
:salary (eno,ename,bpay)
Example:
<%
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con= DriverManager.getConnection("Jdbc:Odbc:gan");
Statement smt=con.createStatement();
ResultSet res=smt.executeQuery("select*from salary");
out.println("Data base connected");
PreparedStatement ps=con.prepareStatement("insert into salary values(?,?,?)
");
String e=request.getParameter("t1");
String en=request.getParameter("t2");
String bp=request.getParameter("t3");
ps.setString(1,e);
ps.setString(2,en);
ps.setString(3,bp);
ps.executeUpdate();
out.println("One Recorded Added");
ps.close();
con.close();
%>
</body>
</html>
-----------------------------------------------------------Update the Recored
-----------------------------------------------------------input2.htm
<html>
<body>
<form action="http://localhost:7001/data2.jsp">
<pre>
Enter The Sno
: <input type="text" name="t1">
Enter The Salary : <input type="text" name="t2">
<input type="submit" value="Ok">
</form>
</body>
</html>
data3.jsp
<%@ page import ="java.sql.*;" %>
<html>
<body>
<%
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con= DriverManager.getConnection("Jdbc:Odbc:gan");
Statement smt=con.createStatement();
out.println("Data base connected");
PreparedStatement ps=con.prepareStatement("update salary set eno=(?)
where bpay=(?)");
String e=request.getParameter("t1");
String bp=request.getParameter("t3");
ps.setString(1,e);
ps.setString(2,bp);
ps.executeUpdate();
out.println("One Recorded Updated");
ps.close();
con.close();
%>
</body>
</html>
-----------------------------------------------------------Delete a Record
-----------------------------------------------------------Input1.htm
<html>
<body>
<form action="http://localhost:7001/data4.jsp">
<pre>
Enter The Sno
: <input type="text" name="t1">
<input type="submit" value="Ok">
</form>
</body>
</html>
data4.jsp:
<%@ page import ="java.sql.*;" %>
<html>
<body>
<%
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con= DriverManager.getConnection("Jdbc:Odbc:gan");
Statement smt=con.createStatement();
PreparedStatement ps=con.prepareStatement("delete from salary where eno=(?
)");
String e=request.getParameter("t1");
ps.setString(1,e);
ps.executeUpdate();
out.println("One Recorded Delected");
ps.close();
con.close();
%>
</body>
</html>
--------------------------------------------------------------