This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "servlet" Watch "servlet" New topic
Author

servlet

Shrawan Bhageria
Ranch Hand

Joined: Jun 20, 2005
Posts: 52
hi
pl. let me know the output of this code and why

index.jsp
==========

<form action="myServlet" method="post">
<input type="button" name="click">
</form>


myServlet.java
===============

public void service(HttpServletRequest req,HttpServletResponse res)throws ServletException ,IOException{

res.setContentType("text/html");

PrintWriter out=res.getWriter();
out.print("<HTML><BODY>Service method is executed</BODY></HTML>");

}

public void doGet(HttpServletRequest req,HttpServletResponse res)throws ServletException ,IOException{

res.setContentType("text/html");
PrintWriter out=res.getWriter();
out.print("<HTML><BODY>Get method is executed</BODY></HTML>");
}


public void doPost(HttpServletRequest req,HttpServletResponse res)throws ServletException ,IOException{
res.setContentType("text/html");
PrintWriter out=res.getWriter();
out.print("<HTML><BODY>Post method is executed</BODY></HTML>");

}



thanx in advance
Shrawan.K.Bhageria

SCJP1.4,MCP
SCWCD(preparing)
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Did you try running it?
What did you get?

Is this a homework assignment?


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Shrawan Bhageria
Ranch Hand

Joined: Jun 20, 2005
Posts: 52
hi Ben
i run it and the output is service method is executed but i can't understand why

thanx
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Take a look at SRV.15.1.2.2 in the Servlet Spec (link in my signature).
All the methods of the HttpServlet interface are described there.

It explains why you should never need to override the service method.
Shrawan Bhageria
Ranch Hand

Joined: Jun 20, 2005
Posts: 52
thanx Ben
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Did that answer it for you?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60810
    
  65

Please read this.

And...

There aren't may rules that you need to worry about here on the Ranch, but one that we take very seriously regards the use of proper names. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks!
bear
Forum Bartender


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Jagadesh Rangasamy
Greenhorn

Joined: Apr 13, 2005
Posts: 2
i think the respective doPost method will be called and get's executed.iam not sure with this.Anyone find the solution.plz share it.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Jagadesh Rangasamy:
i think the respective doPost method will be called and get's executed.iam not sure with this.Anyone find the solution.plz share it.


The section of the servlet spec that I mentioned earlier explains exactly what will happen and why.

In short: The "service" method is called for all requests. It will interrogate the request to find out it's type and call doGet, doPost, doHead, or whatever httpServlet method is appropriate.

There is almost never a need for an application developer to override it.
 
wood burning stoves
 
subject: servlet
 
Similar Threads
Progress bar on IE6
Doubts in HTTP Methods behaviour
Strange problem - request forwarding
How to make doPost as default service
File not found error