Writing a Servlet Class

Writing a Servlet Class


Posted in : Java Posted on : June 6, 2011 at 5:20 PM Comments : [ 0 ]

In this section we will show how can you start to write a sample Servlet class in Java. We will discuss it step-by-step.

Writing a Servlet Class

In this section we will show how can you start to write a simple Servlet class in Java. We will discuss it step-by-step.

To write a simple Servlet ServletDemo class please follow these following steps :

  • Choose your editor where you want to edit new file and named it, Here I am giving the name as ServletDemo.java

  • Write the following statements.

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

public class ServletDemo extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("<html>");
out.println("<body bgcolor='red'>");
out.println("<h2>My First Servlet Program</h2>");
out.println("</body>");
out.println("</html>");
}
}
  • Now edit the web.xml file

<?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>
<servlet-name>SerDemo</servlet-name>
<servlet-class>ServletDemo</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>SerDemo</servlet-name>
<url-pattern>/servlet/ServletDemo</url-pattern>
</servlet-mapping>
</web-app>

In the above example the first three lines of importing java packages helps in reduce to write the java classes in java program.

java.io package povides the input and output facilities, this package is a standard package of core java platform. Here we have used the PrintWriter class of this package which object is used for showing output. And the other two packages javax.servlet.* & javax.servlet.http.* are the Servlet API and the J2EE platform packages. The servlets, general classes and interfaces are provided by the javax.servlet package whereas, the http specific classes and interfaces are provided by the javax.servlet.http.

javax.servlet :- ServletException class is contained by this package. Every servlet throws the servlet exception therefore it is necessary to include this package in your program.

javax.servlet.http  :- HttpServletRequest and HttpServletResponse interfaces are contained by this package. The HttpServletRequest and HttpServletResponse objects created by the servlet container which is passes through the service method doGet(), doPost(), etc. to get servlet request and send servlet response to the client respectively.

HttpServlet :- This is an abstract class of which you must have to extend in each of your Servlet class. This class contains some methods of which at least one of them must be overridden these are doGet(), doPost(), doPut(), doTrace(), doDelete() etc. :

  • doGet() :- HTTP Get operation is performed by this method it reads data from the request.
  • doPost() :- HTTP Post operation is performed by this method it reads data from the request and writes data to response using servlet output stream.
  • doPut() :- HTTP Put operation is performed by this method it sends a file via FTP.

  • Now select your server and compile the above program

Output :

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics