| Author |
How to make doPost as default service
|
Prabhat Jha
Ranch Hand
Joined: Aug 13, 2007
Posts: 58
|
|
Hi, I have a servlet and user directly can access the server something like : http://localhost:8080/doSomeThing.do where doSomeThing.do is the URL pattern. My web server is tomcat 6. Now by default the http method here is Get and my doGet is getting called. If i use RequestDispatcher's forward method to forward the request to any other Servlet and JSP then also its a GET and not POST. Can i force it to call POST instead of GET? Thanks.
|
Thanks,
Prabhat
SCJP 1.5, SCWCD 1.5, SCBCD 1.5
|
 |
Deepak Bala
Bartender
Joined: Feb 24, 2006
Posts: 6588
|
|
|
It is possible to call the POST method from the GET method if you want to ensure that a response is sent for your request. I am not sure what it is that you are trying to do so that reply may or may not help
|
SCJP 6 articles - SCJP 5/6 mock exams - SCJP Mocks - SCJP 5 Mock exam (Word document ) - SCJP 5 Mock exam in Java.Inquisition format
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56200
|
|
|
No you cannot turn a GET request into a POST. You'll need to generate a new request to do that.
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
Prabhat Jha
Ranch Hand
Joined: Aug 13, 2007
Posts: 58
|
|
Thanks for your replies. How do i generate a new request? Do you mean creating a new HttpServletRequest ?
|
 |
Ben Souther
Sheriff
Joined: Dec 11, 2004
Posts: 13410
|
|
No, he means have the browser make a new request to the server. Have you implemented doPost and doGet already in your servlet? If not, why don't you follow John's advice and have doGet call doPost?
|
Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
|
 |
Prabhat Jha
Ranch Hand
Joined: Aug 13, 2007
Posts: 58
|
|
|
Thanks i got it.
|
 |
kamal kannan
Ranch Hand
Joined: Oct 06, 2006
Posts: 33
|
|
import javax.servlet.*; import javax.servlet.http.*; import java.io.*; public class serlife extends HttpServlet { int i=0; public void init() { i=0;} public void service(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException { doPost(req,res); } public void doPost(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException { res.setContentType("text/html"); PrintWriter pr=res.getWriter(); pr.println("<html><body>"); if(i==0) { pr.println("<br>i value at initialized = "+i); } i++; if(i==5) { pr.println("<br>i value reach at = "+i); pr.println("<br>it going to reset calling by destroy method"); destroy(); } if(i<5) { pr.println("<br>The increment value i at ="+i); } } public void destroy() {i=0;} }
|
SCJP 1.5 95% /SCWCD 1.5 93% /RHCE EL4 100% /WEBLOGIC 9.2 SYSTEM ADMINISTRATION 100% / Oracle Weblogic 10g Server Certified System Export 88%
|
 |
Praveen Kumar
Ranch Hand
Joined: Nov 06, 2006
Posts: 133
|
|
|
Can I get that behaviour , if I call doPost() method inside doGet () mehtod ?
|
 |
Deepak Bala
Bartender
Joined: Feb 24, 2006
Posts: 6588
|
|
Naw. Thats bad. You never override service for something like that Thats bad too. We have JSPs and tags for a reason. It is so that we can get the clutter out of the servlet
Can I get that behaviour , if I call doPost() method inside doGet () mehtod ?
What is the behavior you are talking about ? You will have to try one of the suggestions mentioned above by all the posters
|
 |
Ben Souther
Sheriff
Joined: Dec 11, 2004
Posts: 13410
|
|
Originally posted by pdommara Kumar: Can I get that behaviour , if I call doPost() method inside doGet () mehtod ?
Yes
|
 |
 |
|
|
subject: How to make doPost as default service
|
|
|