2010年9月28號---JavaEE學習之Servlet筆記

 

Distributed Multitiered Applications

The Java EE platform uses a distributed mutitiered application model for 

enterprise applications. Application logic is divided into components according to function, 

and the various applications components that make up a Java EE application are installed 

on different machines depending on the tier in the mutitiered Java EE environment to which 

the application component belongs.

 

Applets

A web page received from the web tier can include an embedded applet. An applet is a small client application written in

 the Java programming language that executes in the Java virtual machine installed in the web browser. However,

 client systems will likely need the Java Plug-in and possibly a security policy file for the applet to successfully execute in the web browser. 

 

web層獲取的一個web頁面可以包含一個嵌入的Applet。一個applet 是一個小的由web瀏覽器創建運行在JVM中使用java編寫的一個客戶端應用程序。同時,客戶端系統需要一個Java插件或者是一個策略文件才能夠在web瀏覽器上成功運行。

Web components are the preferred API for creating a web client program because no plug-ins or security policy files are needed on the client systems. Also, web components enable cleaner and more modular 

application design because they provide a way to separate applications programming from web page design.

 Personnel involved in web page design thus do not need to understand Java programming language syntax to do their

 jobs. 

對於建立web客戶端應用程序來說Web組件應該是首選,因爲用web組件來建立一個客戶端應用程序是不需要插件或者策略文件的。Web組件使更加簡介和模塊化的應用程程序成爲可能,因爲它提供了一個將應用程序同web網頁設計分離的技術途徑。在web頁面設計的個人應用開發設計並不需要深入理解Java應用程序設計的語法就能完成他們的工作。

 

Chapter 4

Java Servlet Technology

As soon as the web began to be used for delivering services, service providers recognized the need for dynamic content

Applets, one of the earliest attempts toward this goal, focused on using the client platform to deliver dynamic user 

experiences. At the same time, developers also investigated using the server platform for this purpose. Initially, 

Common Gateway Interface (CGI) scripts were the main technology used to generate dynamic content. 

Although widely used, CGI scripting technology has a number of shortcomings, including platform dependence and 

lack of scalability. To address these limitations, Java Servlet technology was created as a portable way to provide

 dynamic, user-oriented content. 

隨着web技術開始應用到程序服務的傳遞,服務被提供來辨別動態內容提出的請求。Applets,作爲最早的一個朝着這個目標的嘗試,致力於運用客戶端平臺來傳遞動態的用戶處理。與此同時,開發者也在嘗試着使用服務平臺來實現這一目標。開始時,CGI(Common Gateway Interface)腳本作爲核心技術應用在一般的靜態內容請求上。儘管CGI得到了廣泛的應用,但是CGI腳本技術還是有很多的不足之處,包括平臺依賴性和可擴展性方面的缺陷。針對這些不足,開發者創建了Java Servlet技術作爲一個合適的途徑來提供動態支持。

 

 

 

What Is a Servlet?

servlet is a Java programming language class that is used to extend the capabilities of servers that host 

applications accessed by means of a request-response programming model. Although servlets can respond to any type of request, they are commonly 

used to extend the applications hosted by web servers. For such applications, Java Servlet technology defines

 HTTP-specific servlet classes.

一個servlet是一個Java語言編寫的類用來擴展主機應用程序訪問的能力基於請求響應模型的能力。儘管servlet可以響應客戶端的任何類型的請求,他們被經常被web服務器用來擴展應用程序。對於這樣的應用程序,Java Servlet技術定義了Http指定的servlet類。

 

 

以下六點轉載自:
http://blog.sina.com.cn/s/blog_3fc4c0a80100kuhi.html

 

一:Servlet是一種獨立於操作系統和網絡傳輸協議的服務端的Java應用程序,它用來控制服務器的功能,可以生成動態的Web頁面。注意:Servlet不是命令啓動的,而是由包含Java虛擬機的Web服務器進行加載。

 

二:ServletApplet相比較:

相似處:

(1):他們都不是獨立的應用程序,沒有main()方法不能獨立運行。

(2):他們不是由用戶調用,而是由另一個應用程序(容器)調用。

(3):他們都有一個生命週期,包含init()和destory()方法。

 不同之處:

 (1):Applet運行於客戶端,具有豐富的圖形界面

 (2):Servlet運行於服務端,沒有圖形界面

 

三:Servlet最大的用途是:通過動態響應客戶機請求來擴充服務器功能。

 

四:Servlet運行於web服務器上的web容器裏。Web容器負責管理Servlet。它裝入並初始化Srevlet,管理Servlet的多個實例,並充當請求調度器,將客戶端的請求傳遞給服務器,並將服務器的響應反饋給客戶端,web容器在servlet的使用期限結束時將終結Servlet。服務器關閉時,Web容器會從內存中卸載和移除Servlet

 

五:Servlet基本工作流程

1):客戶機將請求發送給服務器

2):服務器上的Web容器實例化Servlet

3):Web容器將請求信息發送到Servlet

4):Servlet創建一個響應,並將其返回到Web容器

5):Web容器將響應發回客戶機

6):服務器關閉或Servlet空閒時間超過一定限度時,調用destory()方法退出

六:結論

客戶機與Servlet間沒有直接的交互。無論客戶機對Servlet的請求還是Srevlet對客戶端的響應,都是通過Web服務器來實現的。

 

 

 

 

javax.servlet

Interface ServletContext

Defines a set of methods that a servlet uses to communicate with 

its servlet container, for example, to get the MIME type of a file, 

dispatch request, or write to a log file.

定義一系列servlet用來與servlet容器通信的方法。例如,獲得文件類型、發送請求、或者想日誌文件中寫入記錄。

There is one context per "web application" per Java Virtual Machine. (A "web application" is a collection of servlets and content installed under a specific 

subset of the server's URL namespace such as /catalog and possibly installed via a .war file. )

In the case of a web application marked "distributed" in this deployment descriptor, 

there will be one context instance for each virtual machine. 

In this solution, the context cannot be used as a location to share global information 

(because the information won't be turely global ). 

Use an external resource like a database instead.

每一個Java虛擬機的web應用程程序都會有一個上下文的(一個web應用程序是指一個servlet的集合和在服務端的URL命名空間下的一個特殊的子集,例如:/catalog或者通過war形式發佈的一個包)都有一個上下文。而對於在項目部署描述文件中被標記爲“distributed”的web應用程序,每一個虛擬機將會只有上下文。在這種途徑下上下文並不能夠作爲一個分享全局信息的所在地(因爲這些信息將不會真正的國際化)。使用一個外部的像數據庫一樣的源。

The ServletContext object is conatined within the ServletConfig object, 

which the Web server provides the servlet when the servlet is initialized.

 

javax.servlet

Interface Servlet

public interface Servlet

Defines methods that all servlets must implement.

A servlet is a small Java program that runs within a Web server. Servlets receive and respond to requests from 

Web clients, usually across HTTP, the HyperText Transfer Protocol.

 

Servlet 通過HTTP超文本文件傳輸協議傳輸信息。

 

To implement this interface, you can write a generic servlet that extends javax.servlet.GenericServlet or an HTTP 

servlet that extends javax.servlet.http.HttpServlet.

This interface defines methods to initialize a servlet, to service requests, and to remove a servlet from the server. 

These are known as life-cycle methods and are called in the following sequence:

The servlet is constructed, then initialized with the init method.

Any calls from clients to the service method are handled.

The servlet is taken out of service, then destroyed with the destroy method, then garbage collected and finalized.

In addition to the life-cycle methods, this interface provides the getServletConfig method, which the servlet can use to get any startup

 information, and the getServletInfo method, which allows the servlet to return basic information about itself, 

such as author, version, and copyright.

s

 

Servlet小例子:

Java類編寫:

package jsp.test;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.io.IOException;
public class FirstServlet extends HttpServlet {
private PrintWriter out;
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException
{
request.setCharacterEncoding("gbk");
response.setContentType("text/html; charset=gbk");
out=response.getWriter();
out.print("這是我的第一個Servlet!");
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException
{
doPost(request,response);
}
}
 

 

 

Web.xml寫法:

<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<servlet>
<servlet-name>firstServlet</servlet-name>
<servlet-class>
jsp.test.FirstServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>firstServlet</servlet-name>
<url-pattern>/FIRST</url-pattern>
</servlet-mapping>
</web-app>
 

 

發佈時,注意Servlet類的位置和包的對應

該例子參考:《Jsp+Oracle數據庫開發與實例》 張曉東 高鑑偉 編著

後續添加實例:
實例地址  http://jyjiao2007.iteye.com/blog/761698

該實例顯示瞭如何新建ServletContext並且使用使用ServletContext進行操作:

java代碼部分:

 

package mypack;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CounterServlet extends HttpServlet{
	
	public static final String CONNECT_TYPE = "text/html;charset=GB2312";

	/**
	 * @purpose to manage resources that are held for the life of the servlet
	 * @param ServletConfig
	 */
	public void init(ServletConfig config)throws ServletException{
		super.init(config);
	}
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
		doPost(request, response);
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
		ServletContext context = getServletContext();
		Integer count = (Integer)context.getAttribute("count");
		
		if(count == null){
			count = new Integer(0);
			context.setAttribute("count", new Integer(0));
		}
		
		response.setContentType(CONNECT_TYPE);
		PrintWriter out = response.getWriter();
		out.println("<HTML>");
		out.println("<HEAD><TITLE>點擊計數</HEAD></TITLE>");
		out.println("<BODY>");
		
		out.println("<P>當前計數是:" + count + "</P>");
		out.println("</BODY></HTML>");
		
		count = new Integer(count.intValue());
		context.setAttribute("count", count);
	}
	public void destroy(){
		
	}
}

 web.xml:

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app>
   <servlet>
       <servlet-name>CounterServlet</servlet-name>
       <servlet-class>mypack.CounterServlet</servlet-class>
   </servlet>
   <servlet-mapping>
       <servlet-name>CounterServlet</servlet-name>
       <url-pattern>/counter</url-pattern>
   </servlet-mapping>
</web-app>
 關於xml部分注意url-pattern中要加入"/"。

補充:
Interface ServletConfig:

public interface ServeltConfig
Annotation: A servlet configuration object used by a servlet container to pass information to a servlet during initialization.
發佈了62 篇原創文章 · 獲贊 2 · 訪問量 3418
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章