Top Banner
Server-side Web Programming Lecture 3: Introduction to Java Server Pages
23

Lecture3

May 12, 2015

Download

Technology

thanhchuongnl
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Lecture3

Server-side Web Programming

Lecture 3: Introduction to Java Server Pages

Page 2: Lecture3

2

Form Handling

• Form data appended to request string

Generates the request:http://frodo.cis.ysu.edu/~john/cgi-bin/test.pl&quantity=3

<FORM NAME="purchaseform" METHOD=GET ACTION=http://frodo.cis.ysu.edu/~john/cgi-bin/test.pl>Quantity: <INPUT TYPE="text" SIZE="8" NAME="quantity" /><BR /><BR /><INPUT TYPE="submit" VALUE="SUBMIT"></FORM>

Page 3: Lecture3

3

Form HandlingGoal:

– Extract values of parameters from the request– Generate appropriate response page based on

parameter values– Take other appropriate action based on request

• Add to shopping cart• Modify or query database

Page 4: Lecture3

4

Server Page Model

• Html document with executable code interspersed• When page requested:

– Code executed– Html generated and inserted in its place– Final all html document sent back as response

request for somepage.jsp

Tomcat server

somepage.jsp

html html Javahtml Java htmlhtml html htmlJava html html

resulting html page

html html htmlhtml html htmlhtml html htmlhtml html html

Page 5: Lecture3

5

Java Server Pages

<HTML><HEAD><TITLE>cgi-bin response</TITLE></HEAD><BODY><P>Thank you for your order of <%= request.getParameter(“quantity”) %>widgets!</P></BODY></HTML>

Page 6: Lecture3

6

JSP Syntax

• Basic tag form: <% … %>

• Simplest form:<%= some Java expression %>

– Tomcat evaluates expression to get value– Inserts that value in place of expression in generated

html page

Page 7: Lecture3

7

JSP Simple Example• Simple example:

<html><body><p>Two plus two is <%= 2 + 2 %>.</p></body></html>

<html><body><p>Two plus two is 4.</p></body></html>

Java Server Page

Resulting html Page

2 + 2 evaluated to value of 4

Page 8: Lecture3

8

JSP Syntax• Basic tag form: <% … %>• Executes code inside brackets without generating html

– Set variables later used to generate html later– Store/access values in session/databases

<html><body><% int sum = 2 + 2; %><p>Two plus two is <%= sum %>.</p></body></html>

Page 9: Lecture3

9

JSP Syntax

<html><body><% int sum = 2 + 2; %><p>Two plus two is <%= sum %>.</p></body></html>

Stores value of 4 in sum variable

Value of 4 in sum used in this JSP

<html><body><p>Two plus two is 4.</p></body></html>

No html here

Page 10: Lecture3

10

JSP Scoping

• Variable declared in a block of JSP on a page

• May be accessed by any other JSP on same page

• No access to variable from any other page (purpose of sessions)

<html><body><% int sum = 2 + 2; %><p>

Two plus two is <%= sum %>.</p></body></html>

Page 11: Lecture3

11

Html Forms

• The FORM tag<form action=”url of response page” method=”get or post”>…</form>

Relative url if response page is part of same webapp

Method by which form data is passed

Page 12: Lecture3

12

Get vs. Post MethodGet method Post method

Default method Must specify in FORM

Appends form data to end of URL

Form data not visible(some security against shoulder surfing, but still need encryption for total security)

Faster

No fixed limit to length of form data passed

Parameters limited to ~4k of data

Allows bookmarking of pages

Prevents bookmarking(good if part of session that user should no be able to enter in middle)

Page 13: Lecture3

13

Simple Form Elements

• TEXT tag<input type=“text” name=“elementname”/>

• SUBMIT tag<input type=”submit” value=”buttonlabel”/>

Necessary for value to be sent to server

Page 14: Lecture3

14

Form Element Example

<form action="orderReply.jsp" method="get"> <table cellspacing="5"> <tr> <td align="right">Number to purchase:</td> <td><input type="text" name="quantity"></td> </tr><tr> <td align="right">Your name:</td> <td> <input type="text" name="customerName"></td> </tr><tr> <td align="right">Your email:</td> <td> <input type="text" name="customerEmail"></td> </tr><tr> <td></td> <td><input type="submit" value="Place Order"></td> </tr> </table><form>

Page 15: Lecture3

15

Form Element Example

Page 16: Lecture3

16

Form Parameter Passing

Parameter string passed:quantity=137&customerName=John+Sullins&[email protected]

Page 17: Lecture3

17

Handling Form Data

• request object in JSP– Java object created from request string– Contains request data and methods to easily access that

data– Accessed by JSP code

Data from formOther data about request

methods to access data about the request

Code in JSP

request

Page 18: Lecture3

18

Handling Form Data

• Most useful method:String request.getParameter(String)

• Example: String name =

request.getParameter("customerName"); sets the value of “name” to “John Sullins”

Takes name of form element as parameter

Returns the corresponding value passed to the server

Page 19: Lecture3

19

Example JSP<body>

<% String name = request.getParameter("customerName"); String email = request.getParameter("customerEmail"); String quantity = request.getParameter("quantity"); %>

<h2>Order Confirmation</h2> <p> Thank you for your order of <%= quantity %> widgets, <%= name %>. </p> <p> You will shortly receive an email confirmation at <%= email %>. </p></body>

Page 20: Lecture3

20

Acquiring Form Data

• Statements to get and store form data: <%

String name = request.getParameter("customerName");

String email = request.getParameter("customerEmail");

String quantity = request.getParameter("quantity");

%>

name “John Sullins”

email “[email protected]

quantity “137”

Page 21: Lecture3

21

Displaying Values in Response<p>

Thank you for your order of <%= quantity %> widgets, <%= name %>.

</p><p>

You will shortly recieve an email confirmation at <%= email %>.

</p>

137John Sullins

[email protected]

Page 22: Lecture3

22

Commenting JSP Files

• Crucial to future maintenance of site

• Inside of JSP code (between <% and %>):// comment/* comment */

• Outside of JSP code (that is, in html)<!-- comment -->

Page 23: Lecture3

23

Importing Library Classes

• Much of Java classes in separate libraries• Must be imported to be used in JSP• Syntax:<%@ page import=”list of Java classes” %>

• Example:<%@ page import=”java.util.Date,

java.io.*” %> Date class

in the util library

All classes in

the io library