Servlet Example

Servlets are used to create dynamic web application in java.javax.servlet packages provides the interfaces and clases. They are not specific to any protocol. javax.servlet.http package contains interfaces and classes processes for http requests.

Servlet has following life cycle methods

  • init() : This method is invoked only once which and used for initialization of servlet.
  • service(): This method is invoked at each time while a request comes for the servlet.
  • destroy():This method is invoked only once and used to do clean up operations.

The HttpServlet class extends the GenericServlet class and implements Serializable interface.

Commonly used methods of HttpServlet

  • protected void doGet(HttpServletRequest req, HttpServletResponse res) :- It handles the GET request.
  • protected void doPost(HttpServletRequest req, HttpServletResponse res) :- It handles the POST request

Here we are taking simple example for HTTP servlet using Eclipse IDE.

servlet-ref

Steps to create web application using Eclipse IDE.

  • Create a dynamic web application
  • Add servlet-api.jar and jsp-api.jar to the WEB-INF/lib.
  • Create a package under src directory
  • Add a Servlet to the package the package by right click on package and choose the New->Servlet.
  • Create a index.jsp under WebContent directory
  • Now finally create a web.xml file under WEB-INF directory and add the following entry to the servlet.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<servlet>
<description></description>
<display-name>HelloWorldServlet</display-name>
<servlet-name>HelloWorldServlet</servlet-name>
<servlet-class>com.startwithjava.HelloWorldServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorldServlet</servlet-name>
<url-pattern>/HelloWorldServlet</url-pattern>
</servlet-mapping>
</web-app>

index.jsp


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Servlet Example</title>
</head>
<body>
<h1>Servlet Example by StartWithJava</h1>
<form action="HelloWorldServlet" method="post">
<input type="text" name="name">
<input type="submit" value="Submit">
</form>
</body>
</html>

HelloWorldServlet.java


package com.startwithjava;

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;

/**
* Servlet implementation class HelloWorldServlet
*/
public class HelloWorldServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public HelloWorldServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//getWriter() method of HttpServletResponse is used to generate response
PrintWriter out = response.getWriter();
//getParameter() method of HttpServletRequest
String name = request.getParameter("name");
out.println("<b>Welcome "+name+"</b>");
}

}

Now deploy the web application on the server.Import war file using Eclipse IDE.You can access the application using following url.

http://localhost:8080/ServletExample/

Download War file.

Leave a Reply