Docsity

Java Server Pages (JSP) - Understanding JSP, Servlets, and Scripting Elements - Prof. Ala , Study notes of Computer Science

Partial preview of the text

Download Java Server Pages (JSP) - Understanding JSP, Servlets, and Scripting Elements - Prof. Ala and more Study notes Computer Science in PDF only on Docsity!

Java Server Pages

(JSP)

What is JSP

„ JSP simply puts Java inside HTML

pages. You can take any existing HTML

page and change its extension to ".jsp"

instead of ".html".

„ Scripting elements are used to provide

dynamic pages

JSP and Servlets

„ Each JSP page is turned into a Java

servlet, compiled and loaded. This

compilation happens on the first request.

After the first request, the file doesn't take

long to load anymore.

„ Every time you change the JSP file, it will

be re-compiled again.

Java Server Page

Java servlet source code

Java Servlet class file

translator compiler

Generated servlets

„ You can examine the source code

produced by the JSP translation process.

„ There is a directory called generated in

Sun Java J2EE Application Server where

you can find the source code.

„ Note that the _jspService method

corresponds to the servlet service method

(which is called by doGet or doPost)

JSP Directive Examples

„ Import java packages

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

„ Multiple import statements

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

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

„ Including file at translation time

<%@ include file="header.html" %>

„ For include the path is relative to the JSP file

JSP Scripting Elements: Expressions

„ For an expression scripting element like

<%= expr %>, expr is evaluated and the

result is converted to a string and placed

into the JSP's servlet output stream. In a

Java servlet this would be equivalent to

PrintWriter out = response.getWriter();
out.print(expr);

JSP Expression Examples

„ Displaying request parameters (request is

an implicit object available in a JSP)

„ Doing calculations

Your name is <%= request.getParameter("name") %>
and your age is <%= request.getParameter("age") %>
The value of pi is <%= Math.PI %> and the square root
of two is <%= Math.sqrt(2.0) %> and today's date is
<%= new java.util.Date() %>.

JSP Scripting Elements: Scriptlet

„ For a scriplet <% statements %> the Java

statements are placed in the translated servlet's

_jspService method body (which is called by

either doGet or doPost)

public void _jspService(HttpServletRequest request, HttpServletResponse response) throws java.io.IOException, ServletException {... statements ... }

JSP Declaration examples

„ Declaring instance variables

„ Declaring methods

<%! private int count = 0; %> ... The count is <%= count++ %>.

private int toInt(String s) { return Integer.parseInt(s); } %>

Including Files

„ Including files at translation time (when

JSP is translated to a servlet)

„ Including files at request time

<%@ include file="filename" %>

A simple JSP

JSP Test

JSP Test Time: <%= new java.util.Date() %>

The expression scripting
element <%= ... %> is
equivalent to the scriptlet
<% out.print(...); %>

The Implicit out Object

„ In a scriptlet <% ... %> you can use the

out object to write to the output stream.

„ Example:

out.print("The sum is "); out.print("1 + 2 = " + (1+2)); %>

JSP Processing ...

JSP Processing form with POST

First name: Last name:

Example: HTML Form Submission Using POST

ProcessForm.jsp:

JSP Form Results

JSP Form Results Hello <%= request.getParameter("firstName") %> <%= request.getParameter("lastName") %>

Processing Submitted HTML Forms

Temperature Conversion Example

<%@ page import="java.text.DecimalFormat" %>

Fahrenheit ... Conversion

Fahrenheit to Celsius Conversion <% String self = request.getRequestURI(); if (request.getParameter("convert") == null) { %>

Fahrenheit temperature:

else { double fahr = 0.0; try { fahr = Double.parseDouble( request.getParameter("fahrenheit")); } catch (NumberFormatException e) { // do nothing, accept default value }

Temperature Conversion Example (Contd.)

Setting properties

„ To set a property of a bean use

A Greeting Bean

package beans; public class Greeting { private String greeting; // the property

public Greeting() { greeting = "Hello World"; }

public String getGreeting() { return greeting; }

public void setGreeting(String g) { greeting = (g == null)? "Hello World" : g; } }

Java Beans Naming convention

If the property name is greeting

„ The get method must have the name

getGreeting

„ The set method must have the name

setGreeting

Example 1: Using Java Beans in JSP Files

Greeting JSP that uses a Greeting bean

Greeting JSP that uses a Greeting bean

Greeting JSP using two Greeting beans Greeting JSP using two Greeting beans

1st bean:

2nd bean:

Example 2: Using Java Beans in JSP Files

„ Two Beans: One initialized explicitly and the

other is initialized using a request parameter

<%-- Following works but param method is better --%>

" />

„ Three Beans: One initialized explicitly, one is

initialized using a request parameter, and one

is initialized using getParameter

Example 3: Using Java Beans in JSP Files

Reference

„ COS 2206 JSP, Barry G. Adams, Department of Mathematics and Computer Science, Laurentain University.