您的位置:首页 > 运维架构

servlet doGet doPost区别

2006-11-12 23:21 507 查看
[align=left]如转贴中所描述,在 servlet生命周期中,service方法进行判断调用doGet还是doPost方法[/align]
[align=left]init()
service() {goGet() & doPost()}
destroy()[/align]
[align=left][/align]
[align=left]技术实现上,可以doGet/doPost使用相同的代码[/align]
[align=left]如写一个doXXX,在doGet/doPost中均使用doXXX[/align]
[align=left][/align]

[align=left]
[/align]



UBBFriend: Email this page to someone!
Author Topic: Differences Between doGet(),doPost(),service()
ganapathy gopinathan
greenhorn
Member # 78471


posted September 28, 2004 07:52 AM











Hi all,

Anyone plz help me to understand the differences between the doGet(),doPost(),and service() methods.

Thank u
gopi
Posts: 30 | Registered: Aug 2004 | IP: Logged
sanjeevmehra mehra
ranch hand
Member # 79249


posted September 28, 2004 08:09 AM










init()
service() {goGet() & doPost())
destroy()
are the lifecycle methods. init() & destroy() are called once in lifetime. And service() method is called whenever a request comes. If request is through the Get method, service() method calls (pass that request to) doGet() and if request is through post method service() method calls (pass that request to the) doPost() method.

thanks & regards,
sanjeev. Posts: 88 | Registered: Aug 2004 | IP: Logged
--------------------

thanks & regards,
Sanjeev.

Stan James
(instanceof Sidekick)
Member # 44095


posted September 28, 2004 10:07 AM










Technically you can make doGet and doPost act identically. It's not uncommon to see both of those methods call doProcess or some other method you make up, allowing users to get or post at will.

The inventors of HTTP saw them as very different however. GET operations must be "idempotent" which just means they have no side effects and repeated calls should give the same result. A pure query in other words. POST operations should be used to send information to the server that will change the state of some object or data store.

HERE is an article on the topic that I happened to be reading right before I saw your note. The inventors of HTTP promote a web application architecture called REST (Representational State Transfer) that assigns very particular meaning to all the HTTP verbs - GET, POST, PUT, HEAD, etc - and further tells us that these are all we need. Posts: 6763 | Registered: Jan 2003 | IP: Logged
--------------------

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Kathy Sierra
Cowgirl and Author
Member # 37766


posted September 28, 2004 10:19 AM











quote:
Originally posted by Stan James:
GET operations must be "idempotent" which just means they have no side effects and repeated calls should give the same result.

I just wanted to make one tiny clarification--I know what you meant Stan, but I just wanted to make sure it's clear to everyone: HTTP GET operations do *not* need to always "give the same result", in terms of the *response*. The *response* can be different with each GET, as long as there are no side-effects. So what you said is correct (no side effects, pure query, etc.); I'm just making sure that everyone knows that "repeated calls should give the same result" does not mean "same RESPONSE".

cheers and thanks for the good link!
-Kathy
Posts: 1716 | Registered: Oct 2002 | IP: Logged
ganapathy gopinathan
greenhorn
Member # 78471


posted September 29, 2004 04:53 PM










what is the difference between 'service()' and 'doGet()'(or 'doPost()')?
Posts: 30 | Registered: Aug 2004 | IP: Logged
Adeel Ansari
ranch hand
Member # 78839


posted September 29, 2004 10:46 PM












Now you haven't got the difference right. ok then read the docs or some tutorial, book.
Again there is no better substitute.

cheers

Posts: 2321 | Registered: Aug 2004 | IP: Logged
--------------------

The code I wrote nine months ago sucks. And I'm happy about it. — Bear Bibeault

sanjeevmehra mehra
ranch hand
Member # 79249


posted September 30, 2004 01:29 AM









quote:
what is the difference between 'service()' and 'doGet()'(or 'doPost()')?

