javax.servlet.http.HttpServlet翻译

<!-- ========= START OF TOP NAVBAR ======= --> <!-- -->
JavaTM 2 Platform
Ent. Ed. v1.4
<!-- ========= END OF TOP NAVBAR ========= --> <!-- ======== START OF CLASS DATA ======== -->

javax.servlet.http
Class HttpServlet



All Implemented Interfaces:
Serializable , Servlet , ServletConfig
public abstract class HttpServlet extends GenericServlet implements Serializable

Provides an abstract class to be subclassed to create an HTTP servlet suitable for a Web site. A subclass of HttpServlet must override at least one method, usually one of these: 提供一个抽象类使子类可以创建一个适合Web站点的HTTP servlet。HttpServlet的子类至少应该覆盖下面列出的方法中的一个:

  • doGet, if the servlet supports HTTP GET requests servlet支持HTTP GET请求
  • doPost, for HTTP POST requests 支持HTTP POST请求
  • doPut, for HTTP PUT requests 支持HTTP PUT请求
  • doDelete, for HTTP DELETE requests 支持HTTP DELETE请求
  • init and destroy, to manage resources that are held for the life of the servlet 管理servlet生命周期中保持的资源
  • getServletInfo, which the servlet uses to provide information about itself servlet用来提供自身的信息

There's almost no reason to override the service method. service handles standard HTTP requests by dispatching them to the handler methods for each HTTP request type (the doXXX methods listed above). 没有理由需要重写service方法。service处理标准HTTP请求,将每种HTTP请求类型转发至不同的处理方法(上面列出的doXXXmethods)

Likewise, there's almost no reason to override the doOptions and doTrace methods. 同样,也没有理由重写doOptions和doTrace方法。

Servlets typically run on multithreaded servers, so be aware that a servlet must handle concurrent requests and be careful to synchronize access to shared resources. Shared resources include in-memory data such as instance or class variables and external objects such as files, database connections, and network connections. See the Java Tutorial on Multithreaded Programming for more information on handling multiple threads in a Java program. servlet通常运行在多线程的服务器上,因此servlet必须处理并发请求,小心地同步化访问共享资源。 共享资源包括内存中的数据比如实例或类变量和外部对象比如文件、数据库连接以及网络连接。 关于Java程序中处理多线程的更多详情参见Java Tutorial on Multithreaded Programming。

Version:
$Version$
Author:
Various
See Also:
Serialized Form

<!-- ======== NESTED CLASS SUMMARY ======== --><!-- =========== FIELD SUMMARY =========== --><!-- ======== CONSTRUCTOR SUMMARY ======== --><!-- -->

Constructor Summary
HttpServlet()
Does nothing, because this is an abstract class. 不做任何事,因为这是一个抽象类。
<!-- ========== METHOD SUMMARY =========== --> <!-- -->
Method Summary
protected void doDelete(HttpServletRequestreq, HttpServletResponseresp)
Called by the server (via the service method) to allow a servlet to handle a DELETE request. 由服务器service方法通过调用来允许servlet处理DELETE请求。
protected void doGet(HttpServletRequestreq, HttpServletResponseresp)
Called by the server (via the service method) to allow a servlet to handle a GET request. 由服务器service方法通过调用来允许servlet处理GET请求。
protected void doHead(HttpServletRequestreq, HttpServletResponseresp)
Receives an HTTP HEAD request from the protected service method and handles the request. 从受保护的service方法接收HTTP HEAD请求,并且处理该请求。
protected void doOptions(HttpServletRequestreq, HttpServletResponseresp)
Called by the server (via the service method) to allow a servlet to handle a OPTIONS request. 由服务器service方法通过调用来允许servlet处理OPTIONS请求。
protected void doPost(HttpServletRequestreq, HttpServletResponseresp)
Called by the server (via the service method) to allow a servlet to handle a POST request. 由服务器service方法通过调用来允许servlet处理POST请求。
protected void doPut(HttpServletRequestreq, HttpServletResponseresp)
Called by the server (via the service method) to allow a servlet to handle a PUT request. 由服务器service方法通过调用来允许servlet处理PUT请求。
protected void doTrace(HttpServletRequestreq, HttpServletResponseresp)
Called by the server (via the service method) to allow a servlet to handle a TRACE request. 由服务器service方法通过调用来允许servlet处理TRACE请求。
protected long getLastModified(HttpServletRequestreq)
Returns the time the HttpServletRequest object was last modified, in milliseconds since midnight January 1, 1970 GMT. 返回HttpServletRequest对象上次修改的时间,记为从GMT1970年1月1日零时以来的毫秒数。
protected void service(HttpServletRequestreq, HttpServletResponseresp)
Receives standard HTTP requests from the public service method and dispatches them to the doXXX methods defined in this class. 从公开的service方法接收标准HTTP请求,转发至类中定义的doXXX方法。
void service(ServletRequestreq, ServletResponseres)
Dispatches client requests to the protected service method. 转发客户端请求至受保护的service方法。
<!-- -->
Methods inherited from class javax.servlet.GenericServlet
destroy, getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletInfo, getServletName, init, init, log, log
<!-- -->
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

