Vous êtes sur la page 1sur 30

Message Flows in a Two-tier

Client/Server Network

1
Introduction:-jsp
A JSP page is a page created by the web
developer that includes JSP
technology-specific and custom tags,
in combination with other static
(HTML or XML) tags.

A JSP page has the extension .jsp


or .jspx;
Continue..
The Web server asks the JSP engine to check whether the
JSP page has never been accessed before. If this is the
case the JSP engine will

Parse the JSP document to translate into a


Servlet Java file

Compile the Servlet Java file into a class file

Then the Servlet container loads the Servlet class for


execution and sends the results back to the client.
JSP life cycle:-

(when JSP get


first access or get
changed) compile
translate Servlet
JSP file
Java file Servlet
Class

JSP engine

Servlet Container
Process of a JSP page
JSP Tags:-
Directive: ex: import classes
<%@ page import = java.util.* %>

Declaration: variable declaration


<%! int age = 56; %>
<%! int i = 0; %>
<%! int a, b; double c; %>
<%! Circle a = new Circle(2.0); %>
Continue.
Scriplet: Java code
<% if password.equals(xyz) { %>
<H1> Welcome <\H1> <%}%>

Output:
<%= age %>
<%= new java.util.Date() %>

Comments (delimited by <%-- and --%>)


<HTML>
<HEAD><TITLE>Welcome to JSP</TITLE></HEAD>
<BODY>

<% java.util.Calendar cal = java.util.Calendar.getInstance();


int hour = cal.get(cal.HOUR_OF_DAY);
if ( hour < 12)
{%> HTML green
<br>Hour is </br> <%= hour %> JSP red / blue
<h3>Good morning!</h3>
<%}else {%>
<h3>Good afternoon!</h3>
<%}%>
</BODY>
</HTML>
JSP starts/stops with <% %> Html can occur anywhere else
JavaScript objects and events
<HTML>
<HEAD><TITLE>Calculating the Fibonacci no.</TITLE></HEAD>
<BODY BGCOLOR = navy TEXT=yellow>

<%! int[] fib; %>


<CENTER>
<H1> Calculating the Fibonacci no. of 0-19 </H1> <BR>
<% fib=new int[20];
fib[0]=0;
fib[1]=1;
for(int i=2;i<20;i++)
{
fib[i]=fib[i-1]+fib[i-2];%>
<B>No=</B> <% =fib[i] %> <br>
<%}%>
</CENTER>
</BODY></HTML>
Orderform.jsp: A JSP code that has a table
containing item price, quantity, total price
<HTML>
<HEAD><TITLE>A catalog order form</TITLE></HEAD>
<BODY>
<H1 ALIGN=center> An order form</H1>
<%! string item[]={toaster, CD, diskette};
double price[]={19.99,12.99,1.99};
int quantity[]={2,9,20};
%>
<TABLE ALIGN=center BGCOLOR=Yellow>
<TR>
<TH>ITEM</TH>
<TH>Price</TH>
<TH>Quantity</TH>
<TH>TotalPrice</TH>
</TR>
CONTINUE..

<%for(int i=0;i<3;i++){%>
<TR>
<TD> <%=item[i]%> </TD>
<TD> <%=price[i]%> </TD>
<TD> <%=quantity[i]%> </TD>
<TD> <%=price[i]*quantity[i]%> </TD>
</TR>
<%}%>
</TABLE>
</BODY>
</HTML>
3-Tier Architectures
Definition: A 3-tier architecture is one which has
a client tier, a middle tier, and a database tier.
The database tier manages the database
The middle tier contains most of the logic and
communicates between the other tiers
The client tier is the interface between the user and
the system

Definition: An n-tier architecture is one which


has n tiers, usually including a database tier, a
client tier, and n-2 tiers in between.
Message Flows in a Three-tier
Client/Server Network

Message Flows in a Three-tier


Client/Server Network

E-Commerce: The Second Wave, Fifth Annual Edition 13


N-tier J2EE Architecture

Web Tier EJB Tier


Including Request Model: A JSP code that manages
a request and includes the output of other JSP
<HTML>
<HEAD> <TITLE> example of the include action <TITLE></HEAD>
<BODY>
Include the first file:
<jsp:include page=powersof2.jsp/><BR>
Include the second file:
<jsp:include page=orderform.jsp/>
</BODY>
</HTML>
Including Request Model: A JSP that manages a
request and includes the output of other JSP.

