Anda di halaman 1dari 22

-------------------------------------------------------------------------------------------------------------------------------Jsp-Java Server Page

------------------------------------------------------------------------------------------------------------------------------Java server page(Jsp) :


* Version
:7.0,8.0
* Tag Oriented Programming but not only class file.
* Embeting html files.
Jsp
Jsp
jsp
Jsp

6.0
-Comman used demain name
7.0
-Indual user domain name.
8.0
-Multi user domain name.
tags are case sensitive.

-----------------------------------------------------------------------------------------------------------------------------Jsp Scripting Elements:


------------------------------------------------------------------------------------------------------------------------------Output comment:
1.comment:
Document the jsp page is not sent to client.
Syn:
<!-Comment
[<%= expression %> ] -->
Example:
<!-- My First Programs -->
<!-- This page was loaded on
<%= (new java.util.Date()).toLocaleString() %> -->
Praticuls:
<html>
<body>
<h2>A text of comment text<h2>
<%-- This comment will not be visible in this page source --%>
</body>
</html>
-----------------------------------------------------------------------------------------------------------------------------Variable Declaration:
-----------------------------------------------------------------------------------------------------------------------------1.local Variable:
<%
// Local Variable Declaration
int a;
a=10;
out.println(a);
%>
2.Global declaration:
<%! --------------%>
Example:
<%! int a; %>
<%

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); %>

<% java code %>


Example:
<%! private int accessesCount=0; %>

Accesses to page since server reboot:


<%= ++ acessesCount %>
--------------------------------------------------------------------------------------------------------------------------------------How to add Scripting
--------------------------------------------------------------------------------------------------------------------------------------1.The Declaration Syntax :
<%!-Declare variable and method
<%! int i=0; %>
<%! int a,b,c; %>

%>

Allowed in one or more var

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

------------------------------------------------------------------------------------------------------------------------------Mingling scripting elements with Tags:


-------------------------------------------------------------------------------------------------------------------------------1.<% } else { %>
->Closing the seriptlet before the tags start.
2.<% } %>
->Reopening the scriptlet to close the language block.
------------------------------------------------------------------------------------------------------------------------------Directive
------------------------------------------------------------------------------------------------------------------------------1.Page Directive
2.Include Directive
1.Page Directive: (Including page attribute)
<%@page-----------------%>
* languaage
-java
*import
-"import packages"
*extends
-"package-class"
*content type
-mine type"
*error page
-page Error Ex: eserrorpage:"true/false"
*eserror page
-"true/false"
*session
-"true/false" Default :True
*buffer
-"size"
*autoflush
-"true/false" Default :true
Example:
<%@page import="java.sql.*" %>
<%@ page import="java.io.*" %>
2.Include Directive:
<%@ include file="url">
<%@ include file="http://localhost:7001/copyright.html" %>
Tags:
* script lib
* declartive
* commands
* expression
Directive:
* page
* include
* taglib
Action Tags:
* userBean
* getproperty
* setProperty
* include
* forward
* plugin
Example:
<jsp:useBean>
------------------------</jsp:useBean>
--------------------------------------------------------------------------------------------------------------------------------

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:
--------------------------------------------------------------------------------

------------------------------------------------Includes a file of text or code in the jsp source file.


The directive lets you include files at the time the jsp page is transla
ted into a servlet.
<% include file="relative url" %>
Example:
<body>
<%@ include file="/csc.html" %>
Example: Include.jsp
<html>
<body bgcolor ="white">
<font color="blue">
The Current Date and Time are
<%@ include file="date.jps" %>
</font>
</body>
</html>
date.jsp:
<%@ page import="java.util.*" %>
<%= (new java.util.Date()).toLocaleString() %>
------------------------------------------------------------------------------------------------------------------------------Example
for using Scripting and Directives Elements:
(Used for jsp expression,scriptlets.declarations and Directives)
<html>
<body>
<ul>
<li><b>Expression .</b><br>
Your HostName :<%= request.getRemoteHost() %>
<li><b>Scriptlets .</b<br>
<% out.println("Attached GET data :"+request.getQueryString()); %>
<li><b>Declaration (plus expression).</b><br>
<%! private int accessesCount=0; %>
Accesses to page since server reboot :<%= ++accessesCount %>
<li><b>directive.(plus expression).</b><br>
<%@ page import="java.util.*" %>
Current Data :<%= new Date() %>
</ul>
</body>
</html>
------------------------------------------------------------------------------------------------------------------------------Jsp predefined Variable :
-------------------------------------------------------------------------------------------------------------------------------there are 8 variable of jsp
*request
-Parameter (get,post ,head and getParameter)
*response
-http and out
*out
-PrintWriter
*session
-Page directive
*application
-getServketConfig().getContext()
*config
-Page
*pageContext
-regular servlet/jsp engine
*page
-placeholder

------------------------------------------------------------------------------------------------------------------------------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.

Plugin: Including Applet file.


