Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to make doPost as default service

 
Prabhat Jha
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65229
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No you cannot turn a GET request into a POST. You'll need to generate a new request to do that.
 
Prabhat Jha
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your replies.

How do i generate a new request?

Do you mean creating a new HttpServletRequest ?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Prabhat Jha
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks i got it.
 
kamal kannan
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;}
}

 
Praveen Kumar
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can I get that behaviour , if I call doPost() method inside doGet () mehtod ?
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by pdommara Kumar:
Can I get that behaviour , if I call doPost() method inside doGet () mehtod ?


Yes
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic