Sponsered Links
Categories
Sponsered Links

Introduction to servlet

 

Initially, Common Gateway Interface (CGI) scripts were the main technology used to generate dynamic content by adding functionality to a Web Server. As it has a number of demerits including platform dependence and lack of scalability, therefore to overcome these limitations, Java Servlet technology was introduced as a portable way to provide dynamic, user-oriented content.

Introduction to Servlet

Java Servlets are the modules of Java code that dynamically receives a request and generates response. Servlets extend the applications hosted by Web servers, although they can respond to any type of request. Java Servlet technology defines HTTP-specific servlet classes to develop such application. The javax.servlet and javax.servlet.http packages provide interfaces and classes for writing servlets. All servlets must implement the Servlet interface, which defines life-cycle methods. Servlets can maintain state across many server transactions by using HTTP cookies, session variables or URL rewriting.

The Java package javax.servlet contains the Servlet API that defines the interactions of a Web container and a servlet. A Web container is the component of a Web server which is responsible for managing the lifecycle of servlets, mapping a URL to a particular servlet and ensuring that the URL requester has the correct access rights. 

The Java Servlet API use the Java platform to add dynamic content to a Web server  The generated content may be HTML or XML, When implementing a generic service, you can use or extend the GenericServlet class provided with the Java Servlet API. The HttpServlet class provides methods, such as doGet and doPost, for handling HTTP-specific services.

You can use a number of Web servers with the Servlet like including Apache’s Tomcat Server, Macromedia’s JRun, IBM’s Weblogic and BEA’s Websphere.

Advantages of Servlets:

Servlets are the java programs that run on a Web server through requests and responses and build Web pages. Java servlets are more efficient, easier to use, more powerful,  platform-independent  that extends the functionality of a Web server. It is having following advantages:

  •  Servlet removes the overhead of creating a new process for each request, as it does not run in a separate process.

  •  A Servlet need not to be loaded and started for each request, as it stays in memory between requests.

  •  There is only a single Servlet instance which generates responses to all requests concurrently. This saves memory and allows a Servlet to easily manage persistent data.

  • A Servlet can be run by a Servlet Engine which allows secure use of Servlets.

  • Servlets are written in the highly portable Java language and follow a standard framework, they provide a means to create sophisticated server extensions in a server and operating system independent way.

First Java Servlet Example

package com.worldinfosoft.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.*;
import javax.servlet.http.*;

public class ServletExample extends HttpServlet {

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

		res.setContentType("text/html");

		PrintWriter out = res.getWriter();

		out.println("<html><head>");
	out.println("<title>TestServlet</title>");
	out.println("\t<style>body { font-family: 
                     'Lucida Grande', " + "'Lucida Sans 
                Unicode';font-size: 13px; }</style>");
		out.println("</head>");
		out.println("<body>");
		
		out.println("<p>Current Date/Time: " +
			new Date().toString() + "</p>");
		out.println("</body></html>");

		out.close();
	}
}

 
 
Sponsered Links
Latest Updates
 
All Content of this site is for learning only. We do not warrant the correctness of its content. The risk from using it lies entirely with the user. While using this site, you agree to have read and accepted our terms of use and privacy policy.
Copyright © 2009 JSPSERVLETTUTORIAL.INFO All Right Reserved