Servlet

Similar documents
(TestFailure) JUnit Framework AssertionFailedError JUnit Composite TestSuite Test TestSuite run() run() JUnit

D getinitparameternames() 9 下 列 选 项 中, 属 于 Servlet API 中 提 供 的 request 对 象 的 包 装 类 的 是 ( ) A HttpServletRequestWrapper B HttpServletRequest C HttpServ

1.JasperReport ireport JasperReport ireport JDK JDK JDK JDK ant ant...6

jsp

基于CDIO一体化理念的课程教学大纲设计

EJB-Programming-3.PDF

EJB-Programming-4-cn.doc

TopTest_Adminstrator.doc

Chapter 9: Objects and Classes

(CIP) Web /,. :, ISBN X.W T P393.4 CIP (2004) Web ( ) ( / ) : * 787

Microsoft Word - 01.DOC

Java

untitled

59 1 CSpace 2 CSpace CSpace URL CSpace 1 CSpace URL 2 Lucene 3 ID 4 ID Web 1. 2 CSpace LireSolr 3 LireSolr 3 Web LireSolr ID

epub83-1

weblogic

untitled

Learning Java

ebook

XXXXXXXX

untitled

Microsoft PowerPoint - 02-Servlet-Basics-Chinese.ppt

D C 93 2

基于ECO的UML模型驱动的数据库应用开发1.doc

RunPC2_.doc

