• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

servlet

 
Shrawan Bhageria
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you try running it?
What did you get?

Is this a homework assignment?
 
Shrawan Bhageria
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Ben
i run it and the output is service method is executed but i can't understand why

thanx
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanx Ben
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64959
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jagadesh Rangasamy
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic