EJB and the Web Tier - java

I am learning EJB and to do that I bought a book name EJB 3 Developer Guides. I got some understanding on how EJB architecture is and how it works. The book does well in explaining this.
But the examples in the book are all run through ant task. I basically wanted to learn how EJB's are being used in conjunction with Servlets and JSP. Some sort of a web client accessing a JSP page and a servlet getting the service of an EJB bean?
Any idea how can I move forward learning how to use EJB with the Web Tier? Tutorials or links would be better or is there any other book that I could read?
Thanks.

Simple EJB3 Servlet Application
And another option is Netbeans Java EE and Java Web Tutorials -- a best bet, if you are using Netbeans. And if you are not, and you are new to Java, I would highly recommend you Netbeans IDE. It is simple and straight forward.

Important note: if you try running the "Simple EJB3 Servlet Application" from the link about be sure you are using an appropriate version of Glassfish. It might work on JBoss 5.x - but I've never tested it (according to JBoss).
You won't be able to run on the sample application on older versions of JBoss (like 4.2.x) without some workarounds - like ones I described in one of my posts: sample application
The Netbeans tutorial seems fine - but stick to the environment and platform they are describing - otherwise you might quickly discourage and give up.
--Jakub

Related

Where can I find a current EJB tutorial?

Here on StackOVerflow I read that we shouldnt use Remote and Local interface inheritance for EJB server appllication and more, we shouldnt use deployment XML files any more.
So what are the main steps now (2013) for creating a complete EJB simple "Hello World" application? Is there any tutorial which explains it step by step for people for whom its a totally new thing? Cheers
What I found on the Internet is that the newest EJB standard is EJB 3.1. - is that correct?
EJB has really fallen out of favor. I recommend listing out what benefits you are looking for and see if there are simpler ways of doing them, in many cases there are. Are you sure you need to go EJB? Try looking at http://spring.io/.
JSR-345 defines the 3.2 version of the ejb specification
Conceptually the added features from 3.1/3.0 are not significant, so most tutorials you find for those versions will still be ok.
anyways, a recent tutorial can be found here
So what are the main steps now (2013) for creating a complete EJB
simple "Hello World" application? Is there any tutorial which explains
it step by step for people for whom its a totally new thing?
You have a lot of tutorials on oracle site.
What I found on the Internet is that the newest EJB standard is EJB
3.1. - is that correct?
Yes, latest EJB (at this moment) is EJB 3.1.

Explain: How does Java work as a web application?

