Overview To J2EE: By: Jayita Mukhopadhyay
Overview To J2EE: By: Jayita Mukhopadhyay
By:
Jayita Mukhopadhyay
Why J2EE?
Presentation world is web-centric
Tried other approaches
Static HTML, CGI, Applets, ASP
Need integration with campus-wide services
Capitalize on developer’s strengths
Programming
Presentation
DBM
Framework is being widely adopted by vendors
2
Evolution of J2EE
Advantages of separating view from business logic and data
storage became apparent (MVC Design Pattern)
3
J2EE Architecture
Java Technologies
External
Application
4
Web APIs
5
Initial Days Applets
Applets
Problems
Require correct JVM on client
6
Servlets
Servlets
7
Anatomy of a Servlet
init() – the init() function is called when the servlet is
initialized by the server. This often happens on the first
doGet() or doPut() call of the servlet.
destroy() – this function is called when the servlet is
being destroyed by the server, typically when the
server process is being stopped.
8
Anatomy of a Servlet
doGet() – the doGet() function is called when the servlet is
called via an HTTP GET.
doPost() – the doPost() function is called when the servlet
is called via an HTTP POST.
POSTs are a good way to get input from HTML forms
9
Sample Servlet
import java.io.*; //Apache Tomcat sample code
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWorld extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter(); out.println("<html>");
out.println("<body>"); out.println("<head>");
out.println("<title>Hello World!</title>");
out.println("</head>"); out.println("<body>");
out.println("<h1>Hello World!</h1>");
out.println("</body>"); out.println("</html>");
}
}
10
How do Servlet Engines work?
HTTPD
GET/POST HTML
HTML
T
GET or POS
Server
Servlet Runner
11
Problems with Java Servlets
Any changes to even a simple output required recompiling the
Servlet, uploading it, and restarting the Servlet engine
(disruption in service)
All the View, Data storage/access, and Business logic are
together in multiple servlets
Difficult to maintain over time
jPortal has >100 servlets and all have database access and
12
Problem output from Servlets
HTML output
from a Servlet
13
JSP: Dealing with the output
HTML output in Servlets had to be ‘printed’ into the outgoing
stream
Hard to maintain – any changes to the ‘look&feel’ required
programmers to change source code, recompile, stop the
server, upload the new servlet, start the server again
Solution – Java Server Pages
Invented by Paul Colton of LiveSoftware, first company to have a
JSP solution for its servlet container
JSP are files that have java source code embedded within <%...
%> tags among HTML – they are specialized web pages.
The servlet engine simply converts everything outside these
tags into output print statements on the fly
Programmers can edit the HTML in JSPs using an HTML editor
The JSP/servlet engine will covert the HTML lines into output print
statements and ‘recompile’ into bytecode and reload into memory ---
dynamic reloading
14
JSP: Introduction
Java Server Pages
Interactive web pages
<jsp:include page=“/docs/footer.html”/>
<h2><bean:write property="pageTitle“ name="swfForm"/></h2>
<% String tempUrl = “http://www.rpi.edu”; %>
Extension of Servlet technology
Similar to ASP
Standard Tag Libraries available
Struts:
MVC framework
Industry standard
Extensible
Can define and implement tag libraries
15
Example JSP page
16
How JSP works
1. HTTP-Browser requests
JSP page
2. Parser recognizes JSP
HTML extension means file
HTTPD
should be given to
jsp page (html+java) JSP/Servlet engine rather
than sent back to browser
JSP Converter 3. JSP container converts
JSP into a java servlet
4. Java servlet is compiled
Servlet Runner ‘on the fly’ and given to the
Java servlet runner
servlet 5. Servlet runner loads servlet
Servlets and executes it
6. Output from the servlet is
given to the HTTPD for
return to the browser
17
Problems with JSP
Java code in HTML pages – difficult to read the java
code and debug
Looping is difficult/error-prone
18
Enterprise JavaBeans (EJB)
EJB Container:
“Manages security, transactions, and state management, multi-
threading, resource pooling, clustering, distributed naming,
automatic persistence, remote invocation, transaction boundary
management, and distributed transaction management”
19
EJB
Enterprise Java Bean
Bean
originally a Java class with get() and set() methods
20
EJB – Entity Bean
Entity Bean
21
EJB – Session Bean
Session Bean
Stateless
22
EJB – Message Driven Bean
Message Driven Bean
session facade
***** *
message queue
*
APPLICATION
23
When to use EJB?
EJB’s are not required components of a web
application – web apps must be servlets, but
can be JSP, and sometimes can invoke EJBs
EJBs are an advantage when:
one has business logic that is easily
compartmentalized
one needs/wants a high degree of scalability (for
example, distribute processing among multi-
machine cluster)
24
JDBC
Java DataBase Connectivity
25
JTA
Java Transaction API
Specifies standard interfaces between a transaction manager
(e.g. JBoss) and the parties involved in a distributed transaction
system:
the resource manager
26
JMS
Java Message Service
Publish/subscribe messaging
each message may have
multiple consumers
27
JNDI
Java Naming & Directory Interface
28
JAAS
Java Authentication and Authorization Service
29
Java tools for Web Applications
Java Servlets
Java Server Pages (JSP)
Java Tag Libraries
Jakarta Struts
JDBC, JMS
Enterprise Java Beans (EJB)
JavaMail (all e-mail capabilities)
JNDI (java naming and directory services)
Emerging additional frameworks
Hibernate (object-relational persistence bridge)
AspectJ (cross cutting code insertion)
30
J2EE Applications
31
Anatomy of J2EE Web Application
32
THANK YOU