You are on page 1of 26

JSP & RMI

THE COMPLETE REFERENCE TO J2EE


JIM KEOGH
CHAPTERS 11 &15
 Java Server Pages are HTML pages embedded
with snippets of Java code.
 It is an inverse of a Java Servlet
 Servlets are pure Java programs. They introduce
dynamism into web pages by using programmatic
content.
 JSP technology is an extension/wrapper over the
Java servlet technology.
 JSP are text based documents.
Typical Architecture of JSP(extra info)
JSP Processing

 As with a normal page, your browser sends an HTTP request to the web server.
 The web server recognizes that the HTTP request is for a JSP page and forwards it to
a JSP engine. This is done by using the URL or JSP page which ends
with .jsp instead of .html.
 The JSP engine loads the JSP page from disk and converts it into a servlet content.
This conversion is very simple in which all template text is converted to println( )
statements and all JSP elements are converted to Java code. This code implements
the corresponding dynamic behavior of the page.
 The JSP engine compiles the servlet into an executable class and forwards the
original request to a servlet engine.
 A part of the web server called the servlet engine loads the Servlet class and executes
it. During execution, the servlet produces an output in HTML format. The output is
furthur passed on to the web server by the servlet engine inside an HTTP response.
 The web server forwards the HTTP response to your browser in terms of static
HTML content.
 Finally, the web browser handles the dynamically-generated HTML page inside the
HTTP response exactly as if it were a static page.
Life cycle of a JSP
JSP Tags

 Comment tag
 Declaration tag
 Directive tag
 Expression tag
 Scriptlet tag

 All tags begin with <% and end with %>


JSP Comments

 JSP comment marks text or statements that the JSP container should
ignore. A JSP comment is useful when you want to hide or "comment out",
a part of your JSP page.
 Following is the syntax of the JSP comments −
 <%-- This is JSP comment --%>
 Example
<html>
<head>
<title>A Comment Test</title>
</head>
<body>
<h2>A Test of Comments</h2>
<%-- This comment will not be visible in the page source --%> </body>
</html>
JSP Declarations

 A declaration declares one or more variables or


methods that you can use in Java code later in the
JSP file. You must declare the variable or method
before you use it in the JSP file.
 Following is the syntax for JSP Declarations −
 <%! declaration; [ declaration; ]+ ... %>
 Example
 <%! int i = 0; %>

 <%! int a, b, c; %>

 <%! Circle a = new Circle(2.0); %>


JSP Directives

 A JSP directive affects the overall structure of the servlet


class.
 It usually has the following form −
 <%@ directive attribute="value" %>
 There are three types of directive tag −
 <%@ import ... %> - used to import Java packages into JSP
programs.
 <%@ include ... %>-Includes a file during the translation phase.
 <%@ taglib ... %>-Declares a tag library, containing custom
actions, used in the page
 Example
 <%@page import=“import java.sql.*”;%>
 <%@include file=“keogh\books.html”%>
 <%taglib uri=“mytags.tld”%>
JSP Expression

 A JSP expression element contains a scripting language


expression that is evaluated, converted to a String, and
inserted where the expression appears in the JSP file.
 Because the value of an expression is converted to a
String, you can use an expression within a line of text,
whether or not it is tagged with HTML, in a JSP file.
 The expression element can contain any expression that
is valid according to the Java Language Specification but
you cannot use a semicolon to end an expression.
 Following is the syntax of JSP Expression −
 <%= expression %>
The Scriptlet

 A scriptlet can contain any number of JAVA


language statements, variable or method
declarations, or expressions that are valid in the page
scripting language.
 Following is the syntax of Scriptlet −
 <% code fragment %>
Variables and Objects

 Program to declare and using a variable


<HTML>
<HEAD>
<TITLE> JSP PROGRAM</TITLE>
</HEAD>
<BODY>
<%! int age=29; %>
<P your age: <%=age%> </P>
</BODY>
</HTML>
 Program to declare multiple variables within a single JSP tag

<HTML>
<HEAD>
<TITLE> JSP PROGRAM</TITLE>
</HEAD>
<BODY>
<%! int age=29;
float sal;
int enum
%>
</BODY>
</HTML>
 Along with variables you can declare objects, arrays and Java collection
classes within a JSP tag.
 Program to declare objects and arrays within a single JSP tag
<HTML>
<HEAD>
<TITLE> JSP PROGRAM</TITLE>
</HEAD>
<BODY>
<%! String name;
String[] phone={“080-2442233”, “080-2123445”};
String Company=new String();
Vector Assignments = new Vector();
int[] Grade = {100,92};
%>
</BODY>
</HTML
Methods

 Defining and calling a method


<HTML>
<HEAD>
<TITLE> JSP Programming</TITLE>
</HEAD>
<BODY>
<%! boolean curve(int grade)
{
return 10 + grade;
}
%>
<P>your curved grade is <%=curve(80)%></P>
</BODY>
</HTML>
 Defining and calling an overloaded method
<HTML>
<HEAD>
<TITLE> JSP Programming</TITLE>
</HEAD>
<BODY>
<%! boolean curve(int grade)
{
return 10 + grade;
}
%>
<%! boolean curve(int grade, int curvevalue)
{
return curvevalue + grade;
}
%>

<P>your curved grade is <%=curve(80)%></P>


<P>your curved grade is <%=curve(70)%></P>

</BODY>
</HTML>
Control Statements

 If – evaluates a condition statement to determine if


one or more lines of code are to be executed or
skipped.
 Switch – compares a value with one or more other
values associated with the case statement. The code
segment that is associated with the matching case
statement is executed. Code segments associated
with other segments are ignored.
 Program to calculate the grade and display the grade.
<HTML>
<HEAD>
<TITLE> JSP Programming</TITLE>
</HEAD>
<BODY>
<%! Int grade =70; %>
<%if (grade > 69) {%>
<P> You Passed… </P>
<% } else { %>
<P>Better luck next time.</P>
<% } %>
<% switch(grade) {
case ’90’: %>
<P> Your final grade is A </P>
< % break;
case ’80’: %>
<P> Your final grade is B </P>
< % break;

case ’70’: %>


<P> Your final grade is C </P>
< % break;

case ’60’: %>


<P> Your final grade is F </P>
< % break;
} %>

</BODY>
</HTML>
Loops
<HTML>
<HEAD>
<TITLE> JSP Programming</TITLE>
</HEAD>
<BODY>
<%! int [] grade = {100,82,93};
int x=0;
%>
<table>
<tr>
<td>First</td>
<td>Second</td>
<td>Third</td>
</tr>
<tr>
<%for (int i=0;i<3;i++) { %>
<td><%=grade[i]%> </td>
<% } %>
</tr>
</table>
Loops

 for – repeats a specified number of times.


 while – executed continually as long as a specified
condition remains true.
 do while – executes atleast once even if the condition
specified is false.
<table>
<tr>
<td>First</td>
<td>Second</td>
<td>Third</td>
</tr>
<tr>
<%while (x<3) { %>
<td><%=grade[x]%> </td>
<% x++;
} %>
</tr>
</table>
<table>
<tr>
<td>First</td>
<td>Second</td>
<td>Third</td>
</tr>
<tr>
<% x = 0;
do { %>
<td><%=grade[x]%> </td>
<% x++;
} while (x<3) %>
</tr>
</table>
</body>
</html>
RMI

 Interface declaration
Server side program
Client Side Program

You might also like