resp.getwriter().print(j + "*" + i + "=" + j * i+" "); resp.getwriter().print("<br/>"); protected void dopost(httpservletrequest req, HttpServletRespo

epub 61-2

没 有 多 余 的 Contruol 或 Action 了 原 来 Domain 层 被 服 务 层 Service layer 遮 挡, 在 右 边 图 中, 则 Domain 层 直 接 暴 露 给 前 台 了, 没 有 被 遮 挡, 裸 露 了 这 样 一 步 到 位 实 现 领 域 模 型

JavaIO.PDF

优迈科技教学大纲2009版本

本章学习目标 小风 Java 实战系列教程 SpringMVC 简介 SpringMVC 的入门案例 SpringMVC 流程分析 配置注解映射器和适配器 注解的使用 使用不同方式的跳转页面 1. SpringMVC 简介 Spring web mvc

WebSphere Studio Application Developer IBM Portal Toolkit... 2/21 1. WebSphere Portal Portal WebSphere Application Server stopserver.bat -configfile..

雲端 Cloud Computing 技術指南 運算 應用 平台與架構 10/04/15 11:55:46 INFO 10/04/15 11:55:53 INFO 10/04/15 11:55:56 INFO 10/04/15 11:56:05 INFO 10/04/15 11:56:07 INFO

<ADB6ADB1C25EA8FAA6DB2D4D56432E706466>

mvc

untitled

目 录 1. 业 务 流 程 系 统 开 发 面 临 的 挑 战 与 机 遇 业 务 流 程 管 理 新 一 代 开 源 业 务 流 程 开 发 平 台 BPMX BPMX3 是 什 么 为 什 么 要 优 先 采 用 BPMX

untitled

untitled

2 Java 语 言 程 序 设 计 教 程 简 单 性 Java 语 言 的 语 法 与 C 语 言 和 C++ 语 言 很 接 近, 使 得 大 多 数 程 序 员 很 容 易 学 习 和 使 用 Java 另 一 方 面,Java 丢 弃 了 C++ 中 很 少 使 用 的 很 难

Fun Time (1) What happens in memory? 1 i n t i ; 2 s h o r t j ; 3 double k ; 4 char c = a ; 5 i = 3; j = 2; 6 k = i j ; H.-T. Lin (NTU CSIE) Referenc

SiteView技术白皮书

AN INTRODUCTION TO PHYSICAL COMPUTING USING ARDUINO, GRASSHOPPER, AND FIREFLY (CHINESE EDITION ) INTERACTIVE PROTOTYPING

untitled

chp6.ppt

2/14 Buffer I12, /* x=2, buffer = I 1 2 */ Buffer I243, /* x=34, buffer = I 2 43 */ x=56, buffer = I243 Buffer I243I265 code_int(int x, char *buffer)

IP505SM_manual_cn.doc

Microsoft Word - template.doc

Java Access 5-1 Server Client Client Server Server Client 5-2 DataInputStream Class java.io.datainptstream (extends) FilterInputStream InputStream Obj

<4D F736F F D D352DBED6D3F2CDF8D7E9BDA8D3EBB9DCC0EDCFEEC4BFBDCCB3CCD5FDCEC42E646F63>

ch_code_infoaccess

9, : Java 19., [4 ]. 3 Apla2Java Apla PAR,Apla2Java Apla Java.,Apla,,, 1. 1 Apla Apla A[J ] Get elem (set A) A J A B Intersection(set A,set B) A B A B

chapter 2 HTML5 目錄iii HTML HTML HTML HTML HTML canvas

詞 彙 表 編 號 詞 彙 描 述 1 預 約 人 資 料 中 文 姓 名 英 文 姓 名 身 份 證 字 號 預 約 人 電 話 性 別 2 付 款 資 料 信 用 卡 別 信 用 卡 號 信 用 卡 有 效 日 期 3 住 房 條 件 入 住 日 期 退 房 日 期 人 數 房 間 數 量 入

Microsoft Word - Learn Objective-C.doc

Logitech Wireless Combo MK45 English

(Microsoft Word - \272\364\263q\245|\244A_ _\304\254\253\330\336\263__\272\353\302\262\263\370\247i.doc)

JSP基础编程

概述

RUN_PC連載_8_.doc

RUN_PC連載_12_.doc

1. 2. Flex Adobe 3.

Java 1 Java String Date

Windows XP

Important Notice SUNPLUS TECHNOLOGY CO. reserves the right to change this documentation without prior notice. Information provided by SUNPLUS TECHNOLO

Chapter 9: Objects and Classes

提纲 1 2 OS Examples for 3

ArcGIS Sever.NET ArcGIS Server Web JAVA ArcGIS Server Web

KillTest 质量更高 服务更好 学习资料 半年免费更新服务

untitled

Java java.lang.math Java Java.util.Random : ArithmeticException int zero = 0; try { int i= 72 / zero ; }catch (ArithmeticException e ) { // } 0,

untitled

untitled

6-1 Table Column Data Type Row Record 1. DBMS 2. DBMS MySQL Microsoft Access SQL Server Oracle 3. ODBC SQL 1. Structured Query Language 2. IBM

Microsoft Word 样章.dot

epub 79-1

目录

PowerPoint 演示文稿

2 WF 1 T I P WF WF WF WF WF WF WF WF 2.1 WF WF WF WF WF WF

<4D F736F F D20BBF9D3DA416E64726F6964C6BDCCA8B5C4B5E7D7D3C5C4C2F4CFB5CDB32E646F63>

Microsoft Word - Functional_Notes_3.90_CN.doc

coverage2.ppt

RUN_PC連載_10_.doc

ebook70-13

多層次傳銷與獎金系統

Microsoft Word doc

Microsoft PowerPoint - 05-Status-Codes-Chinese.ppt

第一章 章标题-F2 上空24,下空24

1. 访 问 最 新 发 行 公 告 信 息 jconnect for JDBC 访 问 最 新 发 行 公 告 信 息 最 新 版 本 的 发 行 公 告 可 以 从 网 上 获 得 若 要 查 找 在 本 产 品 发 布 后 增 加 的 重 要 产 品 或 文 档 信 息, 请 访

Chn 116 Neh.d.01.nis

1

untitled

untitled

untitled

學 科 100% ( 為 單 複 選 題, 每 題 2.5 分, 共 100 分 ) 1. 請 參 閱 附 圖 作 答 : (A) 選 項 A (B) 選 項 B (C) 選 項 C (D) 選 項 D Ans:D 2. 下 列 對 於 資 料 庫 正 規 化 (Normalization) 的 敘

二 Synchronized 1. Java 语 言 的 关 键 字, 当 它 用 来 修 饰 一 个 方 法 或 者 一 个 代 码 块 的 时 候, 能 够 保 证 在 同 一 时 刻 最 多 只 有 一 个 线 程 执 行 该 段 代 码 2. 当 两 个 并 发 线 程 访 问 同 一 个

Microsoft PowerPoint - 10-JSP-Scripting-Elements-Chinese.ppt

科研信息化技术与应用 2015, 6 (6) 联网交换中心 AP 等国内主要互联网运营商实现高 [1] 速互联 截至目前 美国 英国 新加坡 巴西 新西 兰 中国香港等国家和地区的政府机构均开展了宽带 况 在网络出现拥塞时能够快速定位问题 解决故 障 有针对性地提升我们的网络质量 并随着服务 质量的

13 根 据 各 种 网 络 商 务 信 息 对 不 同 用 户 所 产 生 的 使 用 效 用, 网 络 商 务 信 息 大 致 可 分 为 四 级, 其 中 占 比 重 最 大 的 是 ( A ) A 第 一 级 免 费 信 息 B 第 二 级 低 收 费 信 息 C 第 三 级 标 准 收 费

Transcription:

Servlet Allen Long Email: allen@huihoo.com http://www.huihoo.com 2004-04 Huihoo - Enterprise Open Source http://www.huihoo.com 1

Huihoo - Enterprise Open Source http://www.huihoo.com 2

GET POST Huihoo - Enterprise Open Source http://www.huihoo.com 3

CGI FastCGI API (NSAPI ISAPI) ASP Servlet JSP Huihoo - Enterprise Open Source http://www.huihoo.com 4

Servlet Huihoo - Enterprise Open Source http://www.huihoo.com 5

Servlet Servlet Servlet Engine network Service, MIME request, Servlet Container Servlet Engine Servlet Servlet Engine Servlet Huihoo - Enterprise Open Source http://www.huihoo.com 6

Servlet Access Access Model Huihoo - Enterprise Open Source http://www.huihoo.com 7

Huihoo - Enterprise Open Source http://www.huihoo.com 8

Huihoo - Enterprise Open Source http://www.huihoo.com 9

Huihoo - Enterprise Open Source http://www.huihoo.com 10

Service Huihoo - Enterprise Open Source http://www.huihoo.com 11

Destroy Huihoo - Enterprise Open Source http://www.huihoo.com 12

Huihoo - Enterprise Open Source http://www.huihoo.com 13

Package javax.servlet.http Huihoo - Enterprise Open Source http://www.huihoo.com 14

HTTP Servlet Sample Extend HttpServlet The Service method is call by the servlet container for each request Huihoo - Enterprise Open Source http://www.huihoo.com 15

Sample Huihoo - Enterprise Open Source http://www.huihoo.com 16

HttpServletRequest CGI getparmeternames() HTML, getparmetervalues(string name) multi-valued getparameter(string name) Huihoo - Enterprise Open Source http://www.huihoo.com 17

HttpServletResponse servlet /...) URL Huihoo - Enterprise Open Source http://www.huihoo.com 18

Handling Requests Handling Requests Huihoo - Enterprise Open Source http://www.huihoo.com 19

ServletConfig Huihoo - Enterprise Open Source http://www.huihoo.com 20

An Example Huihoo - Enterprise Open Source http://www.huihoo.com 21

URLs A URL pointing to a Servlet can be split into portions: protocol, server, port Web Application name extra path information(servlet jsp html) the query string (containing parameters) Huihoo - Enterprise Open Source http://www.huihoo.com 22

Query Strings Use a query string to pass parameters from the browser to the web server. Query String Rules: Huihoo - Enterprise Open Source http://www.huihoo.com 23

Sample Huihoo - Enterprise Open Source http://www.huihoo.com 24

HTML Forms You can use HTML forms (instead of query strings) to send parameters to a servlet One parameter is passed for each form field HTML Form Syntax: Huihoo - Enterprise Open Source http://www.huihoo.com 25

Sample Huihoo - Enterprise Open Source http://www.huihoo.com 26

Response Structure A servlet uses an instance of HttpServletResponse to send data to the client. Huihoo - Enterprise Open Source http://www.huihoo.com 27

HttpServletResponse Huihoo - Enterprise Open Source http://www.huihoo.com 28

An Example Huihoo - Enterprise Open Source http://www.huihoo.com 29

Status/Error Messages The HttpServletResponse interface has many constants declared for status/errors: A Sampling of HTTP Constants: public static final SC_OK; // status code 200 public static final SC_NOT_FOUND; // status code 404 public static final SC_BAD_REQUEST; // status code 400 public static final SC_FORBIDDEN; // status code 403 These Values Can Be Used With: public void senderror(int); public void senderror(int, String); public void sendstatus(int); public void sendstatus(int, Stirng); Huihoo - Enterprise Open Source http://www.huihoo.com 30

HTTP Application (ServletContext) Session (HttpSession) Request () Page (JSP) Huihoo - Enterprise Open Source http://www.huihoo.com 31

Servlet Servlet Huihoo - Enterprise Open Source http://www.huihoo.com 32

public class SpecialSetter extends HttpServlet { public void doget(httpservletrequest req, HttpServletResponse res) throws ServletException, IOException { res.setcontenttype("text/plain"); PrintWriter out = res.getwriter(); ServletContext context = getservletcontext(); context.setattribute("com.costena.special.burrito", "Pollo Adobado"); context.setattribute("com.costena.special.day", new Date()); out.println("the burrito special has been set."); } } Huihoo - Enterprise Open Source http://www.huihoo.com 33

public class SpecialGetter extends HttpServlet { public void doget(httpservletrequest req, HttpServletResponse res) throws ServletException, IOException { res.setcontenttype("text/html"); PrintWriter out = res.getwriter(); ServletContext context = getservletcontext(); String burrito = (String) context.getattribute("com.costena.special.burrito"); Date day = (Date) context.getattribute("com.costena.special.day"); DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM); String today = df.format(day); out.println("our burrito special today (" + today + ") is: " + burrito); } } Huihoo - Enterprise Open Source http://www.huihoo.com 34

