×
☰ See All Chapters

What is Java Servlet

Servlet is an API. Servlet is a class. Servlet is a component. Servlet is web technology. Servlet is a web-technology from SUN Microsystems which will be used to develop web-based applications. Servlets is used to develop server side components. Servlet is a sub specification of JEE platform which is set by SUN, implemented by various server vendors and used by java developers. And many more definitions can be given to servlet.  At this time this may become confusing from all the above definitions. But once you have learnt servlet concepts you will be well clear about the definitions.

How to create HTTP servlets

We have to extend HttpServlet servlet class to create an HTTP servlet suitable for a Web site. A subclass of HttpServlet must override at least one method, usually one of these:

  1. doGet, if the servlet supports HTTP GET requests 

  2. doPost, for HTTP POST requests 

  3. doPut, for HTTP PUT requests 

  4. doDelete, for HTTP DELETE requests 

  5. init and destroy, to manage resources that are held for the life of the servlet 

  6. getServletInfo, which the servlet uses to provide information about itself 

Most of the time we override either doGet or doPost

Difference between GET and POST

Get

Post

When a request is sent using GET method, data will be attached to the URL as a query string.

When a request is sent using POST method, data will be sent to the server along with the Http Request body.

Size of URL is limited, because of this we can send limited amount of data using GET method.

Using POST we can send unlimited amount of data because the body can take any number of characters, size is not fixed.

GET method is not secure because of attaching the data in the URL which can be seen by the user.

POST method is secure because it carries the data in the request body and does not exposes the data in the URL.

GET is used for bookmarking.

POST is used for carrying secure data and cannot be used for bookmarking.

 

You will understand the above differences after you learn to submit data from HTML form, this will be studied in coming chapters.

package com.java4coding;

 

import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

public class FirstServlet extends HttpServlet {

        protected void service(HttpServletRequest arg0, HttpServletResponse arg1)

                        throws ServletException, IOException {

                super.service(arg0, arg1);

        }

 

        public void doPost(HttpServletRequest req, HttpServletResponse res)

                        throws ServletException, IOException {

                res.setContentType("text/html");

                PrintWriter pw = res.getWriter();

                pw.println("<html>");

                pw.println("<body bgcolor='pink'>");

                pw.println("<h1>");

                pw.println("Hello World");

                pw.println("<br>");

                pw.println("This is my first Servlet Program");

                pw.println("</h1>");

                pw.println("</body>");

                pw.println("</html>");

        }

 

        public void doGet(HttpServletRequest req, HttpServletResponse res)

                        throws ServletException, IOException {

                res.setContentType("text/html");

                PrintWriter pw = res.getWriter();

                pw.println("<html>");

                pw.println("<body bgcolor='pink'>");

                pw.println("<h1>");

                pw.println("Hello World");

                pw.println("<br>");

                pw.println("This is my first Servlet Program");

                pw.println("</h1>");

                pw.println("</body>");

                pw.println("</html>");

        }

}

 


All Chapters
Author