Saturday, July 5, 2014

JavaEE and SSH (Struts/Spring/Hibernate)

Java Structs/Spring/Hibernate are used widely to created JEE applications.

Some resources are here:

- A self-contained Java Struts Spring Hibernate Tutorial [1] in pdf to download. About 80 pages.

- mkyong.com [2] has many tutorials. E.g., Struts 2 + Spring + Hibernate Integration Example

- AppFuse [3] is a full-stack framework for building web applications on the JVM. It can generate SSH website template in SSH. This is a demo site of it.

The JEE ecology system is large, complex and distributed. It contains a lot of technology and can build highly professional enterprise web applications. However the distributed nature decides that it is poor to manage. For example, for each new functionality, one needs to go to a website, download another Jar and include it into the lib. This is comparable to adding references in VS.NET. In VS.NET all references come in package out of the box, and is much easier to handle mentally and practically. Another example is the directory structure. JEE directory structure is much more convoluted than ASP.NET, which artificially adds difficulty to the understandability.


== Servlet and JSP ==

Basically, servlet is more primitive, can use different protocols, embed html in java code. JSP is layered above servlet. JSP uses HTTP protocol, embed java code in html.  JSP is compiled into Servlet class automatically by container server (e.g. Tomcat).  JSP is more modern, like ASP and PHP, has better separation of java logic from UI presentation.

See more at [4][5].

== Tomcat ==

Tomcat is an application server for Java servlet.

It is possible to use Tomcat independent from Apache. But Tomcat works only for Java Servlet, and does not have as many other functions as Apache.

This is what happens when Tomcat works together with Apache. When Apache receives a browser request as a long stream of bytes, it passes it to Tomcat, which creates a request object to parse the stream into request parameters, and passes the request object to a (servlet) java class. After the (servlet) java class finishes the processing, and returns a response object, it then interprets this into a http response stream and passes the stream to Apache to be sent back.

To configure Tomcat to work with Apache, a Tomcat-Apache connector module is needed. This is often the mod_jk module using Apache JServ Protocol (AJP). By default Apache uses port 80, Tomcat uses 8080, the mod_jk module uses port 8009.

See more at [6][7][8].

== Development with Eclipse ==

Tomcat uses port 8080. If development and production are on the same machine, as in the case of personal project, Eclipse can do development in a separate Tomcat instance, the port number can be configured in server.xml, e.g., to 8081. At deployment time, it can use the export function (similar to Android project being exported to a apk file) to package the project into a *.war file. The *.war file, when deployed to the Tomcat server in production, is unpacked (you can do this manually with: jar -xvf *.war) and being served. This way, development and production use different Tomcat instances and different ports on the same machine.

To know if a port is being used by any process, say port 8080, type this:
sudo lsof -i :8080
To kill such process and release the port graciously [9], do:
sudo kill -15 <pid>
Here the tomcat pid can be obtained by:
ps -ax | grep tomcat

When running Tomcat with Eclipse, the server path used by Eclipse (similar to wwwroot for IIS, or /var/www/html for Apache, or /var/lib/tomcat7/webapps/ for Tomcat7) is:  workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps

== Issues running Eclipse with Tomcat ==

My feeing with Eclipse JEE development is, many configurations can go wrong. There is no central support, so you just get on google and search for other people's discussions and solutions. The quality of environment integration is less mature as the .NET world. For example:

1) after first creating a project, it may complain the j2se1.4.0 JRE not match installation jdk. So you have to go to Library, and reselect the jdk to use.
2) another example, Tomcat7 installation has one directory at /var/lib/tomcat7/, and another at /usr/share/tomcat7. The latter needs references to many files/folders in the former location, so you need to manually create soft links in the latter place.
3) add a server to the current project may not work, and complains port 8080 already in use, for this you need to change the port number used in server.xml.  Solutions to these problems are found online, e.g., in [9][10][11][12].
4) as one more prominent example, I finished the tutorial project in [1], in the process I need to download lib jar files from 15 different websites to set it up.
5) use the log4j project as an example, in version 1.2, one file log4j-1.2.17.jar can do it. But in version 2.0-rc2, you got a large number of jar files to choose from, for different platforms at least, have to include the api and core jar files, then some others. It's getting way bloated up.

Compared to the IIS world, there are much to worry here, and many need manual setup.  The .metadata folder under the current workspace contains many files and is complicated.  If you kill the Eclipse process before it's properly shutdown, Eclipse may not be able to start, and fix is to remove something corrupted here in .metadata.

== Struts ==

A MVC web framework for JEE development. For more details see [13][14].

== Spring ==

A programming and configuration framework for JEE development. Its principles include IOC (Dependency Injection), AOP, MVC, Convention over configuration etc. It's ambitious. But criticisms say it's just a loose bundle of best-practices.

For more details see [15][16].

== Hibernate ==

An ORM module for JEE development. ORM stands for Object-Relational Mapping. It basically handles all the work connecting to database and hides the details, through the use of configuration files. For more details see [17][18].

Java SSH can be used together. One major reason of their appearing was to address the unnecessary complexity of EJB.

I feel it's too much building up of concepts, which are often old wine packaged in new bottles. Servlet and JSP are the most primitive building blocks of the entire stack. While these technologies aim to provide a framework that's supposed to make enterprise application development easy, they are actually creating layers over layers, and become very much counter-intuitive to pick up.  Tools are just tools, but now just to learn how to use the tools can become a full time job.  In this aspect, LAMP and .NET are much more intuitive.


References:

[1] Java Struts Spring Hibernate Tutorial
[2] mkyong.com - Java related tutorials
[3] AppFuse - a full-stack framework for building web applications on the JVM

[4] Difference of JSP and Servlet
[5] Coding Servlet and JSP from ground up
[6] Can Tomcat run WITHOUT Apache
[7] Tomcat.apache.org: omcat-Apache Howto
[8] Apache 2 with Tomcat 6: How to configure Tomcat to work with Apache

[9] Tomcat startup 8080 port already in use
[10] Eclipse cannot load the tomcat server configuration at serverstomcat-v7-0-server-at-localhost-config
[12] Cannot add a new Tomcat server

[13] struts.apache.org
[14] Wiki: Apache Struts
[15] spring.io
[16] Wiki: Spring Framework
[17] hibernate.org
[18] Wiki: Hibernate (Java)


4 comments:

Unknown said...

Thank you for sharing this knowledge in a blogpost.Really simple and even more effective and this worked great, very useful tips
sas training in hyderabad

anirudh said...


Thank you for sharing the article. The data that you provided in the blog is informative and

effective. Best Devops Training Institute

Hamad said...

LoadRunner is a testing tool used for the performance of any applications or software from Hewlett Packard Enterprise so-termed as HPE LoadRunner.This tool is used for Testing applications, measuring the behaviour of the system and analyzing performance under user load. Course Name itself Load that defines applying load on the application and with thousands of users at a time. This has the highest market share that makes it become the pioneer in performance testing.

tektutes said...

Very Nice Blog…Thanks for sharing this information with us. Here am sharing some information about training institute.
tableau online training in hyderabad

Blog Archive

Followers