Tuesday, April 15, 2008

Difference between a Web server and Application Server

Difference between a "Web server" and "Application Server"
A Web server handles the HTTP protocol. When the Web server receives an HTTP request, it responds with an HTTP response, such as sending back an HTML page. To process a request, a Web server may respond with a static HTML page or image, send or redirect, or delegate the dynamic response generation to some other program such as CGI scripts, JSPs (JavaServer Pages), servlets, ASPs (Active Server Pages), server-side JavaScripts, or some other server-side technology. Whatever their purpose, such server-side programs generate a response, most often in HTML, for viewing in a Web browser.The web server simply passes the request to the program to handle it. The web server does not provides any functionality beyond providing an environment in which the server - side program can execute and pass back the generated responses. The server-side program usually provides such functions as transaction processing, database connectivity and messing .
Application Server:
While an application server exposes business logic to client applications through various protocols like HTTP, TCP-IP etc. All the web servers mainly deals with sending HTML for displaying to a Web browser. An application server providers allows the client to access the business logic for use. The application server is used to run business logic or dynamically generating presentation code. A J2EE application server runs servlets and JSPs that are used to create HTML pages dynamically. In this case, J2EE application server can run EJBs – which are used to execute business logic. An application server is more capable of dynamic behaviour than webserver.

How to Run a Servlet?

How to Run a Servlet?
To run a servlet one should follow the steps illustrated below:
  • Download and Install the tomcat server: Install the tomcat server in a directory in which you want to install and set the classpath.for the variable JAVA_HOME in the environment variable
  • Set the class for the jar file: Set the classpath of the servlet-api.jar file in the variable CLASSPATH inside the environment variable by using the following steps.For Windows XP,
    Go to Start->Control Panel->System->Advanced->Environment Variables->New button and Set the values asVariable Name: CLASSPATH Variable Value: C:\Program Files\Java\Tomcat 6.0\lib\servlet-api.jarFor Windows 2000 and NT
    Go to Start->Settings->Control Panel->System->Environment Variables->New button and Set the values asVariable Name: CLASSPATH Variable Value: C:\Program Files\Java\Tomcat 6.0\lib\servlet-api.jar
  • Create a java source file and a web.xml file in a directory structure.
  • Compile the java source file, put the compiled file (.class file) in the classes folder of your application and deploy the directory of your application in the webapps folder inside the tomcat directory.
  • Start the tomcat server, open a browser window and type the URL http://localhost:8080/directory (folder name of your application) name/servler name and press enter.
  • If everything is correct your servlet will run.

The Servlet Class Hierarchy

The Servlet Class Hierarchy
The most basic servlet definitions live in the javax.servlet package and consist of a number of interfaces.
  • The ServletContext interface provides a means for servlets to communicate with the surrounding Web server or application server. This communication can take the form of requests for system resources, reports written by the servlet to a log file, and so on. Indirectly, the ServletContext also allows servlets to communicate with one another, primarily by sending requests to other pages. This is how the jsp:forward and jsp:include tags are implemented, as will be seen shortly. ServletContext is implemented by people writing the Web or application server; servlet authors seldom need to use it directly and never need to extend it.
  • The ServletConfig interface provides a mechanism for the web Server to pass initialization information to the servlet's init() method. This information takes the form of pairs of names and values, which are stored in a configuration file called web.xml. If a servlet is going to open a connection to a database, it would not make sense to hard-code the name of the driver class and the database URL in the servlet's code. Doing so would make the servlet more difficult to change if a new database were ever installed. Instead, this information could be sent to the servlet as parameters, and the servlet would use the ServletConfig to retrieve these values and act accordingly. Like ServletContext, this interface is implemented by the authors of the Web server.
  • The Servlet interface defines the three life-cycle methods—init(), service(), and destory()—as well as a handful of others.
  • The ServletRequest and ServletResponse interfaces encapsulate a request to the servlet and a response from the servlet, respectively. Objects that implement these interfaces will be passed to the servlet's service() method. Code within this method can then use the ServletRequest to determine information about the request, such as its origin, the exact data being requested, and so on. Similarly code in the service() method can then use the ServletResponse to return information about the response, as well as the data, such as an HTML page, that comprises the response itself.
  • The javax.servlet package also defines three other classes. Two are the ServletInputStream and ServletOutputStream classes, which servlets use to read and write data, respectively. The third class, GenericServlet, implements both the Servlet and ServletConfig interfaces and forms the basis for most real servlets.

Wednesday, December 19, 2007

What are Java Server Pages?

JavaServer Pages (JSPs)
JavaServer Pages (JSPs) are based on the concept of server-side parsing. A JavaServer Page is an HTML page with Java statements embedded in it. The JSP specification defines certain special tags that can be embedded within the HTML page. In a normal working scenario, the JSP pages are parsed by a JSP compiler within the application server and converted into a Java servlet with the generated code embedded within the Java servlet as a set of Java statements. The generated Java servlet is then compiled as a normal servlet and loaded into the Java servlet container. The Java servlet is then used to process the user requests.


The JSP specification defines the following set of tags that can be used by application component providers:

  • Directive tag— A directive tag embedded in a JSP is used to issue directives to the JSP compiler that compiles the JSP.
  • Declaration tag— A declaration tag can be used by the application component provider to define variables or methods that need to be put outside the service() method of the final generated Java servlet.
  • Expression tag— In order to use Java values or variables directly in the JSP, you can use the expression tag.
  • Scriptlet tag— The scriptlet tag is the most important tag in the JSP specification because it enables the actual embedding of Java code within an HTML page. An application component provider can write entire processing routines within the scriptlet tags.
  • Custom tag— The final set of tags that the JavaServer Page specification defines is the embedding of custom tags. Because the JSP compiler cannot process these tags on its own, a special set of classes called the JSP tag library must be built by the application component provider to support the processing of the custom tags.