<!-- ============ FIELD DETAIL =========== --><!-- ========= CONSTRUCTOR DETAIL ======== --><!-- -->

Constructor Detail
<!-- -->

HttpServlet

 
Does nothing, because this is an abstract class. 不做任何事,因为这是一个抽象类。

<!-- ============ METHOD DETAIL ========== --> <!-- -->
Method Detail
<!-- -->

doGet

Called by the server (via the service method) to allow a servlet to handle a GET request. 由服务器service方法通过调用来允许servlet处理GET请求。

Overriding this method to support a GET request also automatically supports an HTTP HEAD request. A HEAD request is a GET request that returns no body in the response, only the request header fields. 重写方法支持GET请求也会自动支持HTTP HEAD请求。HEAD请求是一个不返回响应体的GET请求,只包含请求头字段。

When overriding this method, read the request data, write the response headers, get the response's writer or output stream object, and finally, write the response data. It's best to include content type and encoding. When using a PrintWriter object to return the response, set the content type before accessing the PrintWriter object. 当重写该方法时,读取请求数据,写入请求头,获取请求writer或者输出流对象,最后,写响应数据。最好包括内容类型和编码。当使用PrintWriter对象返回响应时,在方法PrintWriter对象之前设置内容类型。

The servlet container must write the headers before committing the response, because in HTTP the headers must be sent before the response body. servlet容器必须在提交响应前写入头,因为HTTP中在响应体前必须发送头。

Where possible, set the Content-Length header (with the ServletResponse.setContentLength(int) method), to allow the servlet container to use a persistent connection to return its response to the client, improving performance. The content length is automatically set if the entire response fits inside the response buffer. 有可能的话,应通过ServletResponse.setContentLength(int)方法设置Content-Length头,以允许servlet容器使用持久化的连接来返回对客户端的响应,这样可以改善性能。如果整个响应匹配请求缓存会自动设置内容长度。

When using HTTP 1.1 chunked encoding (which means that the response has a Transfer-Encoding header), do not set the Content-Length header. 当使用HTTP 1.1编码(意味着响应有一个编码转换头)时,不用设置Content-Length头。

The GET method should be safe, that is, without any side effects for which users are held responsible. For example, most form queries have no side effects. If a client request is intended to change stored data, the request should use some other HTTP method. GET方法应当是安全的,也就是对于负责的用户不会有边界效应。比如,大多数的查询没有边界效应。如果客户端请求想要修改保存的数据,请求应该使用其他的HTTP方法。

The GET method should also be idempotent, meaning that it can be safely repeated. Sometimes making a method safe also makes it idempotent. For example, repeating queries is both safe and idempotent, but buying a product online or modifying data is neither safe nor idempotent. GET方法应该是等幂的,意味着它可以安全地重复。有时使一个方法安全也就是使它等幂。比如。重复查询既是安全也是等幂的,但是在线购买商品或者修改数据则既不安全也不等幂。

If the request is incorrectly formatted, doGet returns an HTTP "Bad Request" message. 如果该请求格式不正确,doGet返回一个HTTP“错误请求”信息。

Parameters:
req - an HttpServletRequest object that contains the request the client has made of the servlet HttpServletResponse对象,包含客户端向servlet发出的请求
resp - an HttpServletResponse object that contains the response the servlet sends to the client HttpServletResponse对象,包含servlet向客户端返回的响应
Throws:
IOException - if an input or output error is detected when the servlet handles the GET request 如果servlet处理GET请求时侦测到输入输出错误时抛出
ServletException - if the request for the GET could not be handled 如果GET请求不能被处理则抛出
See Also:
ServletResponse.setContentType(java.lang.String)
<!-- -->

getLastModified

Returns the time the HttpServletRequest object was last modified, in milliseconds since midnight January 1, 1970 GMT. If the time is unknown, this method returns a negative number (the default). 返回HttpServletRequest对象上次修改的时间,记为从GMT1970年1月1日零时以来的毫秒数。

Servlets that support HTTP GET requests and can quickly determine their last modification time should override this method. This makes browser and proxy caches work more effectively, reducing the load on server and network resources. 应该重写该方法使servlet支持HTTP GET请求,可以迅速确定上次修改时间。 这使浏览器和代理cache工作更有效,减少服务器和网络资源的加载。

Parameters:
req - the HttpServletRequest object that is sent to the servlet 发送给servlet的HttpServletRequest对象
Returns:
a long integer specifying the time the HttpServletRequest object was last modified, in milliseconds since midnight, January 1, 1970 GMT, or -1 if the time is not known 一个long型整数,指定HttpServletRequest对象上次修改的时间, 记为从GMT1970年1月1日零时以来的毫秒数,如果未知返回-1。
<!-- -->

doHead

Receives an HTTP HEAD request from the protected service method and handles the request. The client sends a HEAD request when it wants to see only the headers of a response, such as Content-Type or Content-Length. The HTTP HEAD method counts the output bytes in the response to set the Content-Length header accurately. 从受保护的service方法接收HTTP HEAD请求,并且处理该请求。 当客户端想查看请求头,比如 Content-Type和Content-Length时,发动一个HEAD请求。HTTP HEAD方法统计响应中的输出字节大小,来精确设置Content-Length头。

If you override this method, you can avoid computing the response body and just set the response headers directly to improve performance. Make sure that the doHead method you write is both safe and idempotent (that is, protects itself from being called multiple times for one HTTP HEAD request). 如果你重写了该方法,你可以避免计算请求体,直接设置请求头以提高性能。确保你写的doHead方法是安全和等幂的(也就是说,保证自己不被一个HTTP HEAD请求多次调用。)

If the HTTP HEAD request is incorrectly formatted, doHead returns an HTTP "Bad Request" message. 如果HTTP HEAD请求格式不正确,doHead返回一个HTTP“错误请求”信息。

Parameters:
req - the request object that is passed to the servlet HttpServletResponse对象,包含客户端向servlet发出的请求
resp - the response object that the servlet uses to return the headers to the client HttpServletResponse对象,包含servlet向客户端返回的响应
Throws:
IOException - if an input or output error occurs 如果发生输入输出错误时抛出
ServletException - if the request for the HEAD could not be handled 如果HEAD请求不能被处理则抛出
<!-- -->

doPost

Called by the server (via the service method) to allow a servlet to handle a POST request. The HTTP POST method allows the client to send data of unlimited length to the Web server a single time and is useful when posting information such as credit card numbers. 由服务器service方法通过调用来允许servlet处理POST请求。 HTTP POST方法允许客户端向Web服务器一次发送无限长度的数据,对于提交比如信用卡号码信息是有用的。

When overriding this method, read the request data, write the response headers, get the response's writer or output stream object, and finally, write the response data. It's best to include content type and encoding. When using a PrintWriter object to return the response, set the content type before accessing the PrintWriter object. 当重写该方法时,读取请求数据,写入请求头,获取请求writer或者输出流对象,最后,写响应数据。最好包括内容类型和编码。当使用PrintWriter对象返回响应时,在方法PrintWriter对象之前设置内容类型。

The servlet container must write the headers before committing the response, because in HTTP the headers must be sent before the response body. servlet容器必须在提交响应前写入头,因为HTTP中在响应体前必须发送头。

Where possible, set the Content-Length header (with the ServletResponse.setContentLength(int) method), to allow the servlet container to use a persistent connection to return its response to the client, improving performance. The content length is automatically set if the entire response fits inside the response buffer. 有可能的话,应通过ServletResponse.setContentLength(int)方法设置Content-Length头,以允许servlet容器使用持久化的连接来返回对客户端的响应,这样可以改善性能。如果整个响应匹配请求缓存会自动设置内容长度。

When using HTTP 1.1 chunked encoding (which means that the response has a Transfer-Encoding header), do not set the Content-Length header. 当使用HTTP 1.1编码(意味着响应有一个编码转换头)时,不用设置Content-Length头。

This method does not need to be either safe or idempotent. Operations requested through POST can have side effects for which the user can be held accountable, for example, updating stored data or buying items online. 该方法不需要安全和等幂。通过POST的请求操作对于保持账号的用户有副作用,比如更新保存的数据或在线购物。

