Objectives Review: HTML Forms Intro to Java Server-side Web - - PDF document

objectives
SMART_READER_LITE
LIVE PREVIEW

Objectives Review: HTML Forms Intro to Java Server-side Web - - PDF document

Objectives Review: HTML Forms Intro to Java Server-side Web Technology April 26, 2019 Sprenkle - CS335 1 Review: HTML Forms What attribute is required in a form form tag? What attribute is optional? What attribute do we use to


slide-1
SLIDE 1

1

Objectives

  • Review: HTML Forms
  • Intro to Java Server-side Web Technology

April 26, 2019 Sprenkle - CS335 1

Review: HTML Forms

  • What attribute is required in a form

form tag?

Ø What attribute is optional?

  • What attribute do we use to create different

types of input input?

  • How do we distinguish between input data?
  • How do we “group” radio buttons and checkbox

buttons?

  • What tag do we use to improve usability of our

radio buttons and checkboxes?

  • When should we use “get” vs “post”?

April 26, 2019 Sprenkle - CS335 2

slide-2
SLIDE 2

2

Why Requirements/Specifications?

  • “Most programmers regard anything that

doesn’t generate code to be a waste of time. Thinking doesn’t generate code, and writing code without thinking is a recipe for bad code. Before we start to write any piece of code, we should understand what that code is supposed to do. Understanding requires thinking, and thinking is hard.”

  • In the words of the cartoonist Dick Guindon:

“Writing is nature’s way of letting you know how sloppy your thinking is.”

April 26, 2019 Sprenkle - CS335 3

Source: http://www.wired.com/opinion/2013/01/code- bugs-programming-why-we-need-specs

INTRODUCTION TO SERVER-SIDE PROGRAMMING

April 26, 2019 Sprenkle - CS335 4

slide-3
SLIDE 3

3

Architecture of the Web

April 26, 2019 Sprenkle - CS335 5

Client Server Web Browser Web Server Web Application Server

  • Makes requests,

renders responses

  • Executes JavaScript,

client-side code handles static requests handles dynamic requests Service-oriented Alternative: Client is another server/non-user computer Presentation-oriented:

Web Application Architecture

April 26, 2019 Sprenkle - CS335 6

Persistent State User Interface (HTML, CSS, JavaScript) Web Application Server

Application State

slide-4
SLIDE 4

4

Java-based Web Application Server

  • Web Application Server

Ø Container to run the Java-based web applications Ø Typically listens on port 8080 (rather than 80)

April 26, 2019 Sprenkle - CS335 7

Server Web Application Server Client HTTP Request HTTP Response: HTML Document Can have multiple Web applications running

Architecture of the Web

  • Web Application Server

Ø Parses request, including data Ø Executes request Ø Returns response (often an HTML document)

  • May do other things, like send email, …

April 26, 2019 Sprenkle - CS335 8

Server Web Application Server Client HTTP Request HTTP Response: HTML Document

slide-5
SLIDE 5

5

Request Handling in Java

April 26, 2019 Sprenkle - CS335 9

HTTP Request HTTP Response: HTML Document Web Application Server

HttpServlet Request HttpServlet Response

Web Components

Our Focus Start here

Servlets

  • A Java class that extends the functionality of web

servers

Ø Processes requests on server Ø Sends results (typically as an HTML file) back to client

  • In javax.servlet.* packages

Ø Part of Java Enterprise Edition (EE), as a separate download Ø Eclipse for EE development (Web Tools Platform)

  • Java’s answer to CGI (Common Gateway Interface)
  • Portable, more secure (no buffer overflows)
  • Supported by many major Web servers

Ø E.g., Apache Tomcat, Jetty, WebSphere, etc.

April 26, 2019 Sprenkle - CS335 10

slide-6
SLIDE 6

6

The Servlet Servlet Interface

  • javax.servlet.Servlet
  • All servlets implement the Servlet

Servlet interface Ø HttpServlet, GenericServlet, FacesServlet ØWeb application server invokes many methods of

Servlet Servlet automatically

April 26, 2019 Sprenkle - CS335 11

Review from CSCI209: What is an interface?

The HttpServlet HttpServlet Class

  • Web-based servlets typically extend HttpServlet

HttpServlet

Ø Implements Servlet Servlet interface

  • HttpServlet

HttpServlet implements the service service method

Ø Parameters

  • HttpServletRequest

HttpServletRequest - from the client

  • HttpServletResponse

HttpServletResponse - to the client

Ø service service calls the respective method (e.g., doGet or doPost) in response to a HTTP GET or POST request

  • Recall:

Ø GET - data encoded in URL

  • Request a resource (file) or retrieve data

Ø POST - data encoded in body of message

  • Upload data; processing; hide data from URL

April 26, 2019 Sprenkle - CS335 12

HttpServlet Request HttpServlet Response Http Servlet

slide-7
SLIDE 7

7

HttpServletResponse HttpServletResponse

  • Provides output streams and methods

to write data to the client

  • Methods:

Ø ServletOutputStream ServletOutputStream getOutputStream getOutputStream() Ø PrintWriter PrintWriter getWriter getWriter() Ø void void setContentType(String setContentType(String)

  • Specifies the MIME type of the response

ØBrowser knows what it received and how to format it

  • “text/html” specifies an HTML document

April 26, 2019 Sprenkle - CS335 13

HttpServlet Request HttpServlet Response Http Servlet

Client CSCI209 Flashback: Difference between streams and writers?

HttpServletResponse HttpServletResponse methods

  • ServletOutputStream

ServletOutputStream getOutputStream getOutputStream() () ØObtains a byte output stream that enables the servlet to send binary data to the client

  • PrintWriter

PrintWriter getWriter getWriter() () ØObtains a text writer that enables the servlet to send character data (text) to the client

  • void

void setContentType(String setContentType(String) ØSpecifies the MIME type of the response

  • Browser knows what it received and how to format it

Ø“text/html” specifies an HTML document

April 26, 2019 Sprenkle - CS335 14

slide-8
SLIDE 8

8

Example Communication with Servlet

  • HTML page with a form containing a submit

button

Ø Triggers client’s request

  • When the button is pressed, browser sends the

servlet a GET request

April 26, 2019 Sprenkle - CS335 15

Client Web Application Server Web Browser: HTML Form Servlet HTTP Request HTTP Response: HTML Document

Example Servlet Flow

April 26, 2019 Sprenkle - CS335 16

Server Web Application Server Client HTTP Request, data Response HTML Form HTML Document Web Browser s u b m i t doGet 1 2 3 4

slide-9
SLIDE 9

9

To Generate a Response

  • doGet

doGet method needs to

Ø Obtain an output writer to write back to the client Ø Generate/write the HTML page to the client using the writer Ø Close the writer

April 26, 2019 Sprenkle - CS335 17

How can we implement these steps?

void doGet(HttpServletRequest request, HttpServletResponse response)

Accepting Certain Types of Requests

  • We can design the servlet to only accept/handle

GET requests

ØOverride the doGet doGet method ØCould return an error inside of doPost doPost

  • Could do the opposite: only accept/handle POST

requests

April 26, 2019 Sprenkle - CS335 18

slide-10
SLIDE 10

10

HTTP Response Errors

  • HttpServletResponse

HttpServletResponse has a method for returning errors and fields that define errors codes

  • Example status code fields:

Ø SC_HTTP_VERSION_NOT_SUPPORTED Ø SC_METHOD_NOT_ALLOWED Ø SC_NOT_IMPLEMENTED

April 26, 2019 Sprenkle - CS335 19

void void sendError( sendError(int int statusCode statusCode [, [, String String msg msg]) ])

IN-CLASS WORK

Tomcat Eclipse/Tomcat Using Eclipse

HTML, CSS Start servlets

April 26, 2019 Sprenkle - CS335 20

slide-11
SLIDE 11

11

Handling Data

  • So far, we haven’t done anything with data that

comes with the request

  • Data is stored in a hashtable-like object

April 26, 2019 Sprenkle - CS335 21

Requests for a digital publication library:

GET dspace/search?search=xxx&sort=date&title=Title

“Title” “date” “xxx” “title” “sort” “search”

HttpServletRequest HttpServletRequest

  • Provides input streams and methods to read data from

the client

  • Methods:

Ø String String getParameter getParameter(String (String paramname paramname)

  • Returns the value of a request parameter

Ønull if parameter doesn’t exist Ø String[] String[] getParameterValues(String getParameterValues(String paramname paramname)

  • Returns an array of Strings containing the values for a

specific request parameter

Ø Enumeration<String> Enumeration<String> getParameterNames getParameterNames() ()

  • Returns the names of all of the parameters passed in

the request

April 26, 2019 Sprenkle - CS335 22

HttpServlet Request HttpServlet Response Http Servlet

slide-12
SLIDE 12

12

HttpServletRequest HttpServletRequest Methods

  • getParameter("title")

ØReturns “Title”

  • getParameterValues("sort")

ØReturns [ “date” ]

  • Enumeration<String>

getParameterNames()

ØReturns the names of all of the parameters passed to the servlet

April 26, 2019 Sprenkle - CS335 23

Requests for a digital publication library:

GET dspace/simple-search?search=xxx&sort=date&title=Title

Note these are Strings

A More Complex Example: Survey

  • An HTML form that asks the user for their

favorite type of pet

  • After user submits the form, the server sends

back the current results of the survey

  • Uses object serialization to write to/read from

file

April 26, 2019 Sprenkle - CS335 24

slide-13
SLIDE 13

13

Deployment: WAR files

  • Web Archives

Ø Analog to JAR files Ø Bundles together all the code, files for the web application

  • Copy into webapps directory of web application

server

Ø Server will automatically extract files and run Ø Procedure for Apache/Tomcat and Resin

  • Can export WAR files from Eclipse

Ø For submissions MUST include source code

April 26, 2019 Sprenkle - CS335 25

TODO

  • Complete Lab 3

Ø Due tonight at midnight

  • Requirements due tonight at midnight

Ø Pick 3 “key” pages to mock up

  • Basis for other pages
  • Read “Quality Attributes of Web Software

Applications”

Ø See Course Schedule for link Ø Writeup on Sakai Ø Due Wednesday @ midnight

April 26, 2019 Sprenkle - CS335 26