Showing posts with label java ee. Show all posts
Showing posts with label java ee. Show all posts

Sunday, October 18, 2009

Servlets are the starting topic when covering the Java EE platform. So let's begin.



In a Java environment you have three key pieces to making your program run.


  1. The Java Virtual Machine (JVM)

  2. The Classpath

  3. The Class loader





The class loader is the important one here. It is the job of the class loader to get your Java class loaded into the JVM. This is much the same as a loader for an OS.


The Class Loader is "boot strapped" by the java command line utility. This is all fine and dandy, up till you get to a point where you need classes loaded by a remote client.


Enter the container. The container is a concept much like the class is. A container is a class that loads other classes, much like the class loader. It can other things as well like support LDAP security, email users, establish an SSL connection, manage database transactions, and so on. The container is limited by the scope of the system class loader, but the container can load classes based on whatever rules it provides.


Much like a class hides data members from the programmer, containers hide the method of invocation for classes loaded into it. A web container gets an HTTP request and loads Java objects in response, it doesn't really matter how it goes about doing it.


Servlets are the base of the Java EE model. They are invoked by an HTTP request, by means of a web container (see above). Servlets implement the Servlet interface which allows a container to invoke and maintain the life cycle of the object. More than likely you will be looking to extend the javax.servlet.http.HttpServlet class. This class comes with pre-built methods to handle the common HTTP request. Such as:


  • doDelete - to handle the HTTP DELETE request

  • doGet - to handle the HTTP GET request

  • doPost - to handle the HTTP POST request





and so on... Each servlet that you create translates into a web page. Each method implements logic to handle a type of request. You'll be spending the bulk of you code in the doGet and doPost methods.



Working with the base servlet model is not the idea tool for interactive web pages. Basically you are writing a web page in Java as opposed to HTML, which adds to the complexity. Using the base servlet model is good for operations that you really want fine grain control over the entire logic flow. Here is an example of a servlet. You can readily see that we're just writing a web page within Java.





import java.io.IOException;
import java.io,PrintWriter;

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

public class HelloWorld extends HttpServlet {

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

PrintWriter wout = response.getWriter();

wout.println("<html>\n<head><title>Hello, World!</title></head>
\n<body><h1>Hello, World!</h1></body>\n<html>");
}
}





As you can see this isn't exactly the greatest model. That's where JSP comes into play, but I'll cover that later. For now, you can see that a servlet allow fine control over what type of request will cause what action, but it is a lot to be asking you programmers to be Java/Web/Http experts all the time (ie, you could not manage [reasonably] a web site strictly made up of servlets.) Usually you will reserve servlets for testing EJBs and stuff that requires fine control over the process. You'll mostly use JSP/JSF for all other stuff.



Cheers!

Thursday, October 15, 2009

Understanding OOP pitfalls in Java EE:

Object Orientated Programming (OOP) is the foundation on which the Java language is built upon. When you build a Java program you are building a set of objects that will work together towards a goal. This brings me to the first point: in Java EE it is important to remember to think of the object and not the task.

A common pitfall is to start adding logic into a class that broadens its scope. Remember, objects should be single shot, single idea things. Let's say you have an object call BankAccount. You would put into this class the account number, the account name, address, details, balance and so forth. However, you may be tempted to place logic that converts the object into, say a SQL string or properly formats an address for display in a GUI. NO! Do not add logic to the class if it does not need to be added to keep the concept that the class represents, sane. A bank account does not need to know about SQL, a bank account does not need to know about GUIs.

The reasoning behind this is that the Java EE platform takes a lot of the bridge work out for you, you adding it back in prevents you from fully accessing all of the features (besides if you're adding them in who is Java to tell you wrong?)

Complex logic goes into classes that will handle other classes. In the example above you might include the BankController class. It will be this class' job to gather the needed objects to handle the object you pass in. The BankController
class may need to gather the objects that handle permissions, validation, database connectivity, and so forth. The Java EE platform has this covered for you. So you just worry about modeling classes after the real world thing they represent.

Wednesday, October 14, 2009

Java EE 5:
I'm really big into Java. I know a ton of people boo Java because they think it is slow or some other reason. I'm here to tell you that Java is very prevalent and very useful. Many websites are powered by Java EE and with good reason.

Over the years Sun and the Java Community have added really exciting technologies to the Java Platform. Some have seen their day come and go, but many have really useful corner cases. However, the best platform I've seen in some time is the Java EE 5 platform.

Java EE is one of the few multi-tier application platforms still in town. The really hot craze right now is your good old LAMP stack or RoR. However, these approaches return to the days of monolithic hosting of network applications. This approach is nice for small to medium deployments, but without some serious modifications, do not scale well in large deployments. This is one of the reasons why LAMP has taken off so much, the stack is completely open source, so modifications can be done on the code easily to adjust to large deployments.

Multi-tiered software was made to scale. It has lost favor since it takes a lot of resources to get started at first (due mostly from configuring and creating the first bits of each layer.) Java and .NET have taken steps to address this large problem, how do we make multi-tiered APIs but still make it easy for small deployments?

Java EE 5 and the future EE 6 bring a lot to the table to make it easy to stop boiler plating things and start deploying services to meet your customers demands. I know it sound a lot like a big commercial.

Multi-tier software has gotten a bad rap from things like DCOM and CORBA. But if you sit down and try Java EE 5, I'm sure you'll see that multi-tier has come a long way from where it was.