Showing posts with label Servlet and JSP. Show all posts
Showing posts with label Servlet and JSP. Show all posts

Tuesday, January 1, 2013

Adding the executing order for the Servlet 3 Annotation based Filter classes

welcome to my first post o the year 2013 ;) and i today decided to investigate through servlet 3.0 features.  as you all are aware servlet 3 has a nice feature called annotations. therefore we can minimize the XML based declarations on the web.xml deployment descriptor. today i am going to show a simple demonstration on annotated servlet filters and how to add execution order for those servlet filters.

you can generate simple web application project with  following maven command.

mvn archetype:generate -DarchetypeArtifactId=maven-archetype-webapp

one the project is generate, make sure to check and alter the web application deployment descriptor based on the servlet 3 API version . i have post a article about deployment descriptors(web.xml) for different servlet versions   and you can refer that article if you dont know how to do that and what is the purpose of doing that.

after creating the project, add following servlet3 maven dependency for the pom.xml of your project.

then add the following two filters also. you can see that here we are using the annotated servlet filters. 
 FilterOne.java  
FilterTwo.java  

if you need you can add the following servlet class also.  
HelloServlet.java 

you can deploy the web application in the tomcat and access the web application as follows.

http://localhost:8080/sample-servlet3-webapp/hello

then you will get the following result(check the application loggers).
inside the filter one
inside the filter two 
this servlet will be executed after executing the all relevant filters  according to their order 

you can get the full source code from my GitHub account Download Source code through GitHub

Tuesday, July 17, 2012

web.xml deployment descriptor for diffrent sevlet versions

today i am gong to discuss the different web.xml deployment descriptors available for each servlet API versions. hope this might be helpful for you to decide the correct web.xml DD definition base on the servlet API version you are using.

1. Servlet 2.3 Deployment Descriptor

For Servlet 2.3, using a dtd file to validate the XML content. Not recommend to use, consider upgrading to version 2.4 or 2.5.

P.S Maven 3′s quick start web app is still generating this

web.xml -> Namespace = none
<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >
 
<web-app>
  <display-name>Servlet 2.3 Web Application</display-name>
</web-app>
 
 

2. Servlet 2.4 deployment descriptor

For Servlet 2.4, using xsd to validate XML content, the most popular web.xml version.
web.xml -> Namespace = http://java.sun.com/xml/ns/j2ee
 
<web-app 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"
       version="2.4">

  <display-name>Servlet 2.4 Web Application</display-name>
</web-app>
 
 

3. Servlet 2.5 deployment descriptor

For Servlet 2.5, using xsd to validate XML content, from this version and onward, the namespace is changed.

web.xml -> Namespace = http://java.sun.com/xml/ns/javaee
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
       http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
       version="2.5">

  <display-name>Servlet 2.5 Web Application</display-name>
</web-app>
 
 

4. Servlet 3.0 deployment descriptor

For Servlet 3.0, with xsd validation also, the latest version, but not many people using it.

web.xml -> Namespace = http://java.sun.com/xml/ns/javaee
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
       http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
       version="3.0">

  <display-name>Servlet 3.0 Web Application</display-name>
</web-app>
 
 
 
hope this will helpful for you !!!


Regards
Chathuranga Tennakoon
chathuranga.t@gmail.com

references 
http://www.mkyong.com/web-development/the-web-xml-deployment-descriptor-examples/

Monday, August 8, 2011

Solving Package javax.servlet does not exist Error using Eclipse and Ant


Suppose you are working on a web application project with Eclipse IDE, Apache ant and Tomcat server. When you compile and run the program you will get the following error saying that Servlet.jar API cannot be found.

Package  javax.servlet  does not exist

This error will get even if the servlet.jar API is available in the lib directory of the Tomcat web container. The reason for this error is you have not set the CLASSPATH environmental variable for referring javax.servlet API. In addition you have not set the Apache Ant run time class path to javax.servlet API.
You can follow the instructions given below to set both the classpaths properly.

1. Setting up CLASSPATH environmental variable.

 Right Click on My Computer -> Properties -> Advanced tab -> Environment Variables
