What are different ways of writing servlet programs ? Write a sample Servlet program using any one way.

  Writing Servlets Program

There are three ways to create the servlet

1. By implementing the Servlet interface

2. By inheriting the GenericServlet class

3. By inheriting the HttpServlet class

1. By implementing the Servlet interface/Servlet Interface

The Servlet interface provides common behavior to all the servlets. The servlet interface needs to be implemented for creating any servlet (either directly or indirectly). It provides 3 life cycle methods that are used to initialize the servlet, service the requests, and destroy the servlet, and 2 non-life cycle methods that are used to get servlet information and servlet configurations.

Example

import java.io.*;

import javax.servlet.*;

public class ServletInt implements Servlet {

ServletConfig config=null;

public void init(ServletConfig config) {

this.config=config;

System.out.println("servlet is initialized");

}

public void service(ServletRequest req, ServletResponse res) throws

IOException, ServletException {

res.setContentType("text/html");

Printwriter out=res.getWriter();

out.print("<html><body>");

out.print("<b>Hello simple servlet</b>");

out.print("</body></html>");

}

public void destroy() {System.out.println("servlet is destroyed"); }

public ServletConfig getServletConfig(){return config;}

public String getServletInfo(){return "Implementing Servlet Interface";}

}

Output

When we execute this program we will see the message “ Hello simple servlet” in web browser.


2. By inheriting the GenericServlet class/GenericServlet Class

GenericServlet class implements Servlet, ServletConfig and Serializable interfaces. It provides the implementation of all the methods of these interfaces except the service method. Generic Servlet class can handle any type of request so it is protocol-independent. We can create a generic servlet by inheriting the GenericServlet class and providing the implementation of the service method.

Example

import java.io.*;

import javax.servlet.*;

public class GenServlet extends Genericservlet {

public void service(ServletRequest req, ServletResponse res)throws

IOException, ServletException{

res.setContentType("text/html");

PrintWriter out=res.getWriter();

out.print("<html><body>");

out.print("<b>Hello Generic Servlet</b>");

out.print("</body></html>");

}

}

Output

When we execute this program we will see the message “Hello Generic Servlet" in the web browser.


3. By inheriting the HttpServlet class/HttpServlet Class

The HttpServlet class extends the Generic Servlet class and implements a Serializable interface. It provides http-specific methods such as doGet, doPost, etc. doGet() method handles the GET request. And doPost() handles the POST request. Get request is the default request.

Example

import javax.servlet.http.*;

import javax.servlet.*;

import java.io.*;

public class HttpServletExample extends HttpServlet {

public void doGet(HttpServletRequest req, HttpServletResponse res) throws

ServletException, IOException {

res.setContentType("text/html"); //setting the content type

Printwriter pw=res.getWriter();//get the stream to write the data

//writing html in the stream

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

pw.println("Welcome to servlet");

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

pw.close();//closing the stream

}

}

Output

When we execute this program we will see the message “Welcome to Servlet” in the web browser.

Comments

Popular posts from this blog

Suppose that a data warehouse for Big-University consists of the following four dimensions: student, course, semester, and instructor, and two measures count and avg_grade. When at the lowest conceptual level (e.g., for a given student, course, semester, and instructor combination), the avg_grade measure stores the actual course grade of the student. At higher conceptual levels, avg_grade stores the average grade for the given combination. a) Draw a snowflake schema diagram for the data warehouse. b) Starting with the base cuboid [student, course, semester, instructor], what specific OLAP operations (e.g., roll-up from semester to year) should one perform in order to list the average grade of CS courses for each BigUniversity student. c) If each dimension has five levels (including all), such as “student < major < status < university < all”, how many cuboids will this cube contain (including the base and apex cuboids)?

Suppose that a data warehouse consists of the four dimensions; date, spectator, location, and game, and the two measures, count and charge, where charge is the fee that a spectator pays when watching a game on a given date. Spectators may be students, adults, or seniors, with each category having its own charge rate. a) Draw a star schema diagram for the data b) Starting with the base cuboid [date; spectator; location; game], what specific OLAP operations should perform in order to list the total charge paid by student spectators at GM Place in 2004?

What is national data warehouse? What is census data?