Monday 30 January 2017

HTTP GET vs POST Implementation

HTTP GET vs HTTP POST method
Well HTTP is a communication protocol between the client (browser) and the server(tomcat).
Before that, a few heads up on the HTTP GET & POST METHOD.
a) GET method sends the parameters in the form after "?" symbol and appends the parameters by "&"
b) GET method has a limitation of the number of characters
c) GET method is used to obtain static pages from the server

The html page sends the request to the server with the details of "HTTP Method, Content-Type & Content.
The server will respond with the details of "Response Code, Content-Type, Content"

So lets see the difference with a simple example.
1. Create a "Login.html" file as below and place it in "TOMCAT_HOME/webapps/MySample/" folder
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form method="get" action="Serv1">
<br>Name<input type="text" size="15" name="name">
<br>Passwd<input type="text" size="15" name="passwd"><br>
<input type="submit">
</form></body>
</html>
2. Create a Servlet as below 
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class Ch1Servlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException
    {
    doPost(request,response);
    }
    
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException
    {
        
        PrintWriter out= response.getWriter();
        out.println("<html><body><h1>FirstServlet</h1></body></html>");
 
    }
}
3. Compile the servlet and place the class file in "TOMCAT_HOME/webapps/MySample/WEB-INF/classes" as
$javac -classpath TOMCAT_HOME/lib/servlet-api.jar -d TOMCAT_HOME/webapps/MySample/WEB-INF/classes/           Ch1Servlet.java
4. Create a web.xml file as below under "TOMCAT_HOME/webapps/MySample/WEB-INF" folder
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>FirstServlet</display-name>
 <servlet>
 <servlet-name>Chapter1 Servlet</servlet-name>
 <servlet-class>Ch1Servlet</servlet-class>
 </servlet>
 <servlet-mapping>
 <servlet-name>Chapter1 Servlet</servlet-name>
 <url-pattern>/Serv1</url-pattern>
 </servlet-mapping>
</web-app>
5. Start the tomcat server and invoke the application as, http://localhost:8080/MySample/Login.html
6. Change the Login.html to have "POST" instead of "GET" and observe that this time the username & passwd are not sent

No comments:

Post a Comment

Linux : Create a new user for the machine

Creating a new user in linux is sometimes needed, when you want to share the user access with anyone. SSH to the machine over the newly cre...