`
AquariusM
  • 浏览: 143940 次
  • 性别: Icon_minigender_1
  • 来自: 南阳
社区版块
存档分类
最新评论

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.
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics