Powered By Blogger

Tuesday, May 11, 2010

Interfaces

Friday, March 12, 2010

Servlet 3.0 in Java EE6

Java Servlet 3.0 API – JSR 315

Sun Microsystems released Java EE 6 specification and GlassFish v3 , THE Reference Implementation of Java EE6 on December 10th 2009

The list of supported technologies includes, but is not limited, by

  • Servlet 3.0
  • JSF 2.0
  • WebBeans
  • CDI (Contexts and Dependency Injection)
  • Bean Validation
  • EJB 3.1
  • JPA 2.0
  • JAX-RS

We will go through about one of the feature of javaee6, which is Servlet 3.0 . Again in Servlet 3.0(Java ee6) lots of features have been implemented but we will discuss only few which are quite interesting.

1)web.xml optional now

One of the very interesting feature/improvements is using Annotations instead of web.xml . No more complicated web.xml files and writing Servlet is comparatively less complicated now. Given below example Illustrates the same

Servlet 2.5 Example

In Servlet 2.5 , along with Servlet class ,even web.xml is required which is mandatory.

MyServlet.java

----------------------

package com.foo;

public class MyServlet extends

HttpServlet {

public void

doGet(HttpServletRequest

req,HttpServletResponse res)

{

...

}

...

}


web.xml

---------

MyServlet

com.foo.MyServlet

MyServlet

Servlet 3.0 Example

Now Let us see the same servlet

MyServlet.java

---------------------

package com.foo;

@WebServlet(name=”MyServlet”, urlPattern=”/myApp/*”)

public class MyServlet extends HttpServlet {

public void doGet(HttpServletRequest req,

HttpServletResponse res)

{

...

}


Now comparing examples of 2.5 and 3.0 , we could see that web.xml is optional and it is very

easy to code now.


Some more on Annotations

-------------------------------

Annotations to declare Servlets, Filters, Listeners

and Security constraints

@WebServlet – Defines a Servlet

@WebFilter – Defines a Filter

@WebListener – Defines a listener

@WebInitParam – Defines an init param

@ServletSecurity – security constraints

@MultipartConfig – file upload


2)Sharing JSP's and Static pages


From Servlet 3.0 onwards , static pages and JSP's are no longer confined to document root of web application


These can be placed under

WEB-INF/lib/[*.jar]/META-INF/resources

and later add this *.jar in classpath.


But one thing to note here is that , resources in document root take precedence over those bundled in JAR files


3)Asynchronous support

One of the other interesting/significant feature in Servlet3.0 is the asynchronous support.

With this enhancement the servlet no longer has to wait for the response from resource such

as any database operation or webservice operation, its thread can continue its processing . In other words servlet thread is not blocked .

Prior to 3.0 this was achieved in a proprietary way by using API on top of Servlet 2.5 API .

But now standard asynchronous support has been added to the Servlet API.



Sunday, June 7, 2009

cronjobs scheduling in Unix, Linux

I had to struggle a lot to find the exact working example for scheduling cron jobs in Unix.
Searched a lot in google and couldn't find the exact solution, after doing research here are the steps which not only gives complete working example, it even explains how to verify if the jobs have been executed successfully. Let's do step by step

Step1
-------
Create a perl script which prints "Hello world" say hello.pl in location /tmp/ by pasting the below contents

#!/usr/bin/perl
use strict;
use warnings;
print "Hello world\n";



Step2
---------
Before scheduling through cron job let us first verify manually
Execute the command:
/usr/bin/perl /home/hello.pl >> /tmp/log3.txt

The ouput is redirected to /tmp/log3.txt
Open log3.txt and observe the output "Hello world"

Step3
--------
First verify if there are already any cron jobs existing

command: crontab -l
the above command will list the cron jobs


Step4
-------
To add cronjob use the following command:
command: crontab -e
the above command opens the file and we need to add the following line

* * * * * /usr/bin/perl /home/hello.pl >> /tmp/log3.txt

Step5
------
After adding the above line, save the file . As soon as the file is saved and if there are no errors in the entries the cronjob will be saved and executed as described.
You should get a response as installing new crontab on saving the cron file) To list the cronjobs in your id, issue the command
crontab -l

the above command will list the newly added cronjob

Step6
-------
The above cronjob will execute the script(hello.pl) every minute. Check the /tmp/log3.txt at the end of 5 minutes and there should be 5 "Hello world" entries.

More information on timing
------------------------------
if there are 5 stars ( * * * * *) it executes everyminute. We can change this to appropriate date/time/month/year by following the below Rule
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * command to be executed

Hope this example is useful for everyone.