Web Programming with Java pptx

68 984 0
Web Programming with Java pptx

Đ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

1 Web Programming with Java Servlets Huynh Huu Viet University of Information Technology Department of Information Systems Email: viethh@uit.edu.vn 2008 © Department of Information Systems - University of Information Technology 2 Outline Introduction Overview of Servlet technology Servlet basics The Servlet Lifecycle Retrieving and Sending HTML Servlet Sessions 2008 © Department of Information Systems - University of Information Technology 3 Introduction Java networking capabilities:  Socket-based and packet-based communications • Package java.net  Remote Method Invocation (RMI) • Package java.rmi  Servlets and JavaServer Pages (JSP) • Request-response model • Packages javax.servlet – javax.servlet.http – javax.servlet.jsp  the Web tier of J2EE 2008 © Department of Information Systems - University of Information Technology 4 Introduction (cont.) Servlets  Thin clients  Request/response mechanism  Session-tracking capabilities  redirection/forwarding control Servlets: small Java programs that run on a web server  Just as applets run on browser Provide web-based applications Extends functionality of web server 2008 © Department of Information Systems - University of Information Technology 5 Outline Introduction Overview of Servlet technology Servlet basics The Servlet Lifecycle Retrieving and Sending HTML Servlet Sessions 2008 © Department of Information Systems - University of Information Technology 6 A Servlet's Job  Read explicit data sent by client (form data)  Read implicit data sent by client (request  headers)  Generate the results  Send the explicit data back to client (HTML or binary)  Send the implicit data back to client (status codes and response headers) 2008 © Department of Information Systems - University of Information Technology 7 Why Build Web Pages Dynamically? The web page is based on data given by the user  results from search engines and order confirmation pages at on-line stores The web page is derived from data that changes frequently  weather report or news headlines page The web page uses information from databases or other server-side sources  e-commerce site can list current price and availability 2008 © Department of Information Systems - University of Information Technology 8 Outline Introduction Overview of Servlet technology Servlet basics The Servlet Lifecycle Retrieving and Sending HTML Servlet Sessions 2008 © Department of Information Systems - University of Information Technology 9 Servlet Basics Servlet container (or Servlet engine)  Server that executes a Servlet Web servers and application servers  Netscape iPlanet Application Server  Microsoft’s Internet Information Server (IIS)  Apache HTTP Server  BEA’s WebLogic Application Server  IBM’s WebSphere Application Server  JBoss Application Server 2008 © Department of Information Systems - University of Information Technology 10 Servlet Architecture [...]...Creating a Servlet Need to get Servlet libraries on CLASSPATH Part of J2EE application server Also come with Tomcat reference implementation For a general Servlet you must subclass javax.servlet.GenericServlet Seldom do this To process HTTP requests you subclass javax.servlet.http.HttpServlet Override doGet() or doPost() to handle GET and POST requests from browser 2008 © Department... socket over again for requests very close together from the same client (e.g., the images associated with a page, or cells within a framed page) Servlets can't do this unilaterally; the best they can do is to give the server enough info to permit persistent connections So, they should set Content-Length with setContentLength (using ByteArrayOutputStream to determine length of output) Cookie Gives cookies... HTTP/1.1 version User-Agent Best used for identifying category of client • Web browser vs I-mode cell phone, etc For Web applications, use other headers if possible Again, can be easily spoofed 2008 © Department of Information Systems - University of Information Technology 29 Common HTTP 1.1 Request Headers (4) Referer URL of referring Web page Useful for tracking traffic; logged by many servers Can also... when form submitted • getParameter returns an empty string (or possibly a string with whitespace in it) Must check for null before checking for empty string • String param = request.getParameter("someName"); • if ((param == null) || (param.trim().equals(""))) { – doSomethingForMissingValues( ); • } else { – doSomethingWithParameter(param); • } Malformed • Value is a nonempty string in the wrong format... 2008 © Department of Information Systems - University of Information Technology 22 Handling Missing and Malformed Data Use default values Replace missing values with application-specific standard values Redisplay the form Show the form again, with missing values flagged Previously-entered values should be preserved Four options to implement this • Have the same servlet present the form, process the data,... Browsers go to new location automatically Browsers are technically supposed to follow 301 and 302 (next page) requests only when the incoming request is GET, but do it for POST with 303 Either way, the Location URL is retrieved with GET 2008 © Department of Information Systems - University of Information Technology 35 Common HTTP 1.1 Status Codes (2) 302 (Found) Requested document temporarily moved elsewhere... present the results • Have a JSP page “manually” present the form; have a servlet or JSP page process the data and present the results • Have a JSP page present the form, automatically filling in the fields with values obtained from a data object Have a servlet or JSP page process the data and present the results 2008 © Department of Information Systems - University of Information Technology 23 Outline Introduction... preferences and then return to the page they came from Can be easily spoofed; don't let this header be sole means of deciding how much to pay sites that show your banner ads Some browsers (Opera), ad filters (Web Washer), and personal firewalls (Norton) screen out this header 2008 © Department of Information Systems - University of Information Technology 30 Example public class WrongDestination extends HttpServlet... HttpServletRequest req Process and log as required Write response to client • HttpServletResponse res HttpServletResponse has getWriter() method Returns a PrintWriter to write data to client Data starts with content type Use setContentType() before writing any data E.g res.setContentType("text/html"); 2008 © Department of Information Systems - University of Information Technology 12 Example 2008 © Department... header) Browsers go to new location automatically Servlets should use sendRedirect, not setStatus, when setting this header See example 401 (Unauthorized) Browser tried to access password-protected page without proper Authorization header 404 (Not Found) No such page Servlets should use sendError to set this Problem: Internet Explorer and small (< 512 bytes) error pages IE ignores small error page by . 1 Web Programming with Java Servlets Huynh Huu Viet University of Information Technology Department of Information. Technology 3 Introduction Java networking capabilities:  Socket-based and packet-based communications • Package java. net  Remote Method Invocation (RMI) • Package java. rmi  Servlets and JavaServer Pages. Servlets and JavaServer Pages (JSP) • Request-response model • Packages javax.servlet – javax.servlet.http – javax.servlet.jsp  the Web tier of J2EE 2008 © Department of Information Systems - University

Ngày đăng: 07/08/2014, 05:20

Mục lục

  • Web Programming with Java

  • Outline

  • Introduction

  • Introduction (cont.)

  • Outline

  • A Servlet's Job

  • Why Build Web Pages Dynamically?

  • Outline

  • Servlet Basics

  • Servlet Architecture

  • Creating a Servlet

  • Processing Requests

  • Example

  • Outline

  • The Servlet Lifecycle

  • Why You Should Not Override service

  • Outline

  • Creating Form Data: HTML Forms

  • Reading Parameters

  • Reading Form Data

Tài liệu cùng người dùng

Tài liệu liên quan