Wednesday, January 18, 2012

Sowmya, Chandra Batch-1

Understanding the structure of web applications

Web Applications use a standard directory structure defined in the Servlet specification. When developing web applications on J2EE platform, you must follow this structure so that application can be deployed in any J2EE compliant web server. (Tomcat, Weblogic, Jboss, websphere etc.. )
A web application has the directory structureas shown in below figure.

J2EE Web Application Directory Structure
The root directory of the application is called the document root. Root directory is mapped to the context path. Root directory contains a directory named WEB-INF. Anything under the root directory excepting the WEB-INF directory is publically available, and can be accessed by URL from browser. WEB-INF directory is a private area of the web application, any files under WEB-INF directory cannot be accessed directly from browser by specifying the URL like http://somesite/WEB-INF/someresource.html. Web container will not serve the content of this directory. However the content of the WEB-INF directory is accessible by the classes within the application. So if there are any resources like JSPs or HTML document that you don’t wish to be accessible directly from web browser, you should place it under WEB-INF directory.

WEB-INF directory structure

WEB-INF directory contains
  • WEB-INF/web.xml deployment descriptor
  • WEB-INF/classes directory  --> all class files unders this folder loaded by tomcat sercer to memory
  • WEB-INF/lib directory

/WEB-INF/web.xml

web.xml is called the web application deployment descriptor. This is a XML file that defines servlets, servlet mappings, listeners, filters, welcome files etc. Deployment descriptor is a heart of any J2EE web application, so every web application must have a web.xml deployment descriptor directly under WEB-INF folder.

/WEB-INF/classes

The classes directory is used to store compiled servlet and other classes of the application. If your classes are organized into packages, the directory structure must be reflected directly under WEB-INF/classes directory. The classes directory is automatically included in CLASSPATH.

/WEB-INF/lib

Lib directory is used to store the jar files. If application has any bundled jar files, or if application uses any third party libraries such as log4j, JDBC drivers which is packaged in jar file, than these jar files should be placed in lib directory.
All unpacked classes and resources in the /WEB-INF/classes directory, plus classes and resources in JAR files under the /WEB-INF/lib directory are included in classpath and made visible to the containing web application.
There are many changes in Servlet 3.0 Specification, web.xml file is optional, servlets, listeners and filters are no longer required to be defined in web.xml, instead they can declared using annotations. web.xml file can be devided into multiple files called webfragments.
Note Directory and file names are case sensitive.
Simple Servlet example

  1. How to write the servlet class.
  2. How to compile the Servlet class
First of all we need to create the directory structure for our sample web application as explained in the above tutorial. Create the directory structure as shown in below figure.




package jsptube.tutorials.servletexample;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class WelcomeServlet extends HttpServlet {

@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
}


protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

System.out.println("This is the frist Servlet ");


}


public void destroy() {

}
}

Now compile the servlet class as explained below.
Open the command prompt and change the directory to the servlet-example/WEB-INF/src/jsptub/tutorials/servletexample directory. Compile the WelcomeServlet.java using the following command. javac WelcomeServlet.java It will create the file WelcomeServlet.class in the same directory. Copy the class file to classes directory. All the Servlets and other classes used in a web application must be kept under WEB-INF/classes directory.
Note: to compile a servlet you need to have servlet-api.jar file in the class path.
The deployment descriptor (web.xml) file.
Copy the following code into web.xml file and save it directly under servlet-example/WEB-INF directory.

<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 
 <servlet>
  <servlet-name>WelcomeServlet</servlet-name>
  <servlet-class>jsptube.tutorials.servletexample.WelcomeServlet</servlet-class>
 </servlet>
 <servlet-mapping>
  <servlet-name>WelcomeServlet</servlet-name>
  <url-pattern>/WelcomeServlet</url-pattern>
 </servlet-mapping>
 <welcome-file-list>
  <welcome-file> /pages/form.html </welcome-file>
 </welcome-file-list>
</web-app>

Our sample application can be accessed at http://localhost:8080/Servlet-example/WelcomeServlet. If tomcat server is running on port other than 8080 than you need to change the URL accordingly.

Access the servlet from the HTML form SUBMIT button also.

write index.html as below

<html>
<body>
<form action="./WelcomeServlet" >

<input type="submit" value="Click me" />

</form>
</body>

1. open the index.html from the browser  ervlet-example/http://localhost:8080/Servlet-example/index.htmlindex.html
2. click on the Button, we will see that Servlet gets executed.
3. Tomcat console displasys the SOP that we have implemented in the doGET method.

No comments:

Post a Comment