Ok, I know this is a vague question, and I expect a vague/basic answer.
I am well versed with PHP driven MVC frameworks and how the process of serving pages works. I'm very interested in learning Java, and I figure the only way to learn it is to do it. However, after reading page after page after page, it gets more confusing as I read.
I'm running into GWT (front end), Hibernate (ORM connection?), Spring Architecture, Spring Roo (what is this?), JBoss (servelet), JPA (ORM), POJO (a style of using regular java objects with orm?), Maven?
My basic question is how do all of these fit together? I like the idea of using a framework, because it's worked well in the past with PHP. How do I use this functionality with java? Suppose I wanted to build a web application with a relational data store. How does java authenticate, initate a session, and serve pages with dynamic content? What is the process?
When responding, please don't say "stick with what you know," (as I've seen on other pages) because I'm very interested about java and I'm trying to learn.
I totally hear you - too many acronymns have propped up as Java EE has evolved.
I will try to put an explanation
Java is the language
Java EE (Java Enterprise Edition) is the general term used to encapsulate all Java technologies that help create an enterprise/web application with Java
So a typical web application with Java looks like follows:
Front End
Use JSP or JSF for server side processing and rendering (JSP and JSF
offer ability to define your UI using HTML and server side tags that
bind easily with your Java Beans) However they do tend to mix UI with
backend if not implemented correctly
you can also use plain HTML and any client side toolkits for rendering (such as jquery, dojo, extjs or flex) and fetch your data
from the Java EE server.
you can even mix the two (use a client side framework in a JSP to achieve best of both) or use a toolkit like GWT that offers client side richness with javascript with Java APIs and ease of accessing your java beans
Middle tier
Java Servlets offer the base for all server side processing with Java EE (Servlets offer session persistence, HTTP request processing, response processing) and typically offload the business processing to a model bean - which could be a POJO or spring bean.
Servlets are Java programs you write and are executed in what is called a Java EE container. Popular containers are tomcat, IBM websphere, BEA Weblogic
To help implement a good MVC architecture, there are several frameworks and tools provided on top of Servlets:
Struts2 and Spring MVC are examples of such frameworks
To make it easy to implement web services, Restlet/Jersey help with REST based web services, JAX-WS/APache Axis help with SOAP based web services
Integration with backend database/persistent store
You could use JDBC in your POJO or other model bean to access the DB using Java.
Or alternately use one of the frameworks such as Hibernate to make it easy to interface with the DB backend
Sun's petstore application is a good place to start to learn how the pieces fit together
http://www.oracle.com/technetwork/java/javasebusiness/downloads/java-archive-downloads-eedocs-419425.html#7522-petstore-1.1.2-oth-JPR
Build and Deployment
Ant and Maven are tools used to build your app and manage dependencies
the application gets packaged as a WAR or EAR file and can be dropped into any Java EE container to deploy it. Once it is deployed, it can be accessed using a URL
Tools like Eclipse, Netbeans, Oracle JDeveloper offer integrated IDEs to help with local deployment and testing
The good part with Java EE is that you can pick and choose the components you want to use to build your webapp.
The problem Java has is that it's fairly old and there are TONS of frameworks and packages that do similar things as other packages. At the very core, however, is the Servlet, that specifies how Java behaves as a server over the HTTP protocol.
With that basic building block, there are lots of web frameworks like Struts, and Spring MVC that build up layers of functionality by using lots of good OO development patterns, like Fiters, Delegates, Factories, MVC, etc., to allow the developer to put together an application that takes web requests as input and returns a web response as output.
Those frameworks often build on other frameworks or packages to give application developers more capabilities at different layers of the application. This includes things like ORMs (like Hibernate) to talk to relational databases, or view compositing frameworks like Tiles or Velocity to help put together an HTML page as part of the response.
There are lots of other acronyms and tools and layers that get built up around web frameworks, but essentially they are all just programming tools that have useful functionality pre-built and just need to be used.
If you are looking for a more packaged web application development framework that is more cohesive and doesn't make you feel quite as lost, you may want to take a look at Grails. It is in Groovy, but it is very close to Java in terms of the language and actually builds on top of lots of the other tools you have heard of.
Grails itself builds on the Spring and Spring MVC frameworks, as well as Hibernate, so it's using the same technologies as other more pure Java frameworks, but it abstracts the ugly details away so you don't have to worry about them unless you want to. You can then bundle in additional functionality, like authentication / security, through plugins. If you are well versed in PHP-based MVC frameworks and how they work from an architecture standpoint, you will feel right at home in a similar MVC environment like Grails or Spring MVC. The Grails User Guide is a great place to start, and the rest of the documentation is good too.
If you are new to the Java language, I would strongly suggest doing a few small applications (not web apps, just simple ones) to learn the language and get familiar with how things work. Java is very different from PHP, since PHP bundles in a lot of the base functionality into the language whereas Java has a fairly sparse language with much of the commonly used functionality coming from libraries.
Once you have a good grasp of the Java language, I would jump right to the Grails framework and I would skip all of the nitty-gritty details of servlets and ORMs and Hibernate, etc. at first because you don't need to know it all and there is so much there that it can get in the way of actually understanding things.
Then, once as you start to build out an application with Grails, you will gradually get a deeper understanding of the technology Grails is built on in order to do more complex and powerful things. Slowly, you will work your way into Spring and Hibernate and build up an understanding of how things are put together under the covers, but you can be doing real things faster since you don't have to know all of that from the beginning and can quickly make web applications in Grails the just work, especially with an understanding of the MVC architecture pattern with respect to an application responding to a web request.
Wow, not only is this an open ended question, but it could have pages and pages of answers. I worked in php ~ 7 years ago without a framework so I'll try to point you in some starting directions and compare my experience (which is outdated and sans framework!)
You need an application server just like your Apache server for your Java web app, such as Tomcat, Jboss or Glassfish. These servers handle serving dynamic content.
On top of the server you have your web frameworks which you've mentioned GWT, Spring, and Spring Roo. Spring Roo is like Rails, but in Java. GWT goes all out and and will write your html/javascript code based on your Java code.
In Spring You can define objects to be used in your forms and then when they are submitted the entire object is passed back so there is less work. I remember the days of getting a lot of $_POST[] stuff and I'm thankful not to have to do that when using spring. There is Spring Security that you can use for auth.
The web frameworks are configured to connect to the database and then there is the database abstraction ORM, Hibernate. In PHP I used EZSQL for abstraction which didn't contain nearly the amount of features Hibernate does. Hibernate has a steep learning curve, but is worth learning.
For dynamic GUI's you'll probably want to research JSP, but may be interested in learning JSF.
If I were you, I'd pick an application server, maybe tomcat, then a web framework to play around with, personally I'd go with Spring. The framework will probably have dynamic GUI examples so you'll pick up jsp/jsf. Then possibly add the ORM and a build tool to build externally from your IDE, such as Maven, Ant, or Gradle.
There are some very mature java libraries that each target a very small need in a web application. This means that many tutorials on the subject will have to pick and choose the libraries for each need. For someone just starting out from your position, this probably sucks.
Naively, then, I searched for "java full stack framework" and found: Full stack framework for Java
The benefit of a full stack framework is that you don't have to choose each component. The framework has strong (perhaps rigid) opinions on how ORM is done, how templating is done, how mapping URLs to functions or actions is done, etc.
As for your list of technologies and acronyms:
GWT - a framework from google that focuses on the front end. Poorly stated, write your front end functionality in Java and have it magically transform into javascript.
Hibernate (ORM connection?) - yep, store and load objects in your app.
Spring Architecture - Spring is pretty close to a full-stack framework, but it doesn't have as many rigid opinions on things. You can swap out templating engines, swap out ORM, etc. Not a bad framework, though. You might want to simply follow a tutorial on Spring (see below on Roo), and use the components suggested by the tutorial. Just know that you might find something else later that fills a particular niche.
Spring Roo (what is this?) - Spring Roo takes Spring and becomes opinionated (use what we say). This allows for less code on your part because it provides the code that integrates the various components. It still allows quite a bit of flexibility when you want to change something. Bonus, it comes with a nice tutorial.
JBoss (servelet) - Usually I think of JBoss as an application container. Since the Java EE spec is a bit more complicated than simple CGI--there's a lot of things that need to be set up by the web server (loading classes, loading configuration files, connecting crap together)--JBoss does that stuff. Alternatives are Tomcat or Jetty.
JPA (ORM) - Yeah, it's a common set of interfaces that the various serialization providers might implement. It might be a database, it might be something else. But the idea is that your code for storing and retrieving objects would look the same.
POJO (a style of using regular java objects with orm?) - In context, probably. "Plain Old Java Objects" are nice for any library. Sometimes a framework might require that you inherit your classes from some special class, like Model or Controller to work properly (also, HTTPServlet). This isn't good, because it restricts your own class hierarchy design and makes your own code less flexible. Consequently, things that work with POJOs are considered better.
Maven - Maven is a tool that helps manage dependencies. Without it, java has its own form of DLL hell. Library A depends on version 1.1 of Library B, but Library C depends on version 1.5 of Library B. Ohhh crap, time to read through a tutorial on classloaders. Don't worry too much, though, any tutorial on java web apps is likely to tell you exactly what you need to download and use.
The first thing I would suggest you to start with, given a knowledge of http protocol, is the JSP technology. Although you would probably use some framework like JSF2 for instance, it is important to start with JSP in order to well understand the technology and how to use it to deal with request/responses (that's my humble opinion of course).
Once you are familiar with JSP and, let's say, JSF 2.0 (you can find loads of documents on that topic) the next step is to try to connect with a data source. In Java EE technology there is a specification called JPA (Java Persistence API). JPA is just a specification for ORM (which is, roughly speaking, mapping an object java model with a set of DB tables)
Once you have your web application working with some basic pages and some operations on a DB you could enforce the security of your app introducing some security mechanisms.
this is a very good reference and start point for all of these topics and much more. It's a long path and it will take you some time. But, believe me, it's worth it!
Good luck!
I suggest you google those keywords and look for some books and tutorials.
Maven
is a tool for managing your Java projects and artifacts (JARs, WARs, etc.). I'd start learning Maven first, so that you have a foundation to create your Java projects on. It also handles your dependency management: you just specify what JARs you need in your application and Maven will download them for you.
JPA (Java Persistence API) handles the Object-Relational-Mapping of your entities. You can write POJOs (plain old Java objects) and map them to your database tables.
Hibernate is a JPA provider (i.e. implementation of JPA). Usually you don't have to deal with Hibernate that much, most of the time you can use JPA directly. You just configure the JPA provider in the persistence.xml config file.
CDI (Context and Dependency Injection) see description. CDI "wires" the components of your application together.
Springframework started as a framework to offer Dependency Injection capabilities, but today it's much more than that. The WebMVC module of spring might be interesting to you. You can write Controllers and Views (using JSP for example).
Servlet API A servlet acts like a little server, handling a HTTP request and generating the response. You can write your own servlets or use a web framework to do it's job, for example Spring's DispatcherServlet or Java Server Faces, or whatever framework.
JSP is a technology to write templates for your HTML files. JSP files are being compiled into Java classes and can contain HTML code, JSP-specific XML code and Java code.
Example:
<ul>
<c:forEach items="${countries}" var="country">
<li>${country}</li>
</c:forEach>
</ul>
renders a list of countries, where ${countries} might be a collection of country objects (or strings in this case).
JSF (Java Server Faces) is another framework for building web applications, utilizing JSP and/or XHTML for defining the views and backing beans for the backend part. I would start learning with JSP instead of JSF, it's easier to learn.
Most of the frameworks are part of the JavaEE standards portfolio. For stuff like CDI to work you need either an application server (like JBoss AS) or at least a server with a servlet container (like Apache Tomcat or Jetty) in combination with Spring.
Spring and Hibernate are not standard, but they implement many of the standard APIs nowadays.
**EDIT: ** you might also want to learn about Annotations in Java code. They are around since Java 5 and are widely used as an alternative to XML based configuration.
So my suggestion is that you start learning Maven (not necessary tho, you can also manage your project manually), then Spring and JSP to be able to create a simple web application and then JPA/Hibernate to make your data persistent.
In my opinion learning Spring is much easier than learning the whole JavaEE APIs, since Spring is quite good documented.
One of the nice things about GWT is that you'll right your client code in Java, with all of its benefits and if you have server code, that could also be in Java as well, and you could share some of the source code, even if the front-end ends up running as JavaScript.
TO build a basic Java web application, go through this tutorial as it will explain the core foundational technologies with the standard Java EE platform and how to develop something real with them.
http://netbeans.org/kb/docs/javaee/ecommerce/intro.html
It will demonstrate the standard Java web technologies such as EJBs, Servlets, JSP, JPA, and how to use Netbeans for your IDE.
Most beginner's are very overwhelmed by the thousands of different technologies and platforms out there with the Java EE framework. Java caters to a very 'open source' type of community and that means that you have ALOT of different choices and road maps on how you can build applications. This is very different from say the .NET world where everything is pretty straightforward and there's a very clean cookie cutter path.
Once you have learned these foundational basics by going through the tutorial, you will have a grasp on what the Java EE framework. Once you understand these basics, you can then start to learn about Maven or Spring and how those type of technologies can help you build better applications and you can make better and more educated decisions.
GWT - Web interface tools. Some competing technologies: Spring, JSF, Wicket
Hibernate - ORM mapping for databases. Competing technology: JPA
Spring Architecture - Web Framework. Competing technology: JSF, Wicket, GWT
Spring Roo - An MVC style flavor of Spring.
JBoss - Application hosting. Competing technologies: Glassfish, Tomcat
JPA - ORM mapping for databases. Competing technology: Hibernate
POJO - Plain old java object. Just a simple object.
Maven - Maven is a way to manage libraries or dependencies that your project uses. There's a central server that hosts many of these libraries and if you ever need to import a new one to use in your project, it's as simple as searching through the server and then copy and paste the configuration settings that they provide you into an XML document called the POM file or pom.xml
Vijay's answer above is pretty much spot on. I'll elaborate on a few points.
Most of the "fameworks" that you mention, spring, roo (which is just a way to work with spring), gwt, struts, etc. all work on top of "Java EE". Java EE is Sun (now oracle's) term for the "standard" way to work with web applications. Java EE also includes a lot of more enterprise stuff like EJBs, JMS, JMX which you won't really need to concern yourself with unless you're doing very high end stuff.
JSP is the part that will be most familiar to you coming from PHP, it's a way of embedding code inside of an HTML page.
Servlets are the middle tier, that's where database connections are created and application logic happens. As it turns out, JSPs are converted into Servlets at compile time.
All Java EE stuff runs inside of a "servlet container" or an "application server". That's the big difference between java and php. Your choices here are usually Tomcat, JBoss or Jetty.
Maven is a build system that helps you to manage 3rd party jar files (libraries) and compiles and runs your test codes. It has plugins to deploy your code to tomcat/jboss/jetty.
POJO stands for "Plain old java object". Most modern frameworks try to shield you from the complexities of what happens under the hood and let you work with "POJOS". This comes into play when working with something like spring or hibernate. You may also see the term "java bean" tossed around, that's a standard way of defining "pojos" that just sets up a convention about how to name getter/setter methods and constructors.
I would recommend getting a book on getting started with JSPs/Servlets and starting there.

Starting web programming in Java

I've been programming in PHP and ASP.NET for a while now. When doing PHP, I've always used XAMPP to test my websites. I'm wondering (since I'm learning Java) if there are equivalent services around?
Furthermore, I know this is sort of an open question, but how would you make a basic page in Java, just a basic 'Hello World' web page.
You can actually still use XAMPP. You just need to install tomcat. Here is a great screen cast on how to do it: http://www.youtube.com/watch?v=TUV_jPdq7fc.
If your a mac user you can try MAMP (whcih I prefer) and follow http://blog.mirotin.net/22/tomcat-on-mamp-the-simple-way. To get Tomcat going.
You might want to learn about programming in JSP and Servelets. JSP s are kinda like php in that they emit html out.
Also the server would be tomcat.
It would be best if you download Eclipse for EE developers. You can also try Netbeans. a sample servelet
You may read about servlets which is the basis for web applications in Java. There are many web frameworks built on top of them.
I always recommend Grails to anyone getting started with java/the web. It's more important to grasp the patterns, lifecycle, and working with requests and responses than learning the actual language. Learning Grails has got me from hobbyist developer to IT employee working with ATG - one of the most obscenely challenging web frameworks there are.
Grails includes everything you need to get started: local server, scaffolding (that's your ticket to Hello World), database access, dependency injection, the list goes on. It's built on top of Spring, Hibernate, Sitemesh, etc, which are all well-known and excellent frameworks that are being used in production.
Grails is written with Groovy, a language written on top of the JVM not far from Java.
Happy coding!
i seems to be new to the platform nd new to java but would like to suggest what i have gained till now that for developing a java page we can have any of the two servers downloaded(Weblogic or tomcat). weblogic is fully implementation of classes whereas tomcat has partial implementation. Then for the next step that we require is a source file(java file),and html file and a xml file and these all needs to be placed in a root directory as like the source file,html file and a folder named WEB-INF to be placed parallel.
Inside WEB-INF folder needs to create classes folder where our java class file will be kept.
then needs to create a .war file and deploy it over weblogic to get our desired page display.
thanks......

Tools for java web applications?

What are the different ways to create a web-application in Java? Are there tools available other than straight-up servlets?
Open source web frameworks in Java and related view technologies:
Tapestry
Apache Cocoon
Apache MyFaces
Spring MVC
Google Web Toolkit (GWT)
Java Server Faces (JSF)
Apache Struts
Turbine
Seam
Makumba
Java Server Pages (JSP)
Stripes
OpenXava
JPublish
wingS
Strecks
AribaWeb
Echo
RIFE
Anvil
WebOnSwing
Click
ThinWire
Facelets
Wicket
Check http://java-source.net/open-source/web-frameworks for more options and to read a brief overview of each one.
There are many many web frameworks for Java that allow you to build Web Applications. The vast majority are built on top of the Servlet API, but provide a layer of abstraction that you interact with instead of dealing with servlets directly
My favourite (for what its worth) is Wicket
An quite old and simple standard is CGI, and java can do that: http://www.apl.jhu.edu/~hall/java/CGI-with-Java.html
A more recommended today would be to use a web-framework. These usually abstract from the Servlet API and provide a ground for common patterns in web development.
Here is a quite long list of available choices: http://java-source.net/open-source/web-frameworks
In java there's also Facelets, JSF and many other frameworks, like Spring MVC.
JuanZe gives a good answer with the list of frameworks. Here's some more detail.
I'd say take a look at Struts, as it's one of the oldest and simplest frameworks. Struts v1 and v2 are two different beasts; Struts v1.0 was written mostly in a day, and you can learn it in an hour, and might be a very good introduction to web frameworks.
I currently use Spring, and wouldn't go back to Struts from there. The nice part of Spring is that it comes with the framework (Spring MVC), but there are dozens of other "nice to have" components that really help you out as you continue to learn 'em.
Other developers I work with complain regularly that Seam is the way to go. They've said some pretty intelligent things in the past, but I really like Spring, and our project is married to it.
Wicket also looks interesting; Spring is very heavy on XML, and Wicket gets rid of that, which is a very happy thought. That said, there's a pretty huge gain with some of the XML in Spring, so I'd worry about losing that.
As an addition, I'd take a look at Apache Commons, formerly called Jakarta Commons. It's a collection of useful libraries of stuff that probably should be included in the core Java API. Discursive has a wonderful book that walks you through it.
if you are asking about tools interface to develop a web application.i suggest eclipse coz its too convenient to use,flexible and can run on pcs with low configurations.i have just 512 mb ram and i m using it easily
As a quickstart you could download Netbeans http://netbeans.org/ , you can try almost all kinds of J2EE technologies with a preconfigured app-server and database using included sample projects. It tooks only seconds to build and run and if you like analyze the code.
and here there is another one, a very young one this time: (ยต)Micro. It is open source, Apache 2 license, and available on Github. I built this framework for fun and I was trying to port and simulate as much functionality as I could from my experience as a Sinatra/Rails developer. Cheers!

Java for intermediate .NET Developer

I am a .NET Developer with about 5 years of web development experience using Microsoft technologies starting with classic ASP to ASP .NET 3.5.
I do have a little background in Java as well and can write/understand Java code very easily. I am looking for resources (online, books) that are compatible with my .NET experience. I am only interested in web development in Java and want to start at intermediate level even if it may require me to look up some details.
What path or resources would you recommend for intermediate .NET web developers to gain equivalent proficiency in Java web development tools?
Get an IDE: IntelliJ (my preference), Eclipse, Netbeans;
Get an application server: Glassfish (my preference; either v2 or v3 Prelude), JBoss or, if you're feeling adventurous, SpringSource dm server;
Get a JDK eg 6u11;
Get a copy of Spring 2.5.6 (or 3.0M2 if you're feeling adventurous);
Get a copy of the Spring reference documentation;
Put the pieces together.
Get yourself a copy of eclipse, and just start reading other people's code, and reading some java docs. The Java website has all the docs online, and they're one of the best language specs I've seen.
Really, if you know C# pretty well, Java should be a cake walk. The languages are pretty similar, so you just need to get to know the different frameworks.
Probably the most helpful book in regards to learning Java's web development framework, Java Server Faces:
Core JSF
- Great examples and explainations throughout on the whole process of using the JSF Framework.
And of course for anyone new to Java I would recommend:
Effective Java
- Covers various design patterns and practices.
In my experience that official Java exams are better than the .Net equivalents, so it may be worth starting there?
Search for a servlet/JSP tutorial (Good, albeit a little old, example here).
You'll get basic knowledge of Java web application development.
Whatever your level is, JSF for Nonbelievers is the best introduction to JSP/JSF.
I'd check out Bitter Java.
It's a book that shows you the wrong way to do things, why it's wrong, and then it shows you the right way.
It pretty much falls in line with the evolution of the j2ee framework. Started with servlets, then moved into jsps, then command pattern, etc.

Categories