How to write HTML page to invoke a servlet

This post will tell you how to write a client end HTML file that will call your  back end servlet.
This post is further enhancement of this concept.

The simple HTML code can be…..

<meta http-equiv=”Content-Type” content=”text/html; charset=windows-1252″></meta>
        <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”/>

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 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 {
    /**Process the HTTP doGet request.
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String data=request.getParameter(“text”);
        PrintWriter out = response.getWriter();
        out.println(“<p>The servlet has received a GET. This is the reply.Your data is </p>”+data);

See also  Learn Servlets in an Easy and Fast way

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


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

Share and Enjoy !

One thought on “How to write HTML page to invoke a servlet

  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.

Leave a Reply

Your email address will not be published. Required fields are marked *