-------------------------------------------------------------------------------------------------------------------------------Commants and Character Quating convertions:
-------------------------------------------------------------------------------------------------------------------------------<%-Commands --%>
-jsp commant
<!-Commands-->
-Html elements
<\%
-Used in template text
%\>
-Used in scripting elements.
\'
-A single quote
\"A
-Double quote
%\>
-%> in an attribute
<\%
-<% in a attribute
------------------------------------------------------------------------------------------------------------------------------writing a javaserver page:
-------------------------------------------------------------------------------------------------------------------------------A jsp page looks a lot like an html or xml pages.
Jsp Tags:
1.commands
: <!-- commands
-->
: <%-- commands
--%>
2.Directives
:jsp page is translated into a servlet.
3.Declarations
:Set up variable for later use in expression or scriptle
ts.
4.Expression
:You can dynamically calculate values to be inserted dir
ectly into the jsp page.
:Eg:insert the client ip address directly into the gener
ated html.
5.Seriptlets
:Use scriptlet to embed any piece of java code into the
page.
6.JSP
:Tag libraries are new in the jsp 6.16.11
7.jsp
:Forward and jsp:include instruct the jsp engine.
8.jsp:use
:Bean,jsp:setProperty and jsp:getProperty allow
javaBeans to be embeded and
manipulated inside a java page.
9.jsp
:Plugin automatically download the appropriate java plug
in to the client to execute.
Example:
<!-- form.jsp -->
<%@ page language="java" info="form processing page " %>
<html>
<body>
<%! String cities[],month; %>
<p> The Cities Selected are :</p>
<%
cities=request.getParameterValue("city");
month=request.getParaneter("month");
if(cities != null)
{
%>
<ul>
<%
for(int i=0;i<cities.length;i++)
{
%>

<li><%= cities[i] %>


<%
}
%>
</ul>
<%
}
else{
%>
<p>None selected.</p>
<%
}
%>
<br>
<p>and the selected month is :<b><%= month %></b></p>
</body>
</html>
<!-- form.html>
<html>
<body>
<form method=post action=form.jsp>
<input type=checkbox name=city value='new yor'>New York<br>
<input type=checkbox name=city value='bombay'>Bomboy<br>
<input type=checkbox name=city value='delhi'>Delhi<br>
<input type=checkbox name=city value='mumbi'>Mumbi<br>
<input type=checkbox name=city value='gujarathi'>Gujarathi<br>
<br>
<input type=radio name=month value='jan'>Jan<br>
<input type=radio name=month value='feb'>Feb<br>
<input type=radio name=month value='mar'>Mar<br>
<input type=radio name=month value='apr'>Apr<br>
<input type=submit value=submit>
</form>
</body>
</html>
------------------------------------------------------------------------------------------------------------------------Example programming
------------------------------------------------------------------------------------------------------------------------Example:1
<%
out.println("Welcome First Jsp Programs");
%>
Save a File :
e:\bea\user_projects\rams\application\defaultApplication\First.j
sp
Run a File : Http://localhost:7001/First.jsp
----------------------------------------------------------------Example:
banner.htm
<html>
<body>
<table border="0" width="400" cellspacing="0" cellapding="0">

<tr>
<td height="150" width="150">&nbsp;</td>
<td width="250">&nbsp;<td>
</tr>
<td width="150">&nbsp;</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>&nbsp;</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>
-------------------------------------------------------------------------------------------------------------------------

Example for Expresion Tag:


------------------------------------------------------------------------------------------------------------------------<%= expression %>
<%=>
out.println(x);
<%=x%>
Syntax:
<% if(VariableName==null) %>
with <<message>>
<% else { %>
with <ul>
<% for(i=0;i<variableName.length;i++) %>
<li> <%= variableName[i] %>
<% } %>
</ul>
Example:1
Html File

: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

value="Hot Fudge">Hot Fudge<br>


value="Ice Fudge">Ice Fudge<br>
value="Light Fudge">Light Fudge<br>
value="Strong Fudge">Strong Fudge<br>

<input type=radio name=place value="Eat here">Eat Here


<input type=radio name=place value="Take out">Take Out
<input type=submit value="Order">
<input type=reset>
</form>
</body>
</html>
Jsp File

:PostOrder.jsp

<!--Chose the user's order made in a post request -->


<%
String message=request.getParameter("name");

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">&nbsp;</td>
<td width="550">
<h1>My name is Rose.What is your's ?</h1>
</td>
</tr>
<tr>

<td width="150">&nbsp;</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">&nbsp;</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

value="Hot Fudge">Hot Fudge<br>


value="Ice Fudge">Ice Fudge<br>
value="Light Fudge">Light Fudge<br>
value="Strong Fudge">Strong Fudge<br>

<input type=radio name=place value="Eat here">Eat Here


<input type=radio name=place value="Take out">Take Out
<input type=submit value="Order">
<input type=reset>
</form>
</body>
</html>
orderBean.java
package order;
public class OrderBean
{
private String name="Name";
private String password="Password";
private String flavor="Flavor";
private String[] toppings={"Toppings"};
private String place="Place";
public void setName(String n)
{
name=n;
}
public String getName()
{
return name;
}
public void setPassword(String p)
{
password=p;

}
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:

data1.jsp (display all records)

<%@ page import ="java.sql.*;" %>


<html>
<body>
<%
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");
while(res.next())
{
out.println(res.getString(1)+"<br>");
out.println(res.getString(2)+"<br>");
out.println(res.getString(3)+"<br>");
}
con.close();
%>
</body>
</html>
---------------------------------------------------------Client And Server Siden Programs.
---------------------------------------------------------Insert a recorded
---------------------------------------------------------Input.html
<html>
<body>
<form action="http://localhost:7001/data2.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>
</body>
</html>
data2.jsp
<%@ page import ="java.sql.*;" %>
<html>
<body>

<%
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>
--------------------------------------------------------------

Anda mungkin juga menyukai