Monday, April 29, 2013

2.1.3 Working with Filters and Servlets

Filters are java classes that can dynamically intercept requests, manipulates them and manipulates response before sending it back to the client.

Sometimes, it may be necessary to do the same thing in different servlets. In those cases, a filter may be defined and reuse it for all of them.

Tasks that a filter can perform includes
  • Authentication Filter
  • Image Conversion 
  • Data Conversion
  • Encryption 
  • Cache

To explain this concept, we shall create a servlet and two filters. Then we would see how can we call the first filter before the servlet is called, then the servlet and lastly the final filter would be called.

Code of servlet:
@WebServlet(name = "helloWorld", urlPatterns = "/helloWorld")
public class helloWorld extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                      response.getWriter().println("This line is from servlet");
    }
}
Then the code of first filter:
@WebFilter(filterName = "HelloAppenderFilter", urlPatterns = "/*")
public class HelloAppenderFilter implements Filter {
    private FilterConfig filterConfig = null;

    public void destroy() {
        filterConfig = null;
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        resp.getWriter().println("Hello (from filter 1)");
        chain.doFilter(req, resp);


    }

    public void init(FilterConfig config) throws ServletException {
        this.filterConfig = config;
    }

}
Second filter:
@WebFilter(filterName = "WorldAppenderFilter", urlPatterns = "/*")
public class worldAppenderFilter implements Filter {
    private FilterConfig filterConfig;
    public void destroy() {
        filterConfig = null;
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {

        chain.doFilter(req, resp);
        resp.getWriter().println("World (from filter 2)");
        

    }

    public void init(FilterConfig config) throws ServletException {
        this.filterConfig = config;
    }

}
If we check the url patterns of the both filters, we shall find no difference. Then how do we control the order of execution of these filters? Well, the answer lies in the web.xml file.
 
