Java Server Pages (JSP)

thickbugSoftware and s/w Development

Oct 28, 2013 (3 years and 8 months ago)

81 views

Java Server Pages (JSP)

A Java Servlets Example


A simple "HelloWorld" servlet, that also prints the current date.

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

public class HelloWorld extends HttpServlet {


public void doGet(HttpServletRequest req, HttpServletResponse res)

throws
ServletException, IOException {


res.setContentType("text/html");


PrintWriter out = res.getWriter();


out.println("<HTML>");


out.println("<HEAD><TITLE>Hello World</TITLE></HEAD>");


out.println("<BODY>");


out.println("<H1>Hello World</H1>");


out.println("Today is: " + (new java.util.Date().toString()) );


out.println("</BODY></HTML>");


} // doGet

} // HelloWorld


In order to run it, do the following:


Place it in a file, HelloWorld.java


Compile it.


Place the resulting HelloWorld.class file in the "servlets" directory.


Run it by pointing browser to: http://server.address/servlets/HelloWorld



A JSP Example


The previous page can be written using JSP as shown below:

<HTML>


<HEAD>


<TITLE>Hello World</TITLE>


</HEAD>


<BODY>


<H1>Hello World</H1>


Today is: <%= new java.util.Date().toString() %>


</BODY>

</HTML>


In order to run it, do the following:


Place it in a file, HelloWorld.jsp in the same directory as your
.html files


Run it by pointing browser to:
http://server.address/~youraccount/HelloWorld.jsp




Elements of JSP


Whenever a .jsp is requested for the first time, the server does
the following:


1. Translates the .jsp page into a servlet

2. Compiles the servlet into a class file

3. Executes the servlet (response is sent to the client)


Subsequent requests (as long as the .jsp page is unchanged)
use the same loaded class file.


Anatomy of a JSP Page


A JSP page is a mixture of standard HTML tags, web page content,
and some dynamic content that is specified using JSP constructs.
Everything except the JSP constructs is called
Template Text
.


JSP Constructs: JSP Comments


JSP Comments:

Different from HTML
comments



<!
--

an HTML comment
--
>



<%
--

a JSP comment
--
%>


JSP comments are used for documenting
JSP code and are not visible client
-
side
(using browser's View Source option) where
as HTML comments are visible.


JSP Constructs: JAVA Expressions


Format:

<%= some_java_expression %>


Example:

<%= new java.util.Date().toString() %>


Output of the expression is placed in the HTML
template at the same location.


JSP Constructs: Variables in JAVA
Expressions


There are some pre
-
defined Java variables/objects available for use in expressions (provide
access to important servlet functionality):


request

This is the same object as HttpServletRequest parameter in th get/post methods.
Same methods (like, getParameter, getAttribute, etc) can be applied to it.


out

The servlet printwriter.


session

Same as servlet session object.


Example:


<HTML>


<HEAD>


<TITLE>JSP Expressions: Predefined Objects</TITLE>


</HEAD>


<BODY>


<H1>Using Predefined Objects</H1>


<UL>


<LI> Your Hostname: <%= request.getRemoteHost() %>


<LI> Your Session ID: <%= session.getId() %>


<LI> The value of INFO parameter: <%= request.getParameter("INFO") %>


</BODY>


</HTML>


JSP Constructs: Scriptlets


Scriptlets are arbitrary pieces of Java code inserted in the page using the
format:

<% some_java_code %>

Example 1


<HTML>


<HEAD>


<TITLE>JSP: Scriptlets</TITLE>


</HEAD>


<%


String bgColor =
request.getParameter("COLOR");




if (bgColor == null)


bgColor = "WHITE";




%>


<BODY BGCOLOR="<%= bgColor %>" >


<H1>Example Scriptlet: Sets background
color</H1>




</BODY>


</HTML>


Example 2


<HTML>


<HEAD>


<TITLE>JSP: Scriptlets 2</TITLE>


</HEAD>


<% String bgColor = request.getParameter("COLOR"); %>




<% if (bgColor == null) { %>


<BODY BGCOLOR="FFFFFF" >


<% } else { %>


<BODY BGCOLOR="<%= bgColor %>" >


<% } %>




<H1>Example Scriptlet: Conditionally sets background
color</H1>


<% if (bgColor == null) { %>


You did not supply a color, I used white.


<% } else { %>


Here is the color you requested.


<% } %>




</BODY>


</HTML


JSP Constructs: Scriptlets, contd.


Using Arrays
:
One can easily use scriptlets to loop over arrays. In this example, the user is presented with
choice boxes. When s/he presses the submit button, the choices are displayed.


Example

<HTML>


<BODY BGCOLOR="WHITE">


<FORM ACTION="choices.jsp">


<INPUT type="checkbox" name="music" value="Classical"> Classical<BR>


<INPUT type="checkbox" name="music" value="Rock"> Rock<BR>


<INPUT type="checkbox" name="music" value="Jazz"> Jazz<BR>


<INPUT type="checkbox" name="music" value="Blues"> Blues<BR>


<INPUT type="checkbox" name="music" value="DC
-
GoGo"> DC GoGo<BR>


<INPUT type="submit" value="Submit">


</FORM>


<%


String[] selected = request.getParameterValues("music");


if (selected != null && selected.length != 0) {


%>


You like the following kinds of music:


<UL>


<%


for (int i = 0; i < selected.length; i++) {


out.println("<LI>" + selected[i]);


}


%>


<UL>


<% } %>


</BODY>


</HTML>


JSP Constructs: Declarations


You can define variables and/or methods:

<%! some JAVA declarations %>


Example

<HTML>


<BODY>


<%!


private int hitCount = 0;


String randomColor() {


java.util.Random random = new java.util.Random();


int R = (int) (random.nextFloat() * 255);


int G = (int) (random.nextFloat() * 255);


int B = (int) (random.nextFloat() * 255);


return "#" + Integer.toString(R, 16) + Integer.toString(G, 16) + Integer.toString(B, 16); }


%>

<FONT COLOR="<%= randomColor() %>" > This page has been accessed <%= ++hitCount %> times.
</FONT>


</BODY>

</HTML>


JSP Constructs: Summary


Expressions


Scriptlets


Declarations


Availability of pre
-
defined objects



JSP Directives


Format:


<%@ directive attribute="value" %>


<%@ directive attr1="value" attr2="value"
... attrN="value" %>


Directives are used to specify the structure of
the resulting servlet. There are three
directives: page, include, and taglib



JSP: Page Directive


There are 11 specifiable attributes for this directive: import, contentType, isThreadSafe, session, buffer,
autoflush, extends, info, errorPage, and language


Example


<%@ page language="java" contentType="text/html" %>




<%@ page contentType="application/vnd.ms
-
excel" %>


<%@ page import="java.util.*" %>




<%@ page import="java.util.*,java.io.*" %>


Directives can be placed anywhere in the document, but are often placed at the very top.


<%@ page language="java" contentType="text/html import="java.util.*" %>


<HTML>


<HEAD>


<TITLE>Hello World</TITLE>


</HEAD>


<BODY>


<H1>Hello World</H1>


Today is: <%= new Date().toString() %>


</BODY>


</HTML>

JSP: Other Directives


The include Directive


Is used to include a file in the main document.


There are two versions of this. The first one, shown below, includes
the file at translation time.


<%@ include file="relative URL of file" %>


The second version, includes the file at request time.


<jsp:include page="ralative URL of file" flush=true />


The taglib directive


This is used to define custom markup tags. Refer to JSP
documentation for details on this.