0% found this document useful (0 votes)
0 views39 pages

Module 04 JavaServerPages

Java Server Pages (JSP) is a server-side technology that allows for the creation of dynamic web content using HTML and Java code. JSP pages are converted to Java servlets, which are executed on a web server, with Tomcat being a popular choice for running JSP applications. The document also covers JSP tags, methods, control statements, loops, session tracking, cookies, and how to parse request strings for user input.

Uploaded by

sagarashrivibha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views39 pages

Module 04 JavaServerPages

Java Server Pages (JSP) is a server-side technology that allows for the creation of dynamic web content using HTML and Java code. JSP pages are converted to Java servlets, which are executed on a web server, with Tomcat being a popular choice for running JSP applications. The document also covers JSP tags, methods, control statements, loops, session tracking, cookies, and how to parse request strings for user input.

Uploaded by

sagarashrivibha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 39

Java ServerPages

JSP
• Java ServerPages is a server-side program.
• A JSP is called by a client to provide a web service.
• A JSP processes the request by using logic built into the JSP or by
calling other web components built using Java servlet technology
or
Enterprise Java Bean technology or other technology.
• A JSP is simple to create, a JSP is written in HTML rather than
with the Java programming language.
• A JSP offers basically the same features found in Java servlet
because a JSP is converted to a Java servlet the first time that a
client requests the JSP.
• JSP pages are translated into servlets, the servlets are compiled, and
at request time it is the compiled servlets that execute. So, 2

writing JSP pages is really just another way of writing servlets.


• jspInt(), jspDestroy() and service() are the three methods that are
automatically called when a JSP is requested and terminates
normally.
• The jspInt() is called first when the JSP is requested and is used to
initialize objects and variables that are used throughout the life of
JSP.
• The jspDestroy() is automatically called when the JSP terminates
normally. When JSP abruptly terminated, it is not called.
• The service() is automatically called and retrieves a connection to
HTTP.

3
JSP Tags
• A JSP programs consists of a combination of HTML tags and JSP
tags.
• JSP tags define Java code that is to be executed before the output
of the JSP program is sent to the browser.
• Java codes associated with JSP tags in the JSP program is
executed when encountered by Tomcat, and the result of that
process is sent to the browser.
• The browser knows how to display the result because the JSP tag
is enclosed within an open and closed HTML tag.
• There are five types of JSP tags.

4
• Comment tag :
• A comment tag opens with <%-- and closes with --%>.
• Declaration Statement tags : It opens with <%! and is followed
by a Java declaration statement(s) that define variables, objects,
and
methods that are available to other components of the JSP
program.
• Directive tags : A directive tag opens with <%@ and commands
the JSP virtual engine to perform a specific task, such as
importing
a Java package required by objects and methods.
• The directive tag closes with %>. There are three commonly
used
directives. They are :
• import : It is used to import Java packages into the JSP program. 5
<%@ page import = “ import java.sql.* ” ; %>
• taglib : taglib tag specifies a file that contains a tag library.
<%@ taglib url=“myTags.tld” %>
• Expression tags : It opens with <%= and is used for an
expression
statement. It closes with %>.
• Scriptlet tags : A scriptlet tag opens with <% and contains
commonly used Java control statements and loops. Closes with
%>.

6
Variables & Objects

<HTML>
<HEAD>
<TITLE>Order Confirmation</TITLE>
</HEAD>
<BODY>
<%! int age=25; %>
<p> Your age is : <%=age%> </p>
</BODY>
</HTML>

7
<HTML>
<HEAD>
<TITLE> JSP Programming </TITLE>
</HEAD>
<BODY>
<%! int age=25;
float salary;
int empnumber;
%>
</BODY>
</HTML>

8
<HTML>
<HEAD>
<TITLE>JSP Programming </TITLE>
</HEAD>
<BODY>
<%! String Name;
String [] Telephone = {"201-555-1212", "201-555-4433"};
String Company = new String();
int[] Grade = {100,82,93};
%>
</BODY>
</HTML>

9
Methods
• JSP offers same functionality for defining methods as done by
Java.
• In JSP a method definition is placed within a JSP tag.
• We can call the method from within the JSP tag once the method is
defined.
• The JSP tag that calls the method must be a JSP expression tag,
which begins with <%=.

10
<HTML>
<HEAD>
<TITLE> JSP Programming </TITLE>
</HEAD>
<BODY>
<%! int curve (int grade)
{
return 10 + grade;
}
%>
<p> Your curved grade is : <%=curve(80)%> </p>
</BODY>
</HTML>

11
<HTML>
<HEAD>
<TITLE> JSP Programming </TITLE>
</HEAD>
<BODY>
<%! int curve (int grade)
{
return 10 + grade;
}
int curve(int grade, int curvevalue)
{
return curvevalue+grade;
}
%>
<p> Your curved grade is : <%=curve(80,10)%> </p>
<p> Your curved grade is : <%=curve(70)%> </p>
</BODY> 12

</HTML>
Control Statement
• Using JSP it is easy to create dynamic content for a web page
based on conditions received from the browser.
• There are two control statements used to change the flow of a JSP
program. These are the if statement and the switch statement,
both
of which are also used to direct the flow of a Java program.
• The power of these codes comes from the fact that the code
segment that is executed or skipped can consists of HTML tags or
a
combination of HTML tags and JSP tags.

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

14
<% switch (grade) {
case 90 : %>
<p> Your final grade is a A </p>
<% break;
case 80 : %>
<p> Your final grade is a B </p>
<% break;
case 70 : %>
<p> Your final grade is a C </p>
<% break;
case 60 : %>
<p> Your final grade is a D </p>
<% break;
}
%>
</BODY>
</HTML> 15
Loops
• JSP loops are nearly identical to loops used in Java programs.
• The for loop, while loop, and do.. While loop are the three loops.
• Loops play an important role in JSP database programs.

16
<html>
<head>
<title> JSP programming</title>
</head>
<body>
<center>
<%! 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> 17
</table>
<table>
<tr>
<td> First </td>
<td> Second </td>
<td> Third </td>
</tr>
<tr>
<% while(x<3) { %>
<td><%=Grade[x]%></td>
<% x++;
} %>
</tr>
</table>

18
<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>
</center>
</body>
</html> 19
Tomcat
• JSP programs are executed by JSP Virtual Machine that runs on a
web server.
• Hence there is a need to access to a JSP Virtual Machine to run the
JSP programs.
• One of the most popular JSP Virtual Machines is Tomcat, it is
downloadable at free of cost from www.jakarta.apache.org
• Installing Java is also required.

20
Request String
• The browser generates a user request string whenever the Submit
button is selected. The user request string consists of the URL
and
the query string.
• http://www.jimkeogh.com/jsp/myprogram.jsp?fname=“bob”&lname=“smith”
• Your program needs to parse the query string to extract values of
fields that are to be processed by your program. You can parse
the
query string by using methods of the JSP request object.
• The getParameter(Name) is the method used to parse a value of a
specific field. The getParameter() method requires an argument,
which is the name of the field whose value you want to retrieve.

<%! String Firstname = request.getParameter(fname);21


String Firstname = request.getParameter(lname); %>
• There are four predefined implicit objects that are in every JSP
programs. These are request, response, session and out.
• The request object is an instance of the HTTPServletRequest.
• The response object is an instance of the HTTPServletResponse.
• The session object is an instance of the HTTPSession.
• The out object is an instance of the JSPWriter, used to send a
response to the client.
• getParameterValues() method helps us to copy a value from a
multivalued field such as a selection list field.
<%! String[] EMAIL = request.getParameterValues(“ADDR”); %>
<P> <%=EMAIL [O]%> </P>
<P> <%=EMAIL [1]%> </P>
• Field names in the request string can be parsed by using the
getParameterNames() method. 22
<HTML>
<HEAD>
<TITLE>Order Confirmation</TITLE>
</HEAD>
<BODY>
<H2>Order Confirmation</H2>
Thanks for ordering <I><%= request.getParameter("title") %></I>!
</BODY>
</HTML>

23
email.html
<html>
<head>
<title> Email List application</title>
</head>
<body>
<h1>Join our email list</h1>
<p>To join our email list, enter your name and
email address below. <br>
Then, click on the Submit button.</p>
<form action="http://localhost:2013/FirstApp/email.jsp"
method="get">
<table cellspacing="5" border="0">
<tr>
<td align="right">First name:</td>
<td><input type="text" name="firstName"></td>
</tr> 24
email.jsp

<tr>
<td align="right">Last name:</td>
<td><input type="text" name="lastName"></td>
</tr>
<tr>
<td align="right">Email address:</td>
<td><input type="text" name="emailAddress"></td>
</tr>
<tr>
<td></td>
<td><br><input type="submit" value="Submit"></td>
</tr>
</table>
</form>
</body> 25

