servlet doGet doPost区别

如转贴中所描述,在 servlet生命周期中,service方法进行判断调用doGet还是doPost方法
init()
service() {goGet() & doPost()}
destroy()
技术实现上,可以doGet/doPost使用相同的代码
如写一个doXXX,在doGet/doPost中均使用doXXX

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      Profile for ganapathy gopinathan   Email ganapathy gopinathan   Send New Private Message      Edit/Delete Post  Reply With Quote 


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      Profile for sanjeevmehra mehra   Email sanjeevmehra mehra   Send New Private Message      Edit/Delete Post  Reply With Quote 

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      Profile for Stan James   Author's Homepage     Send New Private Message      Edit/Delete Post  Reply With Quote 

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      Profile for Kathy Sierra   Author's Homepage   Email Kathy Sierra   Send New Private Message      Edit/Delete Post  Reply With Quote 
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      Profile for ganapathy gopinathan   Email ganapathy gopinathan   Send New Private Message      Edit/Delete Post  Reply With Quote 

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      Profile for Adeel Ansari   Author's Homepage   Email Adeel Ansari   Send New Private Message      Edit/Delete Post  Reply With Quote 

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      Profile for sanjeevmehra mehra   Email sanjeevmehra mehra   Send New Private Message      Edit/Delete Post  Reply With Quote 
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      Profile for ganapathy gopinathan   Email ganapathy gopinathan   Send New Private Message      Edit/Delete Post  Reply With Quote 
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      Profile for Adeel Ansari   Author's Homepage   Email Adeel Ansari   Send New Private Message      Edit/Delete Post  Reply With Quote 
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
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      Profile for srinivasan doraiswamy        Edit/Delete Post  Reply With Quote 
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  
Post New Topic  Post A Reply Close Topic    Move Topic    Delete Topic next oldest topic   next newest topic
Hop To: Select a Forum:Category: Java--------------------ServletsJSPJSFPortals and PortletsEJB and Other J2EE TechnologiesDistributed JavaObject Relational MappingJDBCWeb ServicesSwing / AWT / SWT / JFaceJNLP and Web StartJava 2 Micro EditionSockets and Internet ProtocolsThreads and SynchronizationPerformanceAppletsI/O and StreamsOther Java APIsGame DevelopmentJava 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 CertificationProduct and Other CertificationsMock Exam ErrataSun Certification ResultsCategory: Books and Education--------------------Authors' CorralBook ReviewsEventsBunkhouse PorchTeachers' LoungeCategory: Engineering--------------------TestingOO, Patterns, UML and RefactoringIDEs, Version Control and other toolsAnt, Maven and Other Build ToolsLinux / UNIXMac OSHTML and JavaScriptXML and Related TechnologiesAgile and Other ProcessesGeneral ComputingObject Oriented ScriptingSecurityCategory: Products--------------------StrutsApplication FrameworksOther Open Source ProjectsBEA/WeblogicIBM/WebsphereOracle/OASApache/TomcatJBossOther Java Products and ServersCategory: This Site--------------------JavaRanchCattle Drive (java college)Moderators OnlyTrash CanCategory: Careers--------------------Jobs OfferedJobs WantedJobs DiscussionCategory: Other--------------------Meaningless DrivelProgramming DiversionsBlatant Advertising

 

 

Contact Us | JavaRanch
Copyright © 1998-2006 Paul Wheaton
Infopop Corporation
Ultimate Bulletin Board TM 6.1.0.3

 

 

你可能感兴趣的:(servlet doGet doPost区别)