Java EE is an open, standards-based development and deployment platform for creating distributed,
transactional, reliable, secure, multitiered, web-based, server-centric, component-based enterprise
applications
Java EE 8(JSRs approved on 22 Sep, 2014, Final Release is expected by the end of 2017)
Getting Started
Hello World
In this section you can see an example of HelloWorld program using Java Servlets.
// Import required java libraries
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
// Extend HttpServlet class
public class HelloWorld extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
// Set response content type
response.setContentType("text/html");
// Actual logic goes here.
PrintWriter out = response.getWriter();
out.println("<h1> Hello From Java!</h1>");
}
}
To run this program on Netbeans just click on the Run button and it automatically opens the default
browser.
Servlet
Java Object based on the Servlet API ; Runs in a server application to answer client requests;
technically, servlets are not tied to a specific client-server protocol, but they are most ; Runs in,
and is managed by, a web-tier container called the "Servlet Container‟
Typically asked with (among other things)
javax.servlet
javax.servlet.http
Servlet Processing
init(config)
service(request, response)
Destroy()
Servlet Request Processing (HttpServletRequest)
Servlet Response Processing (HttpServletResponse)
Servlet Request Dispatching (RequestDispatcher)
Obtain a RequestDispatcher to a resource (static or dynamic) from the request object
Include the dispatcher resource (or its output) in the current response; one or more resources
can be included (e.g. use for banners, footers, etc.)
rqstDsp.include(request, response);
Forwards the processing of the current request to the dispatcher resource; the servlet
processing the current request must not generate a response ( e.g. use in MVC “controller”
servlets)
import java.io.*;
import java.sql.Date;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Redirect extends HttpServlet{
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html");
String site = new String("http://www.webtek.com");
response.setStatus(response.SC_MOVED_TEMPORARILY);
response.setHeader("Location", site);
}
}
JSP
JSP are essentially Servlets, they are created for programmers, since writing html code in Servlets is quite
tedious and impractical, the Java engineers made JSPs (Java Server Pages) which can contain both HTML and
java code altogether. Upon execution JSPs go back with their original form - as a Servlet
JSP pages
are webpages that contain both static and dynamic contents.
static contents refer to page contents that are written in html, xml, etc.
dynamic contents of the page are constructed using jsp elements.
JSP page life cycle
JSP page translation and compilation - a code for writing out data into response stream is generated
from translating the static data.
attribute and attributeName refers to attributes that can modify tag behavior.
Running JSP page
create an html file having jsp contents enclosed in <% %>
save the file with .jsp extension
start server
copy or move the file to a folder in the server.
check if it is working properly by visiting its URL in the server using a browser.
JavaServer Pages Standard Tag Library (JSTL)
Definition
JavaServer Pages Standard Tag Library contains useful JSP tags for common JSP application
functionalities.
JSTL supports common structural tasks such as conditions and iterations as well as XML document
manipulation, tag internationalization, and tags for SQL.
Classification of tags
Core Tags
group of JSTL tags most commonly used
tag descriptions
<c:out> - displays an expression's result.
<c:set> - sets expression evaluation result.
<c:remove> - removes a variable.
<c:catch> - catches throws.
<c:if> - conditional tag that simply evaluates if specified condition is true.
<c:choose> - similar to switch in java.
<c:when> - similar to case in java switch statement.
<c:otherwise> - works somehow similar to else for choose tag.
<c:import> - includes contents from specified URL.
<c:forEach> - common iteration used for jstl.
<c:forTokens> - iterates over strings called tokens which are separated by
delimiters.
<c:param> - adds parameters to import tag.
<c:redirect> - redirects to URL specified.
<c:url> - creates URL.
Formatting Tags
group of JSTL tags for formatting text displays and some variables.
tag descriptions
<fmt:formatNumber> - formats number to specified format.
<fmt:parseNumber> - parses string specified as numbers.
<fmt:formatDate> - formats date specified.
<fmt:parseDate> - parses date using specified pattern.
<fmt:setLocale> - stores given locale.
<fmt:timeZone> - specify timezone for time formatting.
Spring MVC provides alot of functionality for making a robust Web Applications.
The Spring MVC Framework is designed in such a way that every piece of functionality is highly
configurable.
STRUTS
Struts is an open source framework that extends the Java Servlet API and employs a Model, View,
Controller (MVC) architecture. It enables you to create maintainable,
extensible, and flexible web applications based on standard technologies,
such as JSP pages, JavaBeans, resource bundles, and XML.
JSF
JavaServer Faces (JSF) is a Java specification for building component-based user interfaces for web
applications[1] and was formalized as a standard through the Java Community Process being part of the
Java Platform, Enterprise Edition. It is also a MVC web framework that simplifies to construct user
interfaces (UI) for server-based applications by using reusable UI components in a page.