Contact Us

First servlet example

AddThis Social Bookmark Button          AddThis Feed Button
Print   Email

Servlet is way to replacement of CGI technology. Servlet works on request and response system with http client and server side. Servlet take request and give it to server and server process, request and send back response to http browser or client. Here we have a servlet example. How to write first servlet, This servlet examples helps to write servlet. Write this servlet code in nodepad of or any editor and save this servlet example with java extension.



This first servlet java file should be saved in webapps of tomcat in your application context in WEB-INF in classes and compile it, Run javac command in command prompt. Servlet does not contain main method. So servlet can not run on java directly, mean like simple java program, servlet can not execute on command prompt. You can execute servlet on web browser, but servlet class file should be in tomcat web server of WEB-INF/classes folder. When you try compile first servlet example,

classpath of tomcat/lib/servlet-api.jar jar file should be set.




First Servlet Example

// Java servlet Document

import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*; 

public class first extends HttpServlet
    public void doGet(HttpServletRequest req,HttpServletResponse res)
     throws ServletException,IOException
        PrintWriter out = res.getWriter();
        out.println("this is First servlet Example ");


first servlet can be executed on web browser with



Another Servlet Example

This servlet uses post method instead of get method. But we need get method also



import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class SecondServlet extends HttpServlet {

    public void init() throws ServletException 
        ///// Can make here database connection
        ///// This init method once for a servlet
    public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
        out.println(" <HEAD><TITLE>A Second Servlet</TITLE></HEAD>");
        out.println(" <BODY>");
        out.print(" This is ");
        out.println(", using the POST method");
        out.println(" </BODY>");
    public void doGet(HttpServletRequest request,HttpServletResponse response) 
     throws IOException,ServletException {
        doPost(request, response);
    public void destroy() {
            ////// Close all database connnection here
            ///// Called when servlet closing



  follow EasyWayServer  
  HTML TagsNew  
JSP Tutorials New
Learn JSP in very less time with example and output. No more headache to scan books about JSP
JDBC Tutorials New
Learn JDBC in very less time with example.
Tomcat Clustering
Clustering is technique to improve the performance, high scalability, and failover of web-server or any servers. Number of users increase with increase load on the servers
Implementation of tomcat clustering
Simple steps to do tomcat clustering with code
Tomcat Installation & Configuration
Tomcat installation on windows, environment system variables setting in Visual mode for easy installation and configuration of web.xml other servlet invoker setting
Apache Tomcat integration with mod_jk
Integrate Apache web server with tomcat web server with mod_jk
Java Servlet Examples
Learn first servlet by example
Apache installation
Apache installation on windows and linux
Mysql Installation
Mysql installation on windows, Port setting,installing mysql tools query browser, connecting to database,creating database, creating tables
Apache PHP MySql integration

Keep up-to-date with all the latest articles, tutorials, reviews by subscribing to our newsletter.

Enter your email address:

Link to Us
If You Like this Article, share this

More About servlet
Details of Servlet
Java API 1.5
A servlet Tutorial
Sun Java Home page
Java API 1.4.2 Documentation
Struts Core 1.3.9 API
JavaServer Faces (JSF)
Tag Library Documentation
    Privacy Policy