If the HTTP POST request is incorrectly formatted, doPost returns an HTTP "Bad Request" message. 如果HTTP POST请求格式不正确,doPost返回一个HTTP“错误请求”信息。

Parameters:
req - an HttpServletRequest object that contains the request the client has made of the servlet HttpServletResponse对象,包含客户端向servlet发出的请求
resp - an HttpServletResponse object that contains the response the servlet sends to the client HttpServletResponse对象,包含servlet向客户端返回的响应
Throws:
IOException - if an input or output error is detected when the servlet handles the request 如果servlet处理该请求时侦测到输入输出错误时抛出
ServletException - if the request for the POST could not be handled 如果POST请求不能被处理则抛出
See Also:
ServletOutputStream , ServletResponse.setContentType(java.lang.String)
<!-- -->

doPut

Called by the server (via the service method) to allow a servlet to handle a PUT request. The PUT operation allows a client to place a file on the server and is similar to sending a file by FTP. 由服务器service方法通过调用来允许servlet处理PUT请求。 PUT操作允许客户端向服务器放置文件,类似地通过FTP发送文件。

When overriding this method, leave intact any content headers sent with the request (including Content-Length, Content-Type, Content-Transfer-Encoding, Content-Encoding, Content-Base, Content-Language, Content-Location, Content-MD5, and Content-Range). If your method cannot handle a content header, it must issue an error message (HTTP 501 - Not Implemented) and discard the request. For more information on HTTP 1.1, see RFC 2616 . 当重写该方法时,保留完整的发送请求的任何内容头(包括 Content-Length、Content-Type、 Content-Transfer-Encoding、Content-Encoding、Content-Base、Content-Language、Content-Location、 Content-MD5和Content-Range)。如果你的方法不能处理内容头,必须发布错误信息(HTTP 501 - Not Implemented), 丢弃该请求。关于HTTP 1.1得更多详情,参见RFC 2616。

This method does not need to be either safe or idempotent. Operations that doPut performs can have side effects for which the user can be held accountable. When using this method, it may be useful to save a copy of the affected URL in temporary storage. 该方法不需要安全和等幂。doPut执行的操作对于保持账号的用户有副作用。因此使用该方法时,临时保存一份受影响的URL的拷贝是有用的。

If the HTTP PUT request is incorrectly formatted, doPut returns an HTTP "Bad Request" message. 如果HTTP PUT请求格式不正确,doPut返回一个HTTP“错误请求”信息。

Parameters:
req - the HttpServletRequest object that contains the request the client made of the servlet HttpServletResponse对象,包含客户端向servlet发出的请求
resp - the HttpServletResponse object that contains the response the servlet returns to the client HttpServletResponse对象,包含servlet向客户端返回的响应
Throws:
IOException - if an input or output error occurs while the servlet is handling the PUT request 如果servlet处理该请求时发生输入输出错误时抛出
ServletException - if the request for the PUT cannot be handled 如果PUT请求不能被处理则抛出
<!-- -->

doDelete

Called by the server (via the service method) to allow a servlet to handle a DELETE request. The DELETE operation allows a client to remove a document or Web page from the server. 由服务器service方法通过调用来允许servlet处理DELETE请求。 DELETE操作允许客户端删除服务器上的文档或Web页面。

This method does not need to be either safe or idempotent. Operations requested through DELETE can have side effects for which users can be held accountable. When using this method, it may be useful to save a copy of the affected URL in temporary storage. 该方法不需要安全或者等幂。通过DELETE的请求操作对于保持账号的用户有副作用。因此使用该方法时,临时保存一份受影响的URL的拷贝是有用的。

If the HTTP DELETE request is incorrectly formatted, doDelete returns an HTTP "Bad Request" message. 如果HTTP DELETE请求格式不正确,doDelete返回一个HTTP“错误请求”信息。

Parameters:
req - the HttpServletRequest object that contains the request the client made of the servlet HttpServletResponse对象,包含客户端向servlet发出的请求
resp - the HttpServletResponse object that contains the response the servlet returns to the client HttpServletResponse对象,包含servlet向客户端返回的响应
Throws:
IOException - if an input or output error occurs while the servlet is handling the DELETE request 如果servlet处理DELETE请求时发生输入输出错误时抛出
ServletException - if the request for the DELETE cannot be handled 如果DELETE请求不能被处理则抛出
<!-- -->

