HTTPServlet Methods in Java

 Learn How to Use HTTPServlet Methods in Java Web Application

 

e-trainings-httpservlet-methods-example


To serve to client request, there are different types of methods provided in HTTPServlet.
In this tutorial, you are going to learn what are those methods and what is the difference between them, and how and when to use them.
 
Here is the list of HTTPServlet Class methods.


 

 

ServletsHome

 

HttpServlet - Methods

  • doGet()
  • doPost()
  • doDelete()
  • doHead()
  • doOptions()
  • doPut()
  • doTrace()


 

GET and Post requests are commonly used when handling form input.

 

Get: Data from client, send to server in the form of query string.Sends only limited bytes (Max. 100 bytes).

Post:data from client send to server in the form of packets.In this method we can send maximum (8 KB) amount of data.

 

 
 
HttpServletDemo.html

<html>
<body>

<form name="form1" action="HttpServletDemo">

Enter FirstNo:<input type=textbox name="t1" value=""><br>
Enter SecondNo:<input type=textbox name="t2" value=""><br>

<input type=submit name="sub" ><br>

</form>
</body>
</html>
 
 
 
 
import java.io.*;
import java.util.*;
import javax.servlet.*;
 
 
public class HttpServletDemo extends HttpServlet{

 public void doGet(HttpServletRequest req,HttpServletResponse res)
	throws ServletException,IoException{ 
 
  PrintWriter out= res.getWriter();
  String s1= req.getParameter("t1");
  String s2= req.getParameter("t2");
  int c=Integer.parseInt(s1)+Integer.parseInt(s2);


  out.println("c="+c);
 out.close();
}
} 

  


Source Code for Request(client) Info Example 
shows client info in html format. 
 
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class RequestInfo extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException
    {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
 
 out.println("<html>");
        out.println("<body>");
        out.println("<head>");
        out.println("<title>Request Information Example</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h3>Request Information Example</h3>");
        out.println("Request Method: " + request.getMethod());
        out.println("Request URI: " + request.getRequestURI());
        out.println("Protocol: " + request.getProtocol());
        out.println("PathInfo: " + request.getPathInfo());
        out.println("Remote Address: " + request.getRemoteAddr());
        out.println("</body>");
        out.println("</html>");
    }

    /**
     * We are going to perform the same operations for POST requests
     * as for GET methods, so this method just sends the request to
     * the doGet method.
     */

    public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException
    {
        doGet(request, response);
    }
} 

Source Code for RequestHeader Example
Used by Client to pass some additional information. 

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class RequestHeaderDemo extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException
    {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        Enumeration e = request.getHeaderNames();
        while (e.hasMoreElements()) {
            String name = (String)e.nextElement();
            String value = request.getHeader(name);
            out.println(name + " = " + value);
        }
    }
}


doPost()

 Using URL parameter

import java.io.*;
import java.util.*;
import javax.servlet.*;
public class Colorpostservlet extends HttpServlet{

 public void doPost(HttpServletRequest req,HttpServletResponse res)
	throws ServletException,IoException{
  PrintWriter out= res.getWriter();
  String color= req.getParameter("color");

  out.println("selected color is "+color);
  out.close();
}
}
  
 
 
 
 
 
Source Code for RequestHeader Example

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class RequestHeaderDemo extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException
    {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        Enumeration e = request.getHeaderNames();
        while (e.hasMoreElements()) {
            String name = (String)e.nextElement();
            String value = request.getHeader(name);
            out.println(name + " = " + value);
        }
    }
}
 
Dopost() example2
 
Using  HTML form
 
colorpost.html 
 
 
<html>
<body>
<form name="form1" method="post" action="Dopostservletdemo">
<select name="color">
<option>Green</option>
<option>red</option>
<option>Yellow</option>
</select>

<input type=submit name="submit" >
</form>
</body>
</html> 

 

doPost()

 

import java.io.*;
import java.util.*;
import javax.servlet.*;
 
public class Dopostservletdemo extends HttpServlet{

 public void doPost(HttpServletRequest req,HttpServletResponse res)
	throws ServletException,IoException{ 
 
  PrintWriter out= res.getWriter();
  String color= req.getParameter("color");

  out.println("Your Choice is "+color);
  out.close();
}
}

 

 In web.xml add the following code for above servlet.


   <servlet>
        <servlet-name>Dopostservletdemo </servlet-name>
        <servlet-class>Dopostservletdemo </servlet-class>
    </servlet>
  
  <servlet-mapping>
   <servlet-name>Dopostservletdemo </servlet-name>
   <url-pattern>/Dopostservletdemo </url-pattern>
</servlet-mapping>

Comments

Popular posts from this blog

Using javascript pass form variables to iframe src

Shoppingcart using PHP Sessions - Miniproject

Export Data to Excel with PHP and MySQL