Get and Post

A very basic question..what is the difference between the doGet and doPost methods of servlet
[100 byte] By [sumeshzzza] at [2007-11-27 6:07:23]
# 1

The doGet() method handles requests when a HTTP GET request is invoked (plain link, form method="get", etc)

The doPost() method handles requests when a HTTP POST request is invoked (form method="post", etc)

Fairly straightforward, isn't it? If you're not familair with HTTP methods, then I'd suggest you to read the HTTP methods spec at W3: http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html

The HttpServlet API also contains some information: http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/http/HttpServlet.html

BalusCa at 2007-7-12 16:24:54 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

actually adding a little bit more to balu explanation.

lets take an example i have a form with 4 fields

1)firstname

2)lastname

3)username

4)password

Now if i type http://localhost:8080/servlet/TestingServlet

i see 4 fields with 4 textbox. The code for the fields and text goes in the doGet method. cause you are trying to get something.

secondly lets say you have a submit button also in that form. so when a user clicks on submit you need to do something or may be redirect to another page. the code to do that comes in doPost, dont worry be patience i was also not getting the idea properly but thank God i bought this j2ee book which clearly explains about servlets, jsp, ejb. I am still on servlets. hope this helps.

fastmikea at 2007-7-12 16:24:54 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

[nobr]just a simple example. try to run it and you will get an idea what i am trying to say. my interface goes in the doGet method the structure i mean.

when a user clicks submit then whatever happens goes in doPost method

import javax.servlet.*;

import javax.servlet.http.*;

import java.io.*;

import java.util.*;

public class LoginServlet extends HttpServlet {

private void sendLoginForm(HttpServletResponse response,

boolean withErrorMessage)

throws ServletException, IOException {

response.setContentType("text/html");

PrintWriter out = response.getWriter();

out.println("<HTML>");

out.println("<HEAD>");

out.println("<TITLE>Login</TITLE>");

out.println("</HEAD>");

out.println("<BODY>");

if (withErrorMessage)

out.println("Login failed. Please try again.<BR>");

out.println("<BR>");

out.println("<BR>Please enter your user name and password.");

out.println("<BR><FORM METHOD=POST>");

out.println("<BR>User Name: <INPUT TYPE=TEXT NAME=userName>");

out.println("<BR>Password: <INPUT TYPE=PASSWORD NAME=password>");

out.println("<BR><INPUT TYPE=SUBMIT VALUE=Submit>");

out.println("</FORM>");

out.println("</BODY>");

out.println("</HTML>");

}

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

sendLoginForm(response, false);

}

public void doPost(HttpServletRequest request,

HttpServletResponse response)

throws ServletException, IOException {

String userName = request.getParameter("userName");

String password = request.getParameter("password");

if (userName!=null && password!=null &&

userName.equals("jamesb") && password.equals("007")) {

response.sendRedirect("http://domain/app/WelcomePage");

}

else {

sendLoginForm(response, true);

}

}

}

[/nobr]

fastmikea at 2007-7-12 16:24:54 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

Hi we are also using doPost method

we try to create a servlet page that automatically refreshes itself wothout clicking refresh button ... but this page comes when we logged in ...

So we created session variables but when we try to receive them it does not work I think it gives null for session variables

it gives out http errro 500

can we pass session variables to an another servlet page ? that is what we are trying to do but could you help us ?

Please ?

malkaraa at 2007-7-12 16:24:54 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...