CSE 510 Web Data Engineering Java Servlets UB CSE 510 Web Data - - PowerPoint PPT Presentation

cse 510 web data engineering
SMART_READER_LITE
LIVE PREVIEW

CSE 510 Web Data Engineering Java Servlets UB CSE 510 Web Data - - PowerPoint PPT Presentation

CSE 510 Web Data Engineering Java Servlets UB CSE 510 Web Data Engineering Install and Check Tomcat 2 UB CSE 510 Web Data Engineering Installing Tomcat Install stable production release We will be using Apache Tomcat version 6.0.20


slide-1
SLIDE 1

CSE 510 Web Data Engineering

Java Servlets

UB CSE 510 Web Data Engineering

slide-2
SLIDE 2

UB CSE 510 Web Data Engineering 2

Install and Check Tomcat

slide-3
SLIDE 3

UB CSE 510 Web Data Engineering 3

Installing Tomcat

  • Install stable production release

– We will be using Apache Tomcat version 6.0.20 – Do not install alpha, beta, milestone or nightly builds

  • You need Java SE Development Kit (JDK) 6
  • On Windows, use the self-extracting .exe and

follow directions

slide-4
SLIDE 4

UB CSE 510 Web Data Engineering 4

Starting and Testing Tomcat

  • Start Tomcat using bin/startup.bat or “Start

Tomcat” icon in program group

– Preferably, do not set up Tomcat as an “automatic start” service

  • You can also use a Tomcat Launcher Plug-in

within Eclipse

  • Open http://localhost:8080/

– You should see Jakarta project home page

  • Open

http://localhost:8080/examples/jsp/dates/date.jsp

slide-5
SLIDE 5

UB CSE 510 Web Data Engineering 5

HTTP Requests and Responses

slide-6
SLIDE 6

UB CSE 510 Web Data Engineering 6

HTTP Basics

  • TCP/IP protocol used by web servers
  • Synchronous

– Client sending request waits for response

  • Stateless

– All info needed by server-side must be contained in HTTP request – Using appropriate session management techniques, we can go around restrictions of statelessness

  • We show next the request and response message

strings that go back and forth in interactions

– Only for educational purposes – You will never code such strings directly – The application server will do it for you

slide-7
SLIDE 7

UB CSE 510 Web Data Engineering 7

Syntax of an HTTP Request

  • <method> <request URI> <HTTP-version>

– Important ones: GET & POST – See Table 3.1 of textbook for explanations of other methods: HEAD, PUT, DELETE, CONNECT, OPTIONS, TRACE

  • Header fields

– Accept: text/html, text/xml, … (acceptable response types)

  • Message body (optional) (after blank line)
slide-8
SLIDE 8

UB CSE 510 Web Data Engineering 8

Example

GET / HTTP/1.1 Host: db.cse.buffalo.edu User Agent: IE/5.0 Accept: text/html, text/xml …

slide-9
SLIDE 9

UB CSE 510 Web Data Engineering 9

Syntax of an HTTP Response

  • <HTTP-version> <status-code> <reason>

– For example, status codes from 500-599 indicate server-side errors – See Table 3.2 for typical HTTP response codes

  • Header fields

– Content-Type: text/html (or other type)

  • Message body (optional) (after blank line)
slide-10
SLIDE 10

UB CSE 510 Web Data Engineering 10

Communicating Data Provided in Forms: GET, POST and Parameters

  • Consider the multiplication page

<html> <head><title>Multiplier Form</title></head> <body> Welcome to the page that multiplies by 3 <p /> <form method=“GET” action=“multiply”> Provide the number to be multiplied: <input type=“text” name=“num”/> <p /> <input type=“submit” value=“Click to Submit”/> </form> </body> </html>

slide-11
SLIDE 11

UB CSE 510 Web Data Engineering 11

When and How to Use POST (Instead of GET)

  • Upon submitting “3” the browser emits URL

http://localhost:8080/multiplier/multiply?num=4 GET /multiplier/multiply?num=4 HTTP/1.1 Host: localhost:8080

  • If your HTML form may create more than 255

characters use <form method=“POST” …

