[求助]??servlet中doGet,doPost区别??

hooligan31000 2004-07-26 07:39:54
public class HelloServlet extends HttpServlet {
          //所有Servlet必须从HttpServlet派生
  public void doGet (HttpServletRequest req, HttpServletResponse res)
     throws ServletException, IOException
     //doGet()是这个Servlet的核心,真正处理请求的地方
.........
请问大侠:
doGet,doPost区别是什么,什么时候要用doGet,什么时候用doPost???
谢谢!!!
...全文
808 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
sboom 2004-07-27
  • 打赏
  • 举报
回复
GET和POST对应表单的提交方式
松耦合紧内聚 2004-07-26
  • 打赏
  • 举报
回复
doGet() & doPost()都是service()方法调用的!service方法是个模板方法,通过action的类型,判断调用那个方法!
所以你只需要覆盖doGet,doPost方法就好了,系统直接调用service方法来执行。
要是覆盖service方法,那么doGet和doPost就不管用了!
happyili 2004-07-26
  • 打赏
  • 举报
回复

PUT is used to provide a new or replacement document to be stored on the server.
{
PUT /example.html HTTP/1.1
Host: publish.ora.com
Pragma: no-cache
Connection: close
User-Agent: SimplePublish/1.0
Accept: image/gif, image/x-xbitmap,
image/jpeg, image/pjpeg, */*
Content-type: text/html
Content-Length: 182

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE></TITLE>
</HEAD>
<BODY>


The server stores the client's entity-body at /example.html and then responds with:

HTTP/1.0 201 Created
Date: Fri, 04 Oct 1996 14:31:51 GMT
Server: HypotheticalPublish/1.0
Content-type: text/html
Content-length: 30

<h1>The file was created.</h1>
}



DELETE is used to remove a document on the server.
{
DELETE /images/logo22.gif HTTP/1.1
Host: hypothetical.ora.com
The server responds with a success code upon success:

HTTP/1.0 200 OK
Date: Fri, 04 Oct 1996 14:31:51 GMT
Server: HypotheticalPublish/1.0
Content-type: text/html
Content-length: 21

<h1>URL deleted.</h1>
}

TRACE asks that proxies declare themselves in the headers, so the client can learn the path that the document took (and thus determine where something might have been garbled or lost). This is used for protocol debugging purposes.
{
When the TRACE method is used with the Max-Forwards and Via headers, a client can determine the chain of intermediate proxy servers between the original client and web server. The Max-Forwards request header specifies the number of intermediate proxy servers allowed to pass the request. Each proxy server decrements the Max-Forwards value and appends its HTTP version number and hostname to the Via header. A proxy server that receives a Max-Forwards value of ?? returns the client's HTTP headers as an entity-body with the Content-type of message/http.
}

OPTIONS is used when the client wants to know what other methods can be used for that document (or for the server at large).
{
it requests a list of options for a particular resource on the server. The client specifies a URL for the OPTIONS method, or an asterisk (*) to refer to the entire server. The server then responds with a list of request methods or other options that are valid for the requested

// request
OPTION * HTTP/1.1
// server response
HTTP/1.1 200 OK
PUBLIC: GET, HEAD, POST, PUT

}

CONNECT is used when a client needs to talk to a HTTPS server through a proxy server.
{
When an http client wants to connect to an HTTPS server, but needs to do it through a proxy server, it issues a CONNECT to the proxy server. For example, the client connects to the proxy server and issues:

CONNECT www.onsale.com:443 HTTP/1.0
User-Agent: Mozilla/4.08 [en] (WinNT; U ;Nav)
And the server responds with:

HTTP/1.0 200 Connection established
Proxy-agent: Apache/1.3.9 (Unix)
From there, all traffic is encrypted with SSL.
The browser sends another HTTP message, this time inside the connection that was established with CONNECT through the proxy server. At this point, the proxy server just relays the data between the client and origin server.
}




Code Range Response Meaning
100-199 Informational
200-299 Client request successful
300-399 Client request redirected, further action necessary
400-499 Client request incomplete
500-599 Server errors

In HTTP 1.1, the 100 range was defined for the server to declare that it is ready for the client to continue with a request, or to declare that it will be switching to another protocol.

If a 301 is a change of address card, a 302 or 307 is a note on your friend's door saying she's gone to the movies

404 is one of the few codes that the client passes to the user rather than intercepting and handling it in its own way.
For example, when the server sends a 401 (Unauthorized) code, the client does not pass the code directly to the user. Instead, it triggers the client to prompt the user for a username and password







when the size of the document is too dynamic for a server to predict, the Content-length header is omitted. When this happens, the client reads in the data portion of the server's response until the server disconnects the network connection. This practice is obsolete and only works in HTTP 1.0. For generating data without knowing the total message length in advance, the next method is recommended.

Another header could indicate when an entity-body ends, like HTTP 1.1's Transfer-Encoding header with the chunked parameter.

If the server declares that it supports byte ranges using the Accept-Ranges header:

HTTP/1.1 200 OK
[Other headers here]
Accept-Ranges: bytes
The client can then request the data in pieces. For example:

GET /largefile.html HTTP/1.1
[Other headers here]
Range: 0-65535

When the server returns the specified range, it includes a Content-range header to indicate which portion of the document is being sent, and also to tell the client how long the file is:

HTTP/1.1 200 OK
[Other headers here]
Content-range: 0-65535/83028576

For caching purposes, a client can use the If-Range header along with Range to request an updated portion of the document only if the document has been changed. Here is an example:

GET /largefile.html HTTP/1.1
[Other headers here]
If-Range: Mon, 02 May 1996 04:51:00 GMT
Range: 0-65535

The If-Range header can use either a last modified date or an entity tag to verify that the document is still the same.

happyili 2004-07-26
  • 打赏
  • 举报
回复
一点看来得笔记,http协议的。
GET / HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/
jpeg, image/pjpeg, */*
Accept-Language: en-us
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE
5.01; Windows NT)
Host: hypothetical.ora.com
Connection: Keep-Alive

The first line of this request (GET / HTTP/1.1) requests a document at / from the server. HTTP/1.1 is given as the version of the HTTP protocol that the browser uses.

The seventh line (Connection:) tells the server to keep the TCP connection open until explicitly told to disconnect

HTTP/1.1 200 OK
Date: Mon, 06 Dec 1999 20:54:26 GMT
Server: Apache/1.3.6 (Unix)
Last-Modified: Fri, 04 Oct 1996 14:06:11 GMT
ETag: "2f5cd-964-381e1bd6"
Accept-Ranges: bytes
Content-length: 327
Connection: close
Content-type: text/html

the server looks for the server resource associated with "/" and returns it to the browser, preceding it with header information in its response

The first line, HTTP/1.1 200 OK, tells the client what version of the HTTP protocol the server uses. But more importantly, by returning a status code of 200, it says that the document has been found and will transmit the document in its response.

The second line indicates the current date on the server

The fourth line specifies the most recent modification time of the document requested by the client. This modification time is often used for caching purposes—so a browser may not need to request the entire HTML file again if its modification time doesn't change

The fifth line indicates an entity tag. This provides the web client with a unique identifier for the server resource. It is highly unlikely for two different server resources to have the same entity tag. This tag provides a powerful mechanism for caching.

The sixth line indicates to the browser that the server possesses the ability to return subsections of a document, instead of returning the entire document every time it is requested. This is useful for retrieving records in a document, which may be useful for database and streaming multimedia applications.

The eighth line indicates that the connection will close after the server's response. If the client wants to send another request, it will need to open another connection to the server.

You can think of a GET request as meaning that you just want to retrieve a resource on the server. This resource could be the contents of a static file or invoke a program that generates data.



A HEAD request means that you just want some information about the document, but don't need the document itself.
{
The headers returned by the server with the HEAD method should be exactly the same as the headers returned with a GET request. This method is often used by web clients to verify the document's existence or properties (like Content-length or Content-type), but the client has no intention of retrieving the document in the transaction.

+ which make it possible to retrieve:
Modification time of a document for caching purposes
Size of the document, to do page layout, estimate arrival time, or skip the document and retrieve a smaller version of the document
Type of the document, to allow the client to examine only documents of a certain type
Type of server, to allow customized server queries

The server responds with:
HTTP/1.1 200 OK
Date: Mon, 06 Dec 1999 20:54:26 GMT
Server: Apache/1.3.6 (Unix)
Last-Modified: Fri, 04 Oct 1996 14:06:11 GMT
ETag: "2f5cd-964-381e1bd6"
Accept-Ranges: bytes
Content-length: 327
Connection: close
Content-type: text/html
Note the server does not return any data after the headers.
}


A POST request says that you're providing some information of your own (generally used for fill-in forms). This typically changes the state of the server in some way. For example, it could create a record in a database.
{
POST /cgi-bin/create.pl HTTP/1.1
...
Content-type: multipart/form-data; boundary=--
-------------------------11512135131576
Content-Length: 313

-----------------------------11512135131576
Content-Disposition: form-data; name="done"

Submit Query
-----------------------------11512135131576
Content-Disposition: form-data; name="thefile"; filename="c:\temp\hi.txt"
Content-Type: text/plain

hi there

-----------------------------11512135131576--

Using the POST method is not the only way that forms send information. Forms can also use the GET method, and append the URL-encoded data to the URL, following a question mark. If the <form> tag had contained the line method="get" instead of method="post", the request would have looked like this:

GET /cgi-bin/create.pl?user=util-
tester&pass1=1234&pass2=1234 HTTP/1.1
Host: examples.ora.com
Referer: http://examples.ora.com/create.html
Accept: image/gif, image/x-xbitmap,
image/jpeg, image/pjpeg, */*

since it can be appended to the URL itself, it cannot contain special characters such as spaces, newlines, etc. For that reason, it is called URL-encoded.

When the client wants to send characters that normally have special meanings, like the ampersand and equal sign, the client replaces the characters with a percent sign (%) followed by an ASCII value in hexadecimal (base 16). This removes ambiguity when a special character is used. The only exception, however, is the space character (ASCII 32), which can be encoded as a plus sign (+) as well as %20. The preferred format is %20 instead of the plus sign.
}
nighthawk 2004-07-26
  • 打赏
  • 举报
回复
楼上的基本上都说了,我就不重复,补充一下,get的信息字符串大小不能超过255
另外get只能是字符串,post可以是对象。一般来说数据量小,且无隐私可言用get,反之用post
hooligan000112003 2004-07-26
  • 打赏
  • 举报
回复
get一般用来传递少量数据,所有数据值都可以在浏览器里看到,这样的方式传递数据不安全.

post用来传递较大的数量,更为安全.
hooligan31000 2004-07-26
  • 打赏
  • 举报
回复
那么一般是用doPost,什么时候一定要用doGet??
梦想家起飞 2004-07-26
  • 打赏
  • 举报
回复
呵呵。。如果你真想弄个明明白白,最好去看HTTP协议的工作原理。:)
xiaowenand 2004-07-26
  • 打赏
  • 举报
回复
get一般用来传递少量数据,所有数据值都可以在浏览器里看到,这样的方式传递数据不安全.

post用来传递较大的数量,更为安全.
dropship 2004-07-26
  • 打赏
  • 举报
回复
get和post都是提交数据的方式。
get方式传递数据时,参数在浏览器的地址可以看得到。适合少量数据的传输,方便
post方式传递数据数量不限,并且浏览器地址栏看不到。
umbrella1984 2004-07-26
  • 打赏
  • 举报
回复
http://community.csdn.net/Expert/topic/3212/3212183.xml?temp=.5817835

看上面地址后面的temp=.5817835
用doGet()可以取到temp的值,doPost()用来取提交表单的时候填写的值,例如注册的时候填的姓名等等。。
fanqingfeng 2004-07-26
  • 打赏
  • 举报
回复
get是获取 post是发送

81,095

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