<HTML>
<HEAD><TITLE>Example of forward action</TITLE></HEAD>
<BODY>
<%if(math.random()>5){%>
<jsp: forward page = fibonaccicomputation.jsp/>
<%else{%>
<jsp: forward page = factorialcomputation.jsp/>
<%}%>
</BODY>
</HTML>
<html>
<head></head>
<body>
<p>Enter two numbers and click the
calculate button.</p>

<form action=calculator.jsp method=get>


<input type=text name=value1><br>
<input type=text name=value2 ><br>
<input type=submit name=calculate value=calculate>
</form>
</body>
</html>
Calculator.html
<html>
<head><title>A simple calculator: results</title></head>
<body>
<%-- A simpler example 1+1=2 --%>
1+1 = <%= 1+1 %> Calculator.jsp
<%-- A simple calculator --%>
<h2>The sum of your two numbers is:</h2>
<%= Integer.parseInt(request.getParameter("value1")) +
Integer.parseInt(request.getParameter("value2")) %>
</body>
</html>
JSP that verifies login password(login.jsp)
<% String password=request.getParameter(password);
if(verifypolicy(password)){%>
<H1> THANK YOU</H1>
<% } else{ %>
<H1> SORRY</H1>
<%}%>
</BODY> </HTML>

Login.html
<HTML> <BODY>
<FORM ACTION=login.jsp METHOD=POST>
<P>USER NAME:<INPUT TYPE=text NAME=username>
<P>USERNAME:<INPUT TYPE=text NAME=password>
<P><INPUT TYPE=submit VALUE=Submit>
</FORM>
</BODY> </HTML>
JSP that verifies login password(login.jsp)
<HTML> <BODY>
<% ! final static int MIN_PASW_LEN=8;
boolean verifylength(string password)
{ if(password.length() < MIN.PASW_LEN)
return false;
return true;
}
boolean verifydigit(string password)
{ for(int i=0;i<password.length();i++)
{
if(Character.isDigit(password.charAt(i)))
return true;
}
return false;
}
boolean verifypolicy(String password)
{ if(verifylength(password) && verifydigit(password))
return true;
return false;
jsp:plugin Action Element
The jsp:plugin action can insert an Java Applet
client-side component into a server-side JSP page
component.
The syntax is as follows.

<jsp:plugin type=applet code=MyApplet.class


width=400 height=200>
...
<!-- Parameter lists passed on the current JSP -->
<jsp:param name=username value=Smith />

</jsp:plugin>
jsp:plugin Action Element (cont.)
The <jsp:plugin> element can
display an Applet object or a
bean object in the client Web
browser, using a Java plug-in
which is part of the browser or
downloaded from a specified
URL.
When the JSP file sends an HTML response to the client, the <jsp:plugin> element is replaced by an <object> element in HTML specification. In general, the attributes to the <jsp:plugin> element specify whether the object
is a bean or an applet, locate the code that will be run, position the object in the browser window, specify an URL from which to download the plug-in software, and pass parameter names and values to the object.
Example of switch statement
<html> <body>
<%! final int logged_off=0, logged_on=1;
int usertype=logged_on;%>
<% switch(usertype)
{
case logged_on: %> <h1>welcome!</h1>
<%break;

case logged_off:%> <h2> sorry!</h2>


<%break;
default: %> <h1> error!</h1>
%}%>
</body> </html>
A JSP that pulls book information from a database
<HTML> <HEAD><TITLE>Example Of Simple Model</TITLE></HEAD><BODY> <CENTER>
<FONT COLOR=BLUE SIZE=6 > A Listing Of Books</FONT></CENTER> <BR><BR>
<% @ page import=java.sql.*%>
<TABLE WIDTH=100% BORDER=2 BGCOLOR=SILVER>
<TR>
<TH WIDTH=50%>TITLE</TH>
<TH WIDTH=25%>AUTHOR</TH>
<TH WIDTH=25%>ISBN</TH>
</TR>
<% try
{ Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection ("jdbc:oracle:thin:@localhost:1521:xe",
"system","aashiq2");
Statement stmt=conn.createStatement();
String sql=select* from bookdata;
ResultSet rs=stmt.executeQuery(sql);
While(rs.next())
{%>
<TR>
<TD><% =rs.getString(title) % > </TD>
<TD><% =rs.getString(author) %> </TD>
<TD><% =rs.getString(isbn) %> </TD>
</TR>
< % } %>
</TABLE>
<% if(stmt!=null)
stmt.close();
if(conn !=null)
Conn.close();
}
catch(Exception e)
{ out.print(e);
} %>
</BODY> </HTML>
Processing request from the user
(an html form and JSP that process credit card information)

<HTML> <BODY>
<FORM ACTION=creditform.jsp METHOD=post>
Name: <INPUT TYPE=text NAME=name SIZE=10>
Credit cad no:<INPUT TYPE=text NAME=member SIZE=25>
Card type: <SELECT NAME=type>
<OPTION>Visa</OPTION>
<OPTION>Master Card</OPTION>
<OPTION SELECTED> Amex</OPTION>
</SELECT>
<INPUT TYPE =submit NAME=submit VALUE=submit>
</FORM>
</BODY> </HTML>
Processing request from the user
(a JSP code that process from that gathers credit card information creditform.jsp)

<html> <body>
<ul>
<li>name=<% =request.getparameter(name) %>
<li>name=<% =request.getparemeter(member) %>
<li>name=<% =request.getparameter(type) %>
</ul>
</body> </html>
JAVA Bean
package xyz;
public class callingjavabeanmethods
{ private String str; private int a ; private double d;
public callingjavabeanmethods(){}

public void setString(String s){str=s;}


public void setInteger(int i){a=i;}
public void setDouble(double d1){d=d1;}

public String getString(){return str;}


public int getInteger(){return a;}
public double getDouble(){return d;}
}
<H1><using methods</H1>
<% xyz.callingjavaBeanmethods cm2=new xyz.callingjavabeanmethosd();
cm2.setString(string value2);
cm2.setInteger(234);
cm2.setDouble(567.678);
%>
<UL>
<LI><%=cm2.getString() %>
<LI><%=cm2.getInteger() %>
<LI><%=cm2.getDouble() %>
</UL>
</BODY> </HTML>
<HTML> <BODY>
<jsp:useBean class=xyz.callingjavabeanmethods.java id=cm1>
<jsp:setProperty name=cm1 property=str value =value1/>
<jsp:setProperty name=cm1 property =a value =123/>
<jsp:setProperty name=cm1 property=d value=345.45/>
</jsp: useBean>
<UL>
<LI><jsp:getProperty name=cm1 property=str/>
<LI><jsp:getProperty name=cm1 property=a/>
<LI><jsp:getProperty name=cm1 property=d/>
</UL>

Vous aimerez peut-être aussi