(In that you can amend/create CLASSPATH environment variable to servlet.jar
(In my case, CLASSPATH=C:/Program Files/Apache Software Foundation/Tomcat 6.0/lib/javax.servlet.jar )

2. Setting up Apache Ant runtime classpath

In addition, it is required to include the javax.servlet API (servlet.jar ) into the Apache Ant runtime. This can be done using your Eclipse IDE as follows.
Click on Window menu -> preferences -> Ant -> Runtime -> Global Entries
(In this location, you can add the servlet.jar API to the Apache Ant runtime as an external JAR file).


if you follow the above instructions properly,  you should be able to compile and deploy your web application without having such compilation error.

Monday, December 6, 2010

Servlet Life Cycle

As we know the conventional J2EE application consists of set of Servlet and JSP files. therefore it is good to get a proper understanding of the servlet and jsp including their role and lifecycle. today i am going to discuss the servlet life cycle and hope to discuss the JSP in the future post. therefore we will briefly look at the Lifecycle and the role of the servlet in this post.

The Responsibility of the Servlet

the major responsibility of the servlet is to take the client request and send back the response.that means the servlet will be responsible for implementing and the processing business logics. In traditional MVC based J2EE web applications, the servlet act as the Controller.

M (Model) :- contains of POJO classes/core java classes to represent real word entities.

V (View) :- contains a set of JSP pages to implement presentation logic for the user.

C (Controller) :- contains the servlets to implement and process business logics.


The lifecycle of the Servlet

the servlet lives in the servlet container. that means the life cycle of the servlet is handled by the servlet container.servlet container provide necessary services for the servlet from born to dead.Apache tomcat is one of the well known servlet/web container.

i will explain the life cycle of the servlet using the following example.

suppose user clicks the following URL in the web application.

http://localhost:8080/example/sampleServlet

1.) this request will initially come to the web container. then the web container will identify that the request is to invoke a servlet. Then the web container will create two seperate objects known as HttpServletRequest and HttpServletResponse Objects for that request (in this case the web container doesn't know the what the actual servlet is).

2.) after creating the HttpServletRequest and HtppServletResponse objects, the web container will figure out the actual servlet based on the request URL.then it will check whether the Servlet is initialized. if it is not initialized, the container will initialize the servlet first.(the servlet initialization process will bemethod parameters) discussed later)Assume that the servlet is initialized at the moment.the web container will invoke the service() method of the servlet by providing(as the HttpServletRequest and HttpServletResponse Objects created. finally the web container will create and allocate a seperate thread to serve for that particular request.


3.) the service() method will figure out which servlet method to invoke based on the HTTP request methods.(GET or POST). if it is HTTP GET request, then it is required to invoke doGet() method. if it is HTTP POST, then doPost() method should be implemented.The service method will invoke either doGet() or doPost() methods providing the HttpServletRequest and httpServletResponse objects gained.



The servlet initialization process

the servlet must be initialized before handing client requests (invoking service() method). the servlet initialization is done by the servlet container by invoking inti() method. the init method is called(or servlet initilaization is done) only once for the life time of the servlet.servlet intilaization process can be described as follows.

1.) web container loads and instantite( create the object of the servlet) the servlet class.

2.) the created object is still not a servlet object. it is just an object cretaed from the servlet class type.In order to become a servlet, it is required to grant the servlet priviledges for that object. it is done by invoking init() method on that object. init() method is called only once throughout the life time of the servlet.

3.) after completing the init() method, servlet is initialized. therefore it is possible to invoke service() method to handle client requests.it is recommneded the developers to not to override the init() method.but if you required you can override the init() method. in such case, make sure that  you have all the initialization logic(codes) for that servlet(eg:- database connection codes, other connection and resource handling codes)


destroy()

The servlet container invoke the destroy() method on the servlet object to make it eligible for garbage collection.after completing all the works with the servlet, we do not require the servlet object longer. at that time, the servlet container invoke the destroy() method on the servlet object for releasing all the resorces(de-allocating) allocated during the initialization process. the destroy() method is also called only once like the init() method.

hope this will helpful for you!



regards
Chathuranga Tennakoon
chathuranga.t@gmail.com