– Form data will be in body of http request POST /multiplier/multiply HTTP/1.1 Host: localhost:8080 num=4

slide-12
SLIDE 12

UB CSE 510 Web Data Engineering 12

More Input Forms: Dropdown Menus

slide-13
SLIDE 13

UB CSE 510 Web Data Engineering 13

More Input Forms: Checkboxes

slide-14
SLIDE 14

UB CSE 510 Web Data Engineering 14

Encoding URIs

  • HTTP only permits letters, digits, underscores

and a few more

  • Browsers take care of “special” symbols, using

the RFC2277 encoding

slide-15
SLIDE 15

UB CSE 510 Web Data Engineering 15

Example of Encoding Characters in a URI Using the RFC2277

  • Consider a page asking for emails

<html> <head><title>Email Submit Page</title></head><body> <form method=“GET” action=“http://localhost:8080/subemail.jsp”> Type your e-mail here: <input type=“text” name=“eml”/> <p /> <input type=“submit” value=“Click Here”/> </form></body></html>

  • User types mpetropo@buffalo.edu

GET /subemail.jsp?eml=mpetropo%40buffalo.edu HTTP/1.1 Host: localhost:8080

slide-16
SLIDE 16

UB CSE 510 Web Data Engineering 16

Some Useful Aspects of HTTP

  • URI redirection
  • Refresh

– Instruct the browser to reload every N seconds – <meta http-equiv=“refresh” content=“300”> – Refresh: 300

slide-17
SLIDE 17

UB CSE 510 Web Data Engineering 17

Servlets: The 101 of Java-based Web Server-Side Programming

slide-18
SLIDE 18

UB CSE 510 Web Data Engineering 18

Java-Based Server-Side Programming 101: Servlets

  • Servlet: Java program

run inside the app server (Tomcat)

  • Inputs HTTP requests

– App server provides them in appropriate

  • bject format
  • Typically (but not

necessarily) return HTTP responses of HTML content type

App Server Java Servlet Browser

HTTP Request HTTP Response (HTML Content)

slide-19
SLIDE 19

UB CSE 510 Web Data Engineering 19

Multiplication Form and Servlet: The HTML Form Gets Input, Calls Servlet

  • Create Web app (directory) multiplier under

webapps

  • Place multiplier.html in it
  • Browse to

http://localhost:8080/multiplier/multiplier.html

  • When form is submitted, browser issues HTTP GET

request

– ACTION specifies URL to be invoked – URL of servlet may be relative, as in multiplier.html,

  • r absolute:

http://localhost:8080/multiplier/multiply

slide-20
SLIDE 20

UB CSE 510 Web Data Engineering 20

MyMultiplier.java

import java.io.*; /* following packages encapsulate Servlet API */ import javax.servlet.*; import javax.servlet.http.*; public class MyMultiplier extends HttpServlet { /* Overrides doGet coming with HttpServlet */ public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

slide-21
SLIDE 21

UB CSE 510 Web Data Engineering 21

MyMultiplier.java

res.setContentType(“text/html”); /* By having set content to text/html */ /* PrintWriter encodes accordingly */ PrintWriter out = res.getWriter();

  • ut.println(“<html><head><title>Multiply by “ + 3

+ “</title></head><body>”); String parameter = req.getParameter(“num”); /* Ignoring the possibility that parameter is not integer */

  • ut.println(parameter + “ * ” + 3 + “ = ” +

3 * (Integer.parseInt(parameter)));

  • ut.println(“</body></html>”);

} }

slide-22
SLIDE 22

UB CSE 510 Web Data Engineering 22

Compiling & Deploying the Servlet

  • Place MyMultiplier.java in multiplier/src

– Not necessary, but good principle to separate java sources from classes

  • Compile MyMultiplier.java

– Include in CLASSPATH environment variable <CATALINA_HOME>\lib\servlet-api.jar

  • Place MyMultiplier.class in

multiplier/WEB-INF/classes

slide-23
SLIDE 23

UB CSE 510 Web Data Engineering 23

Deployment Descriptor & URL Mapping

  • Map the servlet class to a URL pattern in the deployment

descriptor multiplier/WEB-INF/web.xml <web-app> <servlet> <servlet-name>multiplier</servlet-name> <servlet-class>MyMultiplier</servlet-class> </servlet> <servlet-mapping> <servlet-name>multiplier</servlet-name> <url-pattern>/multiply</url-pattern> </servlet-mapping> </web-app>

  • After restarting Tomcat, you can access servlet at

http://localhost:8080/multiplier/multiply?num=4

slide-24
SLIDE 24

UB CSE 510 Web Data Engineering 24

Deployment Descriptor & URL Mapping

  • URL pattern may include * (wildcard)

<servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>

  • Any URL pattern matching *.do will invoke the

action servlet

  • We’ll see this again in Struts implementations

(indeed this example is from Struts)

slide-25
SLIDE 25

UB CSE 510 Web Data Engineering 25

Servlet Life Cycle

  • First time a servlet is called:

– init() method is invoked – Normally provided by HttpServlet – Unless you want to set up resources that exist for the whole lifetime of the servlet (rare) – Object (extending HttpServlet) is instantiated and becomes memory resident from now on – Class variables exist for entire life of object

  • Series of GET, POST, … HTTP calls lead to

doGet(), doPost(), etc method invocations

  • Servlet removed with destroy()

– Tomcat may call destroy() any time – You may write your own destroy() to save state

slide-26
SLIDE 26

UB CSE 510 Web Data Engineering 26

Handling POST Method Calls

  • Whether parameters are communicated by GET or

POST is normally irrelevant to your code

  • However, you have to provide (override)

doPost() of HttpServlet public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { doGet(req, res); }

slide-27
SLIDE 27

UB CSE 510 Web Data Engineering 27

Handling Other Method Calls

  • DELETE, HEAD, OPTIONS, PUT, TRACE
  • Corresponding doDelete(), doHead(), etc
  • Normally developer does nothing
  • HttpServlet provides defaults
slide-28
SLIDE 28

UB CSE 510 Web Data Engineering 28

Servlet Initialization Parameters: Definition in web.xml

  • Assume we want to change the multiplication

factor without having to change and recompile the MyMultiplier.java servlet

  • Add initialization parameter in web.xml

<web-app> <servlet> <!-- … servlet stuff we’ve seen … --> <init-param> <param-name>times</param-name> <param-value>5.0</param-value> </init-param> </servlet> </web-app>

slide-29
SLIDE 29

UB CSE 510 Web Data Engineering 29

Servlet Initialization Parameters: Use in Servlets

  • Access to initialization parameters by invoking

getInitParameter String timesStr = getInitParameter(“times”);

slide-30
SLIDE 30

UB CSE 510 Web Data Engineering 30

Servlet Context Path

  • Default context name of Web application is the

name of the webapps subdirectory

– In running example, multiplier

  • Create alias context name if you want to hide the

subdirectory name or effect non-default actions

  • n your application’s servlets
  • Add Context element in conf/server.xml, inside

<Host name=“localhost” …>

– <Context path=“/mult” docbase=“multiplier”/>

  • Path is matched against URLs’ beginning

– Must be unique – Try http://localhost:8080/mult/multiply?num=4

slide-31
SLIDE 31

UB CSE 510 Web Data Engineering 31

Automatic Reload

  • Default configuration does not check whether

class files are replaced

– Appropriate setting in production mode

  • We can avoid stopping and restarting Tomcat

during development/compilation by enabling automatic reloading of servlet class files

– For an individual web application, add file context.xml under <WEBAPP_HOME>/META-INF/ and just add <Context path="" reloadable="true"> – To effect automatic reload for all applications add, edit file <CATALINA_HOME>\conf\context.xml, and add reloadable=“true” attribute to the Context element

slide-32
SLIDE 32

UB CSE 510 Web Data Engineering 32

What is Wrong with Servlets

  • The “look” of the resulting HTML is buried in

println() statements

  • Web designers cannot work this way
  • Business logic and presentation horribly mixed
  • other issues…
slide-33
SLIDE 33

UB CSE 510 Web Data Engineering 33

Some Additional Items for Your “To Do” List

  • Automatic Reloading of Servlets
  • Deploy and modify the programs we’ve

seen