service(), doGet() and doPost() are methods, and are used to do operations (methods are to do operations/task, hope that's clear).
you can perform the same or different task in doGet() & in doPost() as per your need(requirement). So, there is no difference b/w doGet() & doPost() and you need to implement these methods as per your requirements. At the moment you should know when doGet() would be called & when doPost() would be called and how.

quote:
Now you haven't got the difference right. ok then read the docs or some tutorial, book.
Again there is no better substitute.

do the same & share your doubts.

thanks & regards.
--------------------

thanks & regards,
Sanjeev.

Posts: 88 | Registered: Aug 2004 | IP: Logged
ganapathy gopinathan
greenhorn
Member # 78471


posted September 30, 2004 08:14 AM









Correct me if i m wrong,

GET():

- 'Get' is used to send the form data with the request itself.
- only 256 characters are allowed.
- when there is no such class or nothing to satisfy the request the data is lost.

POST():
- first header is send, the form fields are send after the finding the something that satisfies the request.
- i think there is no limitation on no of charactors to be sent.
- if there is nothing to satisfy the request then the data is not sent.

let me know in which conditions or situation we have to use the service()?

Thanks
gopinathan Posts: 30 | Registered: Aug 2004 | IP: Logged
Adeel Ansari
ranch hand
Member # 78839


posted October 01, 2004 12:53 AM











hey gopi i found something simple and somewhat complete for you. here is that,

After the servlet is initialized, the container may keep it ready for handling client
requests. When client requests arrive, they are delegated to the servlet through the
service()method, passing the request and response objects as parameters. In the
case of HTTP requests, the request and response objects are implementations of
HttpServletRequest and HttpServletResponse respectively. In the
HttpServlet class, the service() method invokes a different handler method for
each type of HTTP request, doGet() method for GET requests, doPost() method for
POST requests, and so on.


some more,


GET method

The GET method is used to retrieve a resource (like an image or an HTML page) from
the server, which is specified in the request URL. When the user types the request
URL into the browser's location field or clicks on a hyperlink, the GET method is
triggered. If a tag is used, the method attribute can be specified as "GET" to cause the
browser to send a GET request. Even if no method attribute is specified, the browser
uses the GET method by default.
We can pass request parameters by having a query string appended to the request
URL, which is a set of name-value pairs separated by an "&" character. Here we have passed the parameters studname and studno, which have the values
"Tom" and "123" respectively. Because the data passed using the GET method is
visible inside the URL, it is not advisable to send sensitive information in this manner.
The other restrictions for the GET method are that it can pass only text data and not more than 255 characters (i read somewhere that it depends on browser support).

POST method
The purpose of the POST method is to "post" or send information to the server. It is
possible to send an unlimited amount of data as part of a POST request, and the type of
data can be binary or text.
This method is usually used for sending bulk data, such as uploading files or updating
databases. The method attribute of the <form> tag can be specified as "POST" to
cause the browser to send a POST request to the server.
Because the request parameters are sent as part of the request body, it is not visible
as part of the request URL, which was also the case with the GET method.
[ October 01, 2004: Message edited by: adeel ansari ]

--------------------

The code I wrote nine months ago sucks. And I'm happy about it. — Bear Bibeault

Posts: 2321 | Registered: Aug 2004 | IP: Logged
srinivasan doraiswamy
greenhorn
Member # 82476


posted October 01, 2004 01:23 AM





when the req is received by the service method, req and res are cast to their http counter parts and service method with http req and res is invoked, this method directs the request to the appropriate method based on get or post, actually u no need to implement the service method instead u should implement these subsidiary methods (get or post) in ur servlet Posts: 5 | Registered: Sep 2004 | IP: Logged
All times are MST (US) = UTC - 0700














Hop To:
Select a Forum:

Category: Java
--------------------
Servlets
JSP
JSF
Portals and Portlets
EJB and Other J2EE Technologies
Distributed Java
Object Relational Mapping
JDBC
Web Services
Swing / AWT / SWT / JFace
JNLP and Web Start
Java 2 Micro Edition
Sockets and Internet Protocols
Threads and Synchronization
Performance
Applets
I/O and Streams
Other Java APIs
Game Development
Java in General (beginner)
Java in General (intermediate)
Java in General (advanced)

Category: Professional Certification
--------------------
Programmer Certification (SCJP)
Developer Certification (SCJD)
Associate Certification (SCJA)
Web Component Certification (SCWCD)
EJB Certification (SCBCD)
Mobile Application Certification (SCMAD)
Architect Certification (SCEA)
Web Services Certification (SCDJWS)
XML Certification
Product and Other Certifications
Mock Exam Errata
Sun Certification Results

Category: Books and Education
--------------------
Authors' Corral
Book Reviews
Events
Bunkhouse Porch
Teachers' Lounge

Category: Engineering
--------------------
Testing
OO, Patterns, UML and Refactoring
IDEs, Version Control and other tools
Ant, Maven and Other Build Tools
Linux / UNIX
Mac OS
HTML and JavaScript
XML and Related Technologies
Agile and Other Processes
General Computing
Object Oriented Scripting
Security

Category: Products
--------------------
Struts
Application Frameworks
Other Open Source Projects
BEA/Weblogic
IBM/Websphere
Oracle/OAS
Apache/Tomcat
JBoss
Other Java Products and Servers

Category: This Site
--------------------
JavaRanch
Cattle Drive (java college)
Moderators Only
Trash Can

Category: Careers
--------------------
Jobs Offered
Jobs Wanted
Jobs Discussion

Category: Other
--------------------
Meaningless Drivel
Programming Diversions
Blatant Advertising

[align=left]Contact Us | JavaRanch [/align]
[align=left]Copyright © 1998-2006 Paul Wheaton [/align]



Ultimate Bulletin BoardTM 6.1.0.3




[align=left][/align]




内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: