doGET and doPOST

This Article is a guide on how to use doGet and doPost method in Java servlet in JavEE web app

Get requests are the usual type of browser requests for Web pages when the user enters a URL on the address line, follows
a link from a Web page, or submits an HTML form that either does not specify a METHOD or specifies METHOD=”GET”.

When a java class extends HttpServlet , there is a need to either overrider doGET(), or doPOST(); depending on the whether the data is been sent by GET or POST method
doGET takes two arguments as

public class Test extends HttpServlet
{

    @Override
    public void doGet( HttpServletRequest request, HttpSevletResponse reponse ) throws ServletException, IOException
    {
// CODE HERE
    }

}

POST requests are generated when someone submits an HTML form that specifies METHOD=”POST”.

When a java class extends HttpServlet , there is a need to either overrider doGET(), or doPOST(); depending on the whether the data is been sent by GET or POST method

doPOST takes two arguments as

public class Test extends HttpServlet
{

    @Override
    public void doPost( HttpServletRequest request, HttpSevletResponse reponse ) throws ServletException, IOException
    {
// CODE HERE
    }

}

HttpServletRequest get the incoming information from the user OR in other word, it read the incoming information from ths user

HttpServletResponse get the outgoing information like HTTP status
codes (200, 404, etc.) and response headers (Content-Type, Set-Cookie, etc.). OR it return,send or write back the user request information
Creating a Java class that extends Servlet with hand
//STEP 1

//import the neccessary packages

import javax.servlet.*; // For HttpServlet , HttpServletResponse, HttpServletRequest e.t.c
        import java.io.*; // For Printwriterand ,BufferedReader, FileWriter e.t.c
        import javax.servlet.http.*;

public class GetTest extends HttpServlet
{

    @Override

    public void doGet( HttpServletRequest request, HttpServlet response ) throws Exception
    {
        PrintWriter display = response.getWriter();
        disply.println("Hello servlet");
    }

}

To specify your own url , simply use @WebServlet(“/myurl”) before the class declaration

I hope this makes your day better. Why not reading Empty != null?

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: