GET AND POST REQUEST IN JAVA SERVLET

SEND FORM REQUEST IN JSP

Today I am working on JSP(Java servlet page). this is very basic demo of POST and GET request in JSP.

first need to define a class in you folder (src in eclispe).

“How T0 Send GET Request”

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(“/hitesh”)
public class hitesh extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    public hitesh() {
        super();
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setContentType(“text/html”);
        PrintWriter printwrite = response.getWriter();
        String title = “Reading three request Parameter”;
        String docType =
                  “<!doctype html public \”-//w3c//dtd html 4.0 ” +
                  “transitional//en\”>\n”;
        printwrite.println(docType+”<html><head><title>XX</title></head>”+
                “<BODY BGCOLOR=\”#FDF5E6\”>\n”+
                “<H1 ALIGN=CENTER>”+title+”</H1>”+
                “<UL>\n”+
                “<li><b>FirstName</b>:”+request.getParameter(“firstname”)+”\n”+
                “<li><b>Last Name</b>:”+request.getParameter(“lastname”)+”\n”+
                “</ul>\n”+
                “</body></html>”);          
    }
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}

Now Make HTML Page and make link with your servlet page like this:-

<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″>
<title>Insert title here</title>
</head>
<body>
<h1>Hello Hi I ma hitesh</h1>

<form action=”hitesh” method=”GET”>
    <input type=”text” name=”firstname”>
    <input type=”text” name=”lastname”>
    <input type=”submit” value=”ADD”>
</form>
</body>
</html>

Now last step run your TOMCAT server and run this example on server and check the output.

“How To Send Post Request”

make small change in servlet like this.

 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

doGet(request,response); //Only call doGet method and pass the request and response
}

Now make the change in html Form request method like this

<form action=”hitesh” method=”POST”>
    <input type=”text” name=”firstname”>
    <input type=”text” name=”lastname”>
    <input type=”submit” value=”ADD”>
</form>

Now run this code and see the result.

Good Luck 🙂

cheers….

 

 

 

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