Author: Vishal Layka
Pub Date: 2014
Size: 13 Mb
Learn Java for Web Development teaches web developers who are new to Java to use key Java skills, Java-based languages, and frameworks to build simple or complex web sites and applications. As soon as you pick up this book, Vishal Layka’s experience guides you on a very practical learning, then building, journey.
First, you learn the Java nuts and bolts necessary to build a simple “HelloWorld” Java (native) application.
Next, you learn to build a “HelloWorld” Java-based web application example that utilizes Servlets and JavaServer Pages (JSPs).
Then you’ll start the building blocks of your first real-world case study – a book store e-commerce application – by learning more about Servlets, JSPs, and adding JavaServer Faces (JSFs) and the Expressions found in each of these to the mix.
Then you build your web application using Apache Struts2 and Spring MVC framework.
The book finishes by exploring the web application that you’ve built and examining industry best practices and how these might fit with your application. Alternatively, you can explore the alternative Java “languages” and approaches available at the end of this book like Groovy/Grails and Scala/Lift.
Web development is still one of today’s most popular, active, and important programming and development activities. From a single web page to an e-commerce-enabled web site to a fully-fledged web application, the Java programming language and its frameworks allow you great flexibility and productivity for your web application development.
While reading this book, you’ll see all this in action and you can use it as a starting point. Study and experiment with the many source code examples, and later apply them to your own web application building endeavors and 2:00 AM challenges.
What you’ll learn
How to build your first Java-based “HelloWorld” Web application with Servlets and JSPs
How to build web applications with Expressions in Servlets and JSPs
How to build web applications with JSFs and Expressions
How to build web applications with the Struts2 APIs and framework
How to build web applications with the Spring 3 Web MVC framework
How to debug and deploy your web application along the way
Using Java web application best practices
How to build web applications with Java alternatives like Groovy and Grails and Scala
Who this book is for
This book is for current or aspiring web developers who have some programming experience, but have no or little prior experience using the Java programming language and the available frameworks for Java in web development.
Java Web Frameworks
You learned that MVC and the Java EE web tier patterns together facilitate the architectural foundation for building reusable, maintainable, and extensible web applications. As developers gather more experience, they start discovering generic objects that can be used over and over, and patterns begin to emerge. Once you have a collection of such generic objects, a framework begins to emerge. A frameworkis a collection of generic objects and other supporting classes that provide the infrastructure for application development. Frameworks are, essentially, a collection of design patterns guarded by the basic framework principles discussed next. A Java framework uses two types of patterns.
- OO patterns
- Java EE patterns
A framework uses OO patterns for its own construction to address the architectural problem domain, such as extensibility, maintainability, reusability, performance, and scalability. Both OO patterns and Java EE patterns address the business problem domain areas, such as processing requests, authentication, validation, session management, and view management to name a few. .Frameworks address these two major architectural and business problem areas by providing patterns base generic objects and supporting classes, closely guarded by the following key principles:
- Configurability: Configurability is the ability of the framework to be able to use the metadata to alter the behavior of the framework.
- Inversion of control: In traditional programming style, the problem domain code controls the flow of the application execution. Inversion of control refers to the technique where reusable code controls the execution of the problem domain code, thus controlling the flow of the application execution.
- Loose coupling: This principle refers to the independence of the collaborating classes in the framework with which each collaborating class can be altered without influencing the other collaborating class.
- Separation of concerns: This principle refers to the need to classify the problem domain areas and deal with them in an isolated manner so that the concerns of one problem area do not influence the concerns of another problem area. The multitiered Java EE architecture we saw in Chapter 1 is driven by the principle of the separation of concerns.
- Automating common functionalities: A framework provides mechanisms for automated solutions to the mundane functionalities of the domain.
Why Use a Framework?
While Java EE does a great job of standardizing the enterprise infrastructure and providing an application model, there are few major problems associated with it.
- Interacting directly with the Java EE components often results in massive boilerplate code and even code redundancy.
- You have to write code for dealing with common business domain problems.
- You have to write code for solving architectural domain problems.
You could roll your own framework to address the problems associated with building Java EE based web applications using OO patterns and Java EE patterns. But writing an in-house framework entails efforts that are orthogonal to the business goals of the application; in addition, the in-house framework is unlikely to be upgraded, and the new versions of the in-house framework will never see the sun, unlike mainstream frameworks that continuously evolve instead of falling into architectural entropy. With that in mind, it’s time to look at some of the available JVM-based web frameworks (see Table 3-25). This table is far from exhaustive; a myriad of frameworks are available, but this book will
cover the most successful JVM-based web frameworks listed in the table.
All the web frameworks listed in Table 3-25follow the MVC Model-2 architecture, the basic architectural pattern you learned about in Chapter 2. In a typical web application, there are several tasks that you want to do for every incoming request, such as encryption. A single controller for the web application lets you centralize all the tasks that the controller has to perform in a web application, such as the following:
- Centralizing the logic for dispatching requests to views
- Checking whether the user requesting an operation has valid authorization
As the MVC-based web application grows larger with more and more views to display, the controller in the MVC-application model becomes a procedural object that takes too many decisions to render those views. This problem can be alleviated by using the front controller and configuration metadata, instead of using pure MVC architecture. This solution is illustrated in Figure 3-19.
When the HTTP request arrives from the client, the front controller looks up in the configuration metadata file to decide on the right application controller that should handle theHTTP request. The application controller consists ofthe business logic invocations and presentation logic controls for the HTTP request.The difference between using pure MVC and the Front Controller pattern with MVC is that the front controller servlet looks up the configuration metadata to figure out the application controller that handles the HTTP request processing instead of itself deciding to call specific HTTP request handler objects.
In essence, this is the pattern that is common to many leading web frameworks. For instance, Struts 2 and Spring Web MVC are front controller–based MVC frameworks that delegate the control to the application controller using configuration metadata stored in, for example, an XML file. In Struts 2, the ServletFilteris a manifestation of the front controller in Figure 3-19. In JSF 2, it’s FacesServlet;
in Spring Web MVC, it’s DispatcherServlet; and in Grails 2, it’s the subclass of DispatcherServlet.
This concludes the chapter. From the next chapter onwards, this book will navigate through the labyrinth of Web frameworks that capitalize on the strengths of Java EE and JVM.
This chapter focused on evolving the technologies and tools used in a Java EE project. The chapter introduced you to the common problems associated with developing a web application and showed how these problems can be solved and how web applications can be made reusable, maintainable, and extensible by using tools (such as unified EL) available with Java EE.
EL and JSTL are two picture-perfect implementations of the theory of the separation of business and view concerns, and they can be used to build reusable, easy-to-maintain, function-rich web applications. This chapter also introduced you to the Java EE web tier patterns and how these JavaEE patterns provide solutions to the recurring problems in a particular context. Then the chapter showed how frameworks address architectural and business problem areas by providing patterns-based generic objects and supporting classes. Finally, the chapter showed how web frameworks relinquish you from using Java EE design patterns by providing you out-of-the-box best-practice solutions.