doOptions

Called by the server (via the service method) to allow a servlet to handle a OPTIONS request. The OPTIONS request determines which HTTP methods the server supports and returns an appropriate header. For example, if a servlet overrides doGet, this method returns the following header: 由服务器service方法通过调用来允许servlet处理OPTIONS请求。 OPTIONS请求确定服务器支持哪一个HTTP方法,返回一个合适的头。例如,如果servlet重写doGet,方法按以下形式返回头:

Allow: GET, HEAD, TRACE, OPTIONS

There's no need to override this method unless the servlet implements new HTTP methods, beyond those implemented by HTTP 1.1. 不需要重写该方法除非servlet实现了新的HTTP方法,超出了HTTP 1.1的实现。

Parameters:
req - the HttpServletRequest object that contains the request the client made of the servlet HttpServletResponse对象,包含客户端向servlet发出的请求
resp - the HttpServletResponse object that contains the response the servlet returns to the client HttpServletResponse对象,包含servlet向客户端返回的响应
Throws:
IOException - if an input or output error occurs while the servlet is handling the OPTIONS request 如果servlet处理OPTIONS请求时发生输入输出错误时抛出
ServletException - if the request for the OPTIONS cannot be handled 如果OPTIONS请求不能被处理则抛出
<!-- -->

doTrace

Called by the server (via the service method) to allow a servlet to handle a TRACE request. A TRACE returns the headers sent with the TRACE request to the client, so that they can be used in debugging. There's no need to override this method. 由服务器service方法通过调用来允许servlet处理TRACE请求。 TRACE返回发送给客户端的TRACE请求头,因此它们可以用来调试。 不需要重写该方法。

Parameters:
req - the HttpServletRequest object that contains the request the client made of the servlet HttpServletResponse对象,包含客户端向servlet发出的请求
resp - the HttpServletResponse object that contains the response the servlet returns to the client HttpServletResponse对象,包含servlet向客户端返回的响应
Throws:
IOException - if an input or output error occurs while the servlet is handling the TRACE request 如果servlet处理TRACE请求时发生输入输出错误时抛出
ServletException - if the request for the TRACE cannot be handled 如果TRACE请求不能被处理则抛出
<!-- -->

service

Receives standard HTTP requests from the public service method and dispatches them to the doXXX methods defined in this class. This method is an HTTP-specific version of the Servlet.service(javax.servlet.ServletRequest, javax.servlet.ServletResponse) method. There's no need to override this method. 从公开的service方法接收标准HTTP请求,转发至类中定义的doXXX方法。该方法是Servlet.service(javax.servlet.ServletRequest, javax.servlet.ServletResponse)方法对于不同HTTP版本的具体实现。不需要重写该方法。

Parameters:
req - the HttpServletRequest object that contains the request the client made of the servlet HttpServletResponse对象,包含客户端向servlet发出的请求
resp - the HttpServletResponse object that contains the response the servlet returns to the client HttpServletResponse对象,包含servlet向客户端返回的响应
Throws:
IOException - if an input or output error occurs while the servlet is handling the HTTP request 如果servlet处理HTTP请求时发生输入输出错误时抛出
ServletException - if the HTTP request cannot be handled 如果HTTP请求不能被处理则抛出
See Also:
Servlet.service(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
<!-- -->

service

Dispatches client requests to the protected service method. There's no need to override this method. 转发客户端请求至受保护的service方法。不需要重写该方法。

Specified by:
service in interface Servlet
Specified by:
service in class GenericServlet
Parameters:
req - the HttpServletRequest object that contains the request the client made of the servlet HttpServletResponse对象,包含客户端向servlet发出的请求
res - the HttpServletResponse object that contains the response the servlet returns to the client HttpServletResponse对象,包含servlet向客户端返回的响应
Throws:
IOException - if an input or output error occurs while the servlet is handling the HTTP request 如果servlet处理HTTP请求时发生输入输出错误时抛出
ServletException - if the HTTP request cannot be handled 如果HTTP请求不能被处理则抛出
See Also:
Servlet.service(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
<!-- ========= END OF CLASS DATA ========= --> <!-- ======= START OF BOTTOM NAVBAR ====== --> <!-- -->
JavaTM 2 Platform
Ent. Ed. v1.4
<!-- ======== END OF BOTTOM NAVBAR ======= --> Submit a bug or feature

Copyright 2003 Sun Microsystems, Inc. All rights reserved.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值