        HelloAppenderFilter
        /*
    
    
        WorldAppenderFilter
        /*
    
 

2.1.2 Sessions and Servlets

Purpose of using session is the same as of using cookies. The primary difference is, cookies are stored in client browser where sessions are stored in the server.



Sessions can be managed in four methods
  • Cookies
  • URL rewriting
  • Hidden form fields
  • SSL

Examples of these method would come later. For now, we shall use the servlet api to operate on a very simple session.


@WebServlet(name = "SessionServlet", urlPatterns = "/sessionTest")
public class SessionServlet extends HttpServlet {
    int counter = 0;
       public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
           response.setContentType("text/html;charset=UTF-8");
           PrintWriter out = response.getWriter();
           HttpSession mySession = request.getSession(true);

           synchronized (this){
               out.println(mySession.getId());
               out.println("
You have visited this page "+(++counter));
               out.println((counter == 1) ? "time":"times");
               if(counter % 5  == 0) mySession.invalidate();
           }

           out.close();
    }

}
In this code snippet, the retrieved which was associated with the request object. The boolean true means that a new session would be created if there is none in existence.

Saturday, April 27, 2013

2.1.1 Cookies and Servlets

As Http is a stateless protocol, it doesn't remember client information between request intervals. Each request is new to him. But in some kind of applications like shopping card apps, website tracking, visitor profile generation it is necessary that the server does remember information about the client.

Cookie is small amount of data captured by the server and stored in the client end. Each time the client visits a website, server checks if there is any cookie present in the clients end and then responses accordingly.

  • There are 6 parameters that can be passed to a cookie:
  • The name of the ccookie
  • The value of the cookie
  • The expiration date of the cookie
  • The path the cookie is valid for
  • The domain for a secure connection to exist to use the cookie
Again, there are two types of cookies:
  • Permanent/Persistent Cookies-Cookies are not destroyed after closing the browser.
  • Session/ Transient Cookies-in this case, cookies are destroyed after the browser is closed.

Ok, let's move on to create and retrieve some cookie on our own. We shall create a cookie that accepts user input and remembers it for a specified time. When the client visits the page within this time, the information is shown to him. 

First, let's set the cookie.

@WebServlet(name = "SetCookieServlet", urlPatterns = "/setCookie")
public class SetCookieServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        Cookie songCookie = new Cookie("songCookie", "Welcome to the Hotel California!");
        songCookie.setMaxAge(10);              //in seconds
        response.addCookie(songCookie);       //important! This line actually transfers the cookie to the browser

    }
}

Now, in another servlet, we shall retrieve the cookie value.

@WebServlet(name = "GetCookieServlet", urlPatterns = "/getCookie")
public class GetCookieServlet extends HttpServlet {
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setContentType("text/html;charset=UTF-8");

        PrintWriter out = response.getWriter();

        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals("songCookie")) {
                    out.println(cookie.getValue());
                }
            }
        }

    }
}


Hit the url /setCookie once. This will create the cookie which will be alive for 10 seconds. By this time, hit the url /getCookie. You should see the title of one of my most favorite songs.
 

Wednesday, April 24, 2013

Setting up the development environment for Java EE 6 with Intellij Idea and Glassfish.

You would need four things mainly:
  • Java EE SDK
  • Integrated Development Environment
  • Application Server
  • Database
1. Java EE SDK

Select your appropriate one from this link and then install it after downloading.

2. Integrated Development Environment

Sun recommends NetBeans for EE 6 development. It's free for both commercial and non-commercial use. However, I'm suggesting you to use Intellij Idea, which is arguably the best IDE for software development.
Download Intellij from this link. You might download the free trial of the ultimate edition and then register it later. Install it after running and following the on-screen instructions. This link might be helpful.

3. Application Server

I suggest you configure two application servers for development and test each of them. Namely, Glassfish 3 and JBoss 7. You would get a better idea about servers them from these so question 1 and so question 2. The bottom line is, JBoss and Glassfish are the two most popular app server. Though JBoss is supposed to be fast, it lacks JEE6 spec support compared to Glassfish. So, for now, let's just stick to Glassfish.
I suggest you take some time and configure glassfish according to this guide.

4. Database

Postgresql or Mysql would be fine.
sudo apt-get install postgresql-9.1
sudo apt-get install mysql-server
 
That's all for now.


2.1 Servlets

I do not intend to write a comprehensive post that would describe the servlet technology as a whole. It's just not possible. Great books with numerous pages are already written on each of the tech stack of JEE 6. Servlets are no exceptions. So, I would try to capture the basic idea of these things.

Overview:

Servlets are pieces of program that run in server to handle clients requests. Unlike the web servers, Servlets can access DAL, convert the resultset into browser readable format. Servlets can handle multiple client requests efficiently as they are not run as processes rather, as threads.

Let's look at an example of a servlet.
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebServlet(name = "WelcomeServlet", urlPatterns = "/welcome")
public class WelcomeServlet extends HttpServlet {
    @Override
    public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {

        response.getWriter().println("welcome to the hotel california!");

        response.getWriter().close();

    }

}

}
You might notice that the line
@WebServlet(name = "WelcomeServlet", urlPatterns = "/welcome")
defines the servlets name and url pattern that it would serve. For example, if your project's root is localhost:8080/myapp, then this servlet can be called by the url localhost:8080/myapp/welcome. It is possible to override this url pattern annotation by modifying the web.xml file found in WEB.INF folder. If we add these lines in web.xml
    
        HelloServlet
        servlets.HelloServlet
    

    
        HelloServlet
        /hello
    
then the servlet's name would be overridden and it would be called by "/hello".





Monday, April 22, 2013

1. Introduction to Java EE 6

Throughout this series I will be discussing the basic technology stack of JEE6. The discussion will contain brief overview, usage and small programming example.


I assume you have basic knowledge about what enterprise application is or what Java EE 6 is up to. I will be focusing on the tech stack of it.

Basically, there are four tiers of technology that enterprise java uses.

Web Applications Technologies

  • Java Servlet
  • JavaServer Pages [JSP]
  • JavaServer Pages Standard Tag Library [JSTL]
  • JavaServer Faces [JSF]
  • Facelets
Enterprise Applications Technologies
  • Enterprise JavaBeans [EJB]
  • Java Persistence API [JPA]
  • Java Transaction API [JTA]
  • Java Message Service [JMS]
  • Java EE Connector Architecture
  • Java Mail
Web Services Technologies
  • SOAP and RESTful
  • Java API for XML Registries [JAXR]
Security Technologies
  • JACC
  • JASPIC
So, sit tight, and learn with me. The best is yet to come. :)
 

Motivations Behind Choosing Enterprise Java

Hello there!

Actually, this is not going to be reasons appreciated or accepted by all. It's just the reasons I've decided to give Java Enterprise 6 a go. At least before Ruby On Rails or Django.

1. Since the early days of my programming, I've been using Java SE, C/C++ or PHP. The syntactical structure are somewhat same in these languages. Brackets, semi-colons are must and these sort of things. That's why it seemed somewhat awkward to me when I saw the syntax of python or ruby. I thought to myself, 'How the hell these people manage to maintain OOP!'. Maybe I didn't dig deep enough. But still, why won't you say the type of a variable while you are declaring it! One or two words too much for you? :)

2. If I make a mistake in my code, I like it better appear at compile time. So, Java got my preference over ruby or python.

3. I found a ocean of Q/A or comparison posts in the internet about the productivity issue of JavaEE 6 and ruby or python. Both have some valid grounds of argument. The bottom line of these posts was this:' Java EE is for very big enterprise applications where security, scalability, maintainability are deciding factors. Frameworks like Ruby on Rails or Django are for startups, where quick output counts." My philosophy is, If you know how to build a Titanic, then you can quickly build one or two ships by applying some new tricks.

4. Productivity is an issue in enterprise java. All those compilation, deployment and then seeing the output takes a large amount of time. Though JRebel and Play framework are trying to minimize this, it's still a lot. However, as I said earlier, first I need to learn how to build a Titanic. So I'm not taking it as a factor right now.

5. Finally, I just want to harness the monstrous power of astronomically big Java EE 6 and feel myself stronger. :)

So, plain and simple, not (Rails, Django, JRebel, Play,  Spring), just Java Enterprise Edition 6.

I will get them later, after I acquire some destructive power from Java EE 6. :)