Skip to main content

Introduction to Java Servlets

In order to understand the advantages of servlets , you must have the basic under standing of how web browser and web servers are cooperate to provide content to the client . For sake of argument , consider a request for a static web page. The user enters a Uniform Resource Locator( URL) . Then the browser generate a HTTP request for the appropriate server ; the server redirect to the request to the index file of the specified web site . That file return a HTTP response to the client .HTTP header in the HTTP response indicates that the content type of the response. Multipurpose Internet Mail Extension(MIME) is used to the same purpose. for example HTML(Hypertext Markup language ) have a mime type of text/html

       In the early days of web, the web server construct a dynamic web page by creating separate process to handle simultaneous request from the client .the process need to open one or more connection to the database to obtain the necessary information. It is communicate with the server using a interface called Common Gateway Interface (CGI). CGI allow process to read data from http request and write data to the http response as well.CGI program can written in a variety of language like c cpp perl and so on.
   

    A Java Servlet is server side program that called by user interface or by another web component and contain business logic to process a request. Java servlet are runt in the address space of the web server and act as a middle layer between request coming from the client or browser  and database or application resides in the server . Usually the business logics are written in servlet ie code related to database manipulation and as we know all other presentation code will be written in jsp .Using servlet you can collect data from user through web forms . display data from database and from other source as well and create web page dynamically .

   Servlets serve the same purpose as same as the program implementing CGI. But servlet offer some advantages over CGI.

-> It is significantly better performance
-> Platform independent since it is written in java
->  Servlets are run with in the address space of the server. There is no need to create separate process to handle simultaneous request from the client.   Mulithreading helps to servlet to do the same .
->Security manager in the server enforce some restrictions to protect the resource in the server machine. So we can trust servlet blindly .
->Full functionality of the java class library are available to the servlet . ie it can communicate with database , applet and other software through RMI,sockets and so forth .

Servlets are responsible to do the following tasks .

->  Read the explicit data from client(browser) . it may include data from web forms , applet or from http custom client .

-> Read the implicit HTTP Request data from client(browser). It may include content type ,cookies , compression schema which understand by the browser and so forth .

-> Process the data and generate the result or HTTP response . This process may need to talk with database may invoke RMI, CORBA ,or compute the request directly

->Send explicit data to the client(Browser). This data can be send to the client in a variety of forms like text(html,xml), excel ,binary (GIf image ).

-> Send implicit data to the client(Browser). This may include tell browser or client side application what type of data being send,Cookies , Caching parameters and things like that



Comments

Popular posts from this blog

JSP page directives

A jsp page directive looks like this: <%@ directive attribute="value" %> I am not gonna explain each and every page directives here . I would like to discuss about two page directives  which is import and include. First of all consider the import directive . The following simple program illustrate the use of import page directive: The output would be something looks like this: <%@ page language="java" contentType="text/html; charset=UTF-8"     pageEncoding="UTF-8"%>  <%@ page import="java.util.Date" %>   //page directive <!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=UTF-8"> <title>Jsp Basics</title> </head> <body> <%=new Date() %> </body> </html> Tue Nov 12 17:42:34 I...

a simple example for jdbc PreparedStatement

a simple example for PreparedStatement package basics.in.java.blogspot.in; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class Main { private static final String USERNAME="root"; private static final String PASSWORD=""; private static final String CONN_STRING="jdbc:mysql://localhost/basicsinjavablogspot"; public static void main(String[] args) throws ClassNotFoundException, SQLException { Class.forName("com.mysql.jdbc.Driver"); Connection conn=null; Statement stmt=null; ResultSet rs=null; try { conn= DriverManager.getConnection(CONN_STRING, USERNAME, PASSWORD); System.out.println("database connection successful"); //stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); String sql="select * fr...