How can we use GenericServlet and HTTPServlet class in writing servlets? Explain with example.

  GenericServlet Class

GenericServlet class implements Servlet, ServletConfig and Serializable interfaces. It provides the implementaion of all the methods of these interfaces except the service method. GenericServlet 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.

Methods of GenericServlet class

There are many methods in GenericServlet class. They are as follows:

public void init(ServletConfig config)// is used to initialize the servlet.

public abstract void service(ServletRequest request, ServletResponse response) //provides service for the incoming request. It is invoked at each time when user requests for a servlet.

public void destroy() //is invoked only once throughout the life cycle and indicates that servlet is being destroyed.

public ServletConfig getServletConfig()// returns the object of ServletConfig.

public String getServletInfo() //returns information about servlet such as writer, copyright, version etc.

public void init() //it is a convenient method for the servlet programmers, now there is no need to call super.init(config)

public ServletContext getServletContext() //returns the object of ServletContext.

public String getInitParameter(String name)// returns the parameter value for the given parameter name.

public Enumeration getInitParameterNames()// returns all the parameters defined in the web.xml file.

public String getServletName()// returns the name of the servlet object.

public void log(String msg)// writes the given message in the servlet log file.

public void log(String msg,Throwable t) //writes the explanatory message in the servlet log file and a stack trace.


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("<b>Gello Generic Servlet</b>");

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

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

}

}

Output

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


HttpServlet Class

The HttpServlet class extends the GenericServlet class and implements 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 DemoServlet extends HttpServlet

{

public void doGet (HttpServletRequest req, HttpServletResponse res) ServletException, IOException

throws

{

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 web browser..


Comments

Popular posts from this blog

Suppose that a data warehouse consists of the three dimensions time, doctor, and patient, and the two measures count and charge, where a charge is the fee that a doctor charges a patient for a visit. a) Draw a schema diagram for the above data warehouse using one of the schemas. [star, snowflake, fact constellation] b) Starting with the base cuboid [day, doctor, patient], what specific OLAP operations should be performed in order to list the total fee collected by each doctor in 2004? c) To obtain the same list, write an SQL query assuming the data are stored in a relational database with the schema fee (day, month, year, doctor, hospital, patient, count, charge)

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?