1. Trang chủ
  2. » Giáo Dục - Đào Tạo

Introduction to java server pages (lập TRÌNH WEB SLIDE)

23 22 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 23
Dung lượng 383 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Form Handling• Form data appended to request string Generates the request: Quantity:... Form HandlingGoal: – Extract values of parameters from the request – Generate appropriate respons

Trang 1

Server-side Web Programming

Lecture 3:

Introduction to Java Server Pages

Trang 2

Form Handling

• Form data appended to request string

Generates the request:

<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>

Trang 3

Form Handling

Goal:

– 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

Trang 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 Java html Java html

html html html

resulting html page

html html html html html html

Trang 5

Java Server Pages

Trang 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

Trang 7

Resulting html Page

2 + 2 evaluated to value of 4

Trang 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

Trang 11

Html Forms

• The FORM tag

<form action=”url of response page”

Method by which form

data is passed

Trang 12

Get vs Post Method

Get 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

Parameters limited to ~4k

of data No fixed limit to length of form data passed

Allows bookmarking of Prevents bookmarking

Trang 13

Simple Form Elements

<input type=“text” name=“elementname”/>

<input type=”submit” value=”buttonlabel”/>

Necessary for value to be sent to server

Trang 14

Form Element Example

<form action="orderReply.jsp" method="get">

Trang 15

Form Element Example

Trang 16

Form Parameter Passing

Parameter string passed:

quantity=137&customerName=John+Sullins&

Trang 17

Handling Form Data

• request object in JSP

Code in JSP

request

Trang 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

Trang 19

Example JSP

<body>

<%

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

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

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

Trang 20

Acquiring Form Data

• Statements to get and store form data:

Trang 21

Displaying Values in Response

john@cis.ysu.edu

Trang 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 >

Trang 23

Importing Library Classes

• Much of Java classes in separate libraries

• Must be imported to be used in JSP

the io library

Ngày đăng: 29/03/2021, 10:55

TỪ KHÓA LIÊN QUAN