This post will tell you how to write a client end HTML file that will call your  back end servlet.
http://www.askqtp.com/2012/01/how-to-call-servlet-from-html-using.html
This post is further enhancement of this concept.

The simple HTML code can be…..

<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=windows-1252″></meta>
 </head>
    <body>
        <form action=”servlet2″ method=”get” enctype=”text/plain” name=”sample”>
            <input type=”text” name=”text” value=”text” maxlength=”20″ size=”20″/>
            <input type=”submit” name=”Submit” value=”Submit”/>
        </form>
     </body>
</html>

So here we are creating a HTML file. There is a form inside it . Within the form there is a textbox where we can give some input. There is a submit button which submits data to the server. The method “get” is to send the information to the server. The target servlet is servlet2.

The servlet will be…

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;
import javax.servlet.http.*;

public class Servlet2 extends HttpServlet {
    private static final String CONTENT_TYPE = “text/html; charset=windows-1252”;
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
    }
    /**Process the HTTP doGet request.
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType(CONTENT_TYPE);
        String data=request.getParameter(“text”);
        PrintWriter out = response.getWriter();
        out.println(“<html>”);
        out.println(“<head><title>Servlet2</title></head>”);
        out.println(“<body>”);
        out.println(“<p>The servlet has received a GET. This is the reply.Your data is </p>”+data);
        out.println(“</body></html>”);
        out.close();
    }
}

READ  What Is Single Directory Architecture For JSP or Serlet

as the request type is get , so doGet has to be configured…Here inside the doGet we are receiving the request which is thrown from your HTML from written above. The container which is carrying this textbox value is “text”. The code to get the value is

request.getParameter(variable_Name);

You are done!!!! try sending data from Client to server and get a response. 

1 COMMENT

  1. I don’t know if it’s just me or if perhaps everybody else
    encountering problems with your site. It appears like some of the
    text on your posts are running off the screen. Can someone else please comment and
    let me know if this is happening to them as well? This may be a issue with my internet browser because I’ve had this happen before.
    Kudos

LEAVE A REPLY

Please enter your comment!
Please enter your name here