</html>
<html>
<head>
<title>Chapter 4 - Email List application</title>
</head>
<body>
<%
String firstName = request.getParameter("firstName");
String lastName = request.getParameter("lastName");
String emailAddress = request.getParameter("emailAddress");
%>
<h1>Thanks for joining our email list</h1>
<p>Here is the information that you entered:</p>
<table cellspacing="5" cellpadding="5" border="1">
<tr>
<td align="right">First name:</td>
<td><%= firstName %></td>
</tr> 26
<tr>
<td align="right">Last name:</td>
<td><%= lastName %></td>
</tr>
<tr>
<td align="right">Email address:</td>
<td><%= emailAddress %></td>
</tr>
</table>
<p>To enter another email address, click on the Back <br>
button in your browser or the Return button shown <br>
below.</p>
<form action=“email.html” method="post">
<input type="submit" value="Return">
</form>
</body>
</html> 27
Parsing Other Information
• The request string sent to the JSP by the browser is divided into
two general components separated by question mark.
• The URL component appears to the left of the question mark and
the query string is to the right of the question mark.
• The URL is divided into four parts, beginning with the protocol.
• The first three parts commonly used protocols like HTTP,
HTTPS
(secured version of HTTP) and FTP.
• Next is host and port combination. The host and port is the
virtual
path of the JSP programs. The server maps the virtual path to
the
physical path.
28
• User Sessions
• A JSP program must be able to track a session. There are three
commonly used methods to track a session.
• These are by using a hidden field, by a cookier or by Java Bean.
• A hidden field is a field in an HTML form whose value isn’t
displayed on the HTML page.

29
Cookies
• Cookie is a small piece of information created by a JSP program
that
is stored on the client’s hard disk by the browser.
• Cookies are used to store various kinds of information like user
preferences and an ID that tracks a session with a JSP database
system.

30
<HTML>
<HEAD>
<TITLE>JSP Programming</TITLE>
</HEAD>
<BODY>
<%! String MyCookieName = "userID";
String MyCookieValue = "HK1917"; %>
<% response.addCookie(new Cookie(MyCookieName,
MyCookieValue)); %>
<p> MyCookieName is : <%=MyCookieName%> </p>
<p> MyCookieValue is : <%=MyCookieValue%> </p>
</BODY>
</HTML>

31
<HTML>
<HEAD>
<TITLE>JSP Programming</TITLE>
</HEAD>
<BODY>
<%! String CName, CValue;
int found=0; %>
<% Cookie[] cookies = request.getCookies();
for(int i=0; i<cookies.length; i++)
{
CName = cookies[i].getName();
CValue = cookies[i].getValue();
} %>
<p> Cookie name = <%=CName%> </p>
<p> Cookie value = <%=CValue%> </p>
</BODY>
</HTML> 32
Session Objects
• Each time a session is created, a unique ID is assigned to the
session and stored as a cookie.
• The unique ID enables JSP programs to track multiple sessions
simultaneously while maintaining data integrity of each session.
• The session ID is used to prevent intermingling of information
from
clients.
• Along with session ID, a session object is also used to store other
types of information, called attributes.
• An attribute can be login information, preferences, or even
purchases placed in an electronic shopping cart.

33
<HTML>
<HEAD>
<TITLE>JSP Programming</TITLE>
</HEAD>
<BODY>
<%! String AtName = "Product";
String AtValue = "1983"; %>
<% session.setAttribute(AtName, AtValue); %>
</BODY>
</HTML>

34
<HTML>
<HEAD>
<TITLE>JSP Programming</TITLE>
</HEAD>
<BODY>
<% Enumeration e = session.getAttributeNames();
while(e.hasMoreElements())
{
String AtName = (String)e.nextElement();
String AtValue =(String)session.getAttribute(AtName);
%>
<p> Attribute Name is : <%=AtName%> </p>
<p> Attribute Value is : <%=AtValue%> </p>
<% } %>
</BODY>
</HTML>

35
JSP to print 10 even and 10 odd numbers.
<html>
<head>
<title> jsp </title>
</head>
<body>
<%!int i=0;%>
<p> even nos </p>
<table border=2>
<tr>
<%for(i=0;i<20;i++){
if(i%2==0)
{ %>
<td> <%=i%> </td>
<%}%>
<%}%>
</tr> 36
</table>
<table border=1>
<p> odd nos </p>
<tr>
<%for(i=1;i<20;i++){
if(i%2!=0)
{ %>
<td> <%=i%> </td>
<%}%>
<%}%>
</tr>
</table>
</body>
</html>

37
JSP for checking the valid username and password.
<html>
<head>
<title> validation </title>
</head>
<body>
<% String username=request.getParameter("uname");
String pssword=request.getParameter("pswd");
if(username.equals("jnnce") && pssword.equals(“ise"))
{%>
<p> welcome <%=username%> </p>
<%}
else{%>
<p> invalid usr name </p>
<%}%>
</body>
38
</html>
HTML file for username and password

<html>
<head>
<title> validation </title>
</head>
<body>
<h2> LOGIN </h2>
<p> enter name n passwd </p>
<form method="GET"
action="http://localhost:8080/jnnce/welcome.jsp">
<p> usrname
<input type="text" name="uname" size="20">
</p>
<p> pswd
<input type="password" name="pswd" size="20"> </p>
<input type="submit" name="submit">
<input type="reset" name="reset">
</form>
</body>
39
</html>

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy