File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Get vs Post Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Get vs Post" Watch "Get vs Post" New topic

Get vs Post

Chiran Mathur
Ranch Hand

Joined: Feb 07, 2001
Posts: 63
Can anyone explain the real differences between get and a post.
a. This is what I did.
II wrote a program using Get and did somethoing like :
PrintWriter out = new PrintWriter();
out.print("GET " +url+ "HTTP/r/r/n");
In this case the program returns me the html in the page . Now when I do:
PrintWriter out = new PrintWriter();
out.print("POST " +url+ "HTTP/r/r/n");
the program works but it does not return anything.

Can anyone explain how get and post differ and how they work internally?
Ricardo Cortes
Ranch Hand

Joined: Jan 23, 2002
Posts: 140
Can you show us your program that you wrote? (Remember to use the <CODE> attributes)

Sun Certified J2EE Architect for the J2EE Platform (Part 1)<br />Sun Certified Web Component Developer for the J2EE Platform<br />Sun Certified Programmer for the Java 2 Platform
Hemal Mehta
Ranch Hand

Joined: Nov 16, 2000
Posts: 101
Code is:
Do not woryy about the SSL code, Just look at the GET vs POST. GEt always retyurns something but POST doesn't
public void run() throws Exception {
String thisLine;
// Get a SocketFactory object for creating SSL sockets
SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault();
// Create a secure socket connected to the HTTPS port (port 443) of a server
SSLSocket sslsock = (SSLSocket) factory.createSocket("", 443);
//SSLSocket sslsock = (SSLSocket) factory.createSocket("", 443);
// Get the certificate presented by the web server. This may throw an
// exception if the server didn't supply a certificate. Look at the
// issuer of the certificate and decide if it is trusted.
SSLSession session = sslsock.getSession();
X509Certificate cert = (X509Certificate)session.getPeerCertificateChain()[0];
String issuer = cert.getIssuerDN().getName();
// Assuming we trust the certificate, we now use the socket just like a normal
// object. So send a HTTP request and read the response
PrintWriter out = new PrintWriter(sslsock.getOutputStream());
//out.print("GET " + "/j2se/1.4/docs/api/java/lang/IllegalArgumentException.html" + " HTTP/1.0\r\n");
out.write("POST " + "/eweb/secure/web/jsp/index.jsp" + " HTTP/1.0\r\n\r\n");

// Next, read the server's response and print it to the console.
BufferedReader in =
new BufferedReader(new InputStreamReader(sslsock.getInputStream()));
String line;
while((line = in.readLine()) != null) System.out.println(line);
// Finally, close the socket.

Kyle Tang
Ranch Hand

Joined: Aug 22, 2002
Posts: 78
well, if your program sends a HTTP Get request, and the request url is a html page, then you will see that html page. if the url is a servlet, or jsp, you will see what the doGet() responses. if the doGet writes something, you see it, if it writes nothing, you see nothing. (you got only Http status code and Http headers)
if your program sends a HTTP Post request, but the url(in your program it is a jsp page) does not generate any response, then you got nothing.
both Get and Post could generate responses, binary or text, but it could just response with Http status code and Http headers, with no content returned. Depend on what the requested url does.
I am not sure if I am answering the stuff you want to know.

Kyle Tang<br />SCJP 91<br />SCWCD 96<br />SCBCD 95
Chiran Mathur
Ranch Hand

Joined: Feb 07, 2001
Posts: 63
This URL when using GET returns the html. However when using POST it doesn't return anything. That's what I cannot understand.
Kyle Tang
Ranch Hand

Joined: Aug 22, 2002
Posts: 78
a static resource can't handle a POST request.
web server receives the HTTP request.
If the requested url is a static resource,
if it is a GET, web server will return that resource.
if it is a POST, what will be returned depends on the web server. it may be status_code 405, for example.
If the requested url is a dynamic resource, say a Servlet or jsp, web server will let the dynamic resouce handle the request. No matter whether it is GET, POST, PUT, DELETE and other HTTP requests.
POST means "I give you this posted data, you process it for me, and return me the result". static resources can't process the POST request.
GET means, "hey web server, give me that url".
Chiran Mathur
Ranch Hand

Joined: Feb 07, 2001
Posts: 63
Cool, I got it Kyle great explanation.
I agree. Here's the link:
subject: Get vs Post
It's not a secret anymore!