Session A session for a client is shared among multiple servlets. Huihoo - Enterprise Open Source http://www.huihoo.com 35

Web URL Cookie HttpSession Huihoo - Enterprise Open Source http://www.huihoo.com 36

The HttpSession object is the servlet s view of the session Session: public HttpSession getsession(boolean createnew); createnew: true session session false - session null Session: HttpSession session = request.getsession(true); Huihoo - Enterprise Open Source http://www.huihoo.com 37

HttpSession Huihoo - Enterprise Open Source http://www.huihoo.com 38

HttpSession public class SessionSnoop extends HttpServlet { public void doget(httpservletrequest req, HttpServletResponse res) throws ServletException, IOException { res.setcontenttype("text/html"); PrintWriter out = res.getwriter(); // Get the current session object, create one if necessary HttpSession session = req.getsession(); Integer count = (Integer)session.getAttribute("snoop.count"); if (count == null) count = new Integer(1); else count = new Integer(count.intValue() + 1); session.setattribute("snoop.count", count); out.println("<html><head><title>sessionsnoop</title></head>"); out.println("<body><h1>session Snoop</H1>"); out.println("<h3>here is your saved session data:</h3>"); Enumeration enum = session.getattributenames(); while (enum.hasmoreelements()) { String name = (String) enum.nextelement(); out.println(name + ": " + session.getattribute(name) + "<BR>"); } out.println("</body></html>"); } } Huihoo - Enterprise Open Source http://www.huihoo.com 39

Session web.xml <session-config> <session-timeout>30</session-timeout> </session-config> public void setmaxinactiveinterval(int interval) public int getmaxinactiveinterval() Huihoo - Enterprise Open Source http://www.huihoo.com 40

Servlets and Threads Huihoo - Enterprise Open Source http://www.huihoo.com 41

A servlet must be able to handle multiple, concurrent requests. Huihoo - Enterprise Open Source http://www.huihoo.com 42

Servlet Synchronized singelthreadmodel Huihoo - Enterprise Open Source http://www.huihoo.com 43

Huihoo - Enterprise Open Source http://www.huihoo.com 44

public class SimpleCounter extends HttpServlet { int count = 0; public void doget(httpservletrequest req, HttpServletResponse res) throws ServletException, IOException { res.setcontenttype("text/plain"); PrintWriter out = res.getwriter(); count++; out.println("since loading, this servlet has been accessed " + count + " times."); } } Huihoo - Enterprise Open Source http://www.huihoo.com 45

(block) Synchronized Block synchronized (AnyObject) { } The thread that can obtain AnyObject s mutex flag gets to enter the block Huihoo - Enterprise Open Source http://www.huihoo.com 46

Import java.io.*; Import javax.servlet.*; Import javax.servlet.http.*; Public class MultipleInstanceServlet extends HttpServlet { Object lock = null; public void init(servletconfig cg) { lock = new Object(); } Public void service(httpservletrequest request, HttpServletResponse response) throws IOExceptions, ServletException { synchronized(lock) { } // reqular code } } Huihoo - Enterprise Open Source http://www.huihoo.com 47

JBuilder Servlet Creating the project Creating the WebApp Creating the Servlet with the Servlet wizard Adding code to the Servlet Compiling and running the Servlet Huihoo - Enterprise Open Source http://www.huihoo.com 48

Create WebApp Huihoo - Enterprise Open Source http://www.huihoo.com 49

Creating the servlet with the Servlet wizard-1 Huihoo - Enterprise Open Source http://www.huihoo.com 50

Creating the servlet with the Servlet wizard-2 Huihoo - Enterprise Open Source http://www.huihoo.com 51

Creating the servlet with the Servlet wizard-3 Huihoo - Enterprise Open Source http://www.huihoo.com 52

Web Application Huihoo - Enterprise Open Source http://www.huihoo.com 53

Web Server Setup Huihoo - Enterprise Open Source http://www.huihoo.com 54

Tomcat Tomcat jakarta JavaWorld 2001 java (Most Innovative Java Product) sun servlet jsp ( http://java.sun.com/products/jsp/tomcat/) servlet jsp tomcat Huihoo - Enterprise Open Source http://www.huihoo.com 55

Web Application Archive (WAR) HTML JSP Java class Java jar Web (web.xml) Huihoo - Enterprise Open Source http://www.huihoo.com 56

WAR Huihoo - Enterprise Open Source http://www.huihoo.com 57

ServletConfig web.xml servlet Huihoo - Enterprise Open Source http://www.huihoo.com 58

Servlet Mapping Huihoo - Enterprise Open Source http://www.huihoo.com 59

Servlet Mapping <servlet> <servlet-name>hi</servlet-name> <servlet-class>helloworld</servlet-class> </servlet> <servlet-mapping> <servlet-name> hi</servlet-name> <url-pattern> /hello.html</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name> hi</servlet-name> <url-pattern> *.hello</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name> hi</servlet-name> <url-pattern> /hello/*</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name> hi</servlet-name> <url-pattern> /hello.html</url-pattern> </servlet-mapping> Huihoo - Enterprise Open Source http://www.huihoo.com 60

Web Huihoo - Enterprise Open Source http://www.huihoo.com 61

Servlet Huihoo - Enterprise Open Source http://www.huihoo.com 62

Huihoo - Enterprise Open Source http://www.huihoo.com 63

weblogic.xml Huihoo - Enterprise Open Source http://www.huihoo.com 64

Application Event Listener Servlet Event listener are classes that respond to notifications about ServletContext HttpSession Huihoo - Enterprise Open Source http://www.huihoo.com 65

Listener ServletContextListener ServletContextAttributeListener, HttpSessionListener HttpSessionAttributeListener Huihoo - Enterprise Open Source http://www.huihoo.com 66

Event ServletContextEvent ServletContextAttributeEvent HttpSessionEvent HttpSessionBindingEvent Huihoo - Enterprise Open Source http://www.huihoo.com 67

Sample Huihoo - Enterprise Open Source http://www.huihoo.com 68

<listener> <listener-class>app.listener</listener-class> </listener> Huihoo - Enterprise Open Source http://www.huihoo.com 69

GOF Observer( ) Huihoo - Enterprise Open Source http://www.huihoo.com 70

Servlet Filter : Log Huihoo - Enterprise Open Source http://www.huihoo.com 71

Servlet Filter Change request Change response Huihoo - Enterprise Open Source http://www.huihoo.com 72

Develop Filter Huihoo - Enterprise Open Source http://www.huihoo.com 73

Sample Huihoo - Enterprise Open Source http://www.huihoo.com 74

<filter> <filter-name>servlet3</filter-name> <filter-class>untitled7.servlet3</filter-class> <init-param> <param-name>param</param-name> <param-value /> </init-param> </filter> <filter-mapping> <filter-name>servlet3</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> Huihoo - Enterprise Open Source http://www.huihoo.com 75

Filter Chain Order Filter execute in the order they are define in the web.xml file <filter> <filter-name>authfilter</filter-name> <filter-class>untitled7.authfilter</filter-class> </filter> <filter> <filter-name>logfilter</filter-name> <filter-class>untitled7.logfilter</filter-class> </filter> <filter> <filter-name>servlet3</filter-name> <filter-class>untitled7.servlet3</filter-class> <filter> Huihoo - Enterprise Open Source http://www.huihoo.com 76

SA Huihoo - Enterprise Open Source http://www.huihoo.com 77

SA Huihoo - Enterprise Open Source http://www.huihoo.com 78

Huihoo - Enterprise Open Source http://www.huihoo.com 79

Huihoo - Enterprise Open Source http://www.huihoo.com 80

Sample public class HelloWorldGraphics extends HttpServlet { public void doget(httpservletrequest req, HttpServletResponse res) throws ServletException, IOException { ServletOutputStream out = res.getoutputstream(); // binary output! Frame frame = null; Graphics g = null; try { // Create an unshown frame frame = new Frame(); frame.addnotify(); // Get a graphics region, using the Frame Image image = frame.createimage(400, 60); g = image.getgraphics(); Huihoo - Enterprise Open Source http://www.huihoo.com 81

Sample // Draw "Hello World!" to the off-screen graphics context g.setfont(new Font("Serif", Font.ITALIC, 48)); g.drawstring("hello World!", 10, 50); // Encode the off-screen image into a GIF and send it to the client res.setcontenttype("image/gif"); GifEncoder encoder = new GifEncoder(image, out); encoder.encode(); } finally { // Clean up resources if (g!= null) g.dispose(); if (frame!= null) frame.removenotify(); } } } Huihoo - Enterprise Open Source http://www.huihoo.com 82

Servlet SVG Servlet XML Huihoo - Enterprise Open Source http://www.huihoo.com 83

http://java.sun.com/products/servlet/ sun servlet http://www.huihoo.com Huihoo - Enterprise Open Source http://www.huihoo.com 84

allen@huihoo.com http://www.huihoo.com Huihoo - Enterprise Open Source http://www.huihoo.com 85