Unit 3 Java Server Pages (JSP) AWT
Unit 3 Java Server Pages (JSP) AWT
INTRODUCTION
Java Server Pages (JSP) technology enables you to mix regular, static HTML with dynamically
generated content. You simply write the regular HTML in the normal manner, using familiar
Web-page-building tools. You then enclose the code for the dynamic parts in secial tags, most of
which start with <% and end with %>
Ex: Following code uses a request parameter to display the title of a book. Notice that the listing
is mostly standard HTML, the dynamic code consists entirely of the half line as shown below;
Code: OrderConfirmation.jsp
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN”>
<HTML>
<HEAD>
<TITLE>
Order Confirmation
</TITLE>
<LINK REL=STYLESHEET HREF=”JSP-Styles.css” TYPE=”text/css”>
</HEAD>
<BODY>
<H2> Order Cofirmation </H2>
Thanks for ordering
<I> <%=request.getParameter(“title”)%> </I>!
</BODY>
</HTML>
You can think of Servlets as Java code with HTML inside; and of JSP as HTML with Java code
inside. Neither Servlets nor JSP pages are restricted to using HTML, but they usually do and this
over-simplified description is a common way to view the technologies.
Despite the large apparent differences between JSP pages and Servlets, behind the scenes they
are the same thing. JSP pages are translated into servlets, the servlets are compiled, and at
request time it is the compiled servlets that execute. So, writing JSP pages is really just another
way of writing Servlets.
Even though servlets and JSP pages are equivalent behind the scenes, they are not equally useful
in all situations. Separating the static HTML from the dynamic content provides a number of
benefits over servlets alone, and the approach used in Java Server Pages offers several
advantages over competing technologies.
UNDERSTANDING THE NEED FOR JSP
Servlets are convenient to write and efficient to execute. They make it simple to read request
parameters and to set up custom code to handle missing and malformed data. They can easily
make use of HTTP request headers and can flexibly manipulate HTTP response data. They can
customize their behavior based on cookies, track user-specific data with the session tracking API
and talk to relational databases with JDBC.
Servlets are indeed useful and JSP by no means makes them obsolete. Servlets excel at all tasks
related to programming or data processing. But servlets are not so good at presentation. Servlets
have the following deficiencies;
1. It is hard to write and maintain the HTML
2. You cannot use standard HTML tools
3. The HTML is inaccessible to non-Java developers
JSP pages are translated into servlets. So, fundamentally, any task JSP pages can perform could
also be accomplished by servlets. However, this underlying equivalence does not mean that
servlets and JSP pages are equally appropriate in all scenarios. The issue is not the power of the
technology, it is the convenience, productivity, and maintainability of one or the other. After all,
anything you can do on a particular computer platform in the Java programming language you
could also do in assembly language. But it still matters which you choose.
JSP provides the following benefits over servlets alone.
1. It is easier to write and maintain the HTML
2. You can use standard website development tools
3. You can divide up your development team.
<html>
<body>
<%= "welcome to jsp" %>
</body>
</html>
Note: Do not end your statement with semicolon in case of expression tag.
Example of JSP expression tag that prints current time
To display the current time, we have used the getTime() method of Calendar class. The
getTime() is an instance method of Calendar class, so we have called it after getting the instance
of Calendar class by the getInstance() method.
index.jsp
<html>
<body>
Current Time: <%= java.util.Calendar.getInstance().getTime() %>
</body>
</html>
Example of JSP expression tag that prints the user name
In this example, we are printing the username using the expression tag. The index.html file gets
the username and sends the request to the welcome.jsp file, which displays the username.
File: index.jsp
<html>
<body>
<form action="welcome.jsp">
<input type="text" name="uname"><br/>
<input type="submit" value="go">
</form>
</body>
</html>
File: welcome.jsp
<html>
<body>
<%= "Welcome "+request.getParameter("uname") %>
</body>
</html>
WRITING SCRIPLETS
A scriptlet tag is used to execute java source code in JSP. Syntax is as follows:
<% java source code %>
Example of JSP scriptlet tag
In this example, we are displaying a welcome message.
<html>
<body>
<% out.print("welcome to jsp"); %>
</body>
</html>
<html>
<body>
<form action="welcome.jsp">
<input type="text" name="uname">
<input type="submit" value="go"><br/>
</form>
</body>
</html>
File: welcome.jsp
<html>
<body>
<%
String name=request.getParameter("uname");
out.print("welcome "+name);
%>
</form>
</body>
</html>
SCRIPLET EXAMPLES
A JSP page that uses the bgColor request Parameter to set the background color of the page;
<! DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN”>
<HTML>
<HEAD>
<TITLE> COLOR TESTING </TITLE>
</HEAD>
<%
String bgColor=request.getParameter(“bgColor”);
if ((bgColor==null) || (bgColor.trim().equals(“”)))
{
bgColor=”WHITE”;
}
%>
<BODY BGCOLOR=”<%=bgColor %>”>
<H2 ALIGN=”CENTER”> TESTING A BACKGROUND OF “<%=bgColor %>”
</H2>
</BODY>
</HTML>
<html>
<body>
<%! int data=50; %>
<%= "Value of the variable is:"+data %>
</body>
</html>
<html>
<body>
<%!
int cube(int n){
return n*n*n*;
}
%>
<%= "Cube of 3 is:"+cube(3) %>
</body>
</html>
includes the original content in the generated servlet. calls the include method.
<html>
<body>
</body>
</html>
Note: The include directive includes the original content, so the actual page size grows at
runtime.
The jsp:useBean, jsp:setProperty and jsp:getProperty tags are used for bean development. So we
will see these tags in bean developement.
<html>
<body>
<h2>this is index page</h2>
<html>
<body>
<% out.print("Today is:"+java.util.Calendar.getInstance().getTime()); %>
</body>
</html>
<html>
<body>
<h2>this is index page</h2>
</body>
</html>
printdate.jsp
<html>
<body>