CSC 224 Lecture Notes
Week 10: J2EE- EJB, Servlets, and JSP
Wrapping things up... [1/11]
Server Side Enterprise Java [2/11]
Benefits of Middle-Tier Servers [3/11]
J2EE Architecture [4/11]
Enterprise Beans [5/11]
Coding the Enterprise Bean [6/11]
A Client [7/11]
Servlet [8/11]
Dyanamic output from POST [9/11]
JSP [10/11]
Bye, bye [11/11]
Wrapping things up... [1/11]
-Make sure to check the grades and that there are no problems
-Let's go over the homework
-Any questions before the quiz? You are free to leave after this quiz, it won't hurt my feelings, everything we cover from this point on is beyond
the required material for this course.
-DL STUDENTS YOUR FINAL IS NEXT WEEK, MONDAY THE 24TH, LET ME KNOW IF YOU HAVE ANY QUESTIONS
Server Side Enterprise Java [2/11]
- Enterprise JavaBeans (EJB) technology is part of a larger distributed framework-- the Java 2 Platform, Enterprise Edition (J2EE).
- Enterprise applications require system-level services, which the J2EE provides
- transaction management
- security
- client connectivity
- database access.
- Allows you to focus on the business logic in your applications, not the system-level plumbing.
- You code the business logic in enterprise beans, reusable components that can be accessed by client programs.
- Enterprise beans run on a J2EE server, which functions as a middle-tier server in a three-tier client/server system.
Benefits of Middle-Tier Servers [3/11]
- Provide business services to clients.
- a middle-tier server in an online shopping application might provide a variety of services: catalog lookup, order entry, and credit verification.
- Also provide system-level services:
- Remote access to clients and back-office systems
- Session and transaction management
- Security enforcement
- Resource pooling
-
Because the middle-tier provides these services, the clients can be thin, simple, and rapidly developed.
- You can integrate new clients with existing applications and databases, protecting your investment in legacy systems.
J2EE Architecture [4/11]
-
The J2EE Server
-
Naming and Directory - allows programs to locate services and components through the Java
Naming and Directory Interface (JNDI) API
-
Authentication - enforces security by requiring users to log in
-
HTTP - enables Web browsers to access servlets and JavaServer Pages (JSP) files
-
EJB - allows clients to invoke methods on enterprise beans
- EJB Container
- Transaction Management- The code required to control distributed transactions (method calls) can be quite complex.
- Security-
The container permits only authorized clients to invoke an enterprise bean's methods.
- Remote Client Connectivity-
The container manages the low-level communications between clients and enterprise beans.
-
Life Cycle Management-The container moves in and out of active states
-
Database Connection Pooling-The container manages a pool of database connections.
-
Web Container
- JSP and Servlets, we'll discuss soon...
Enterprise Beans [5/11]
Enterprise beans are server components written in the Java programming language.
- Enterprise beans DO contain the business logic for your application.
- A checkbook client might invoke the debit and credit methods of an account enterprise bean.
- EJB'S do NOT!!
-
Managing or synchronizing threads
-
Accessing files or directories with the java.io package
-
Using AWT functionality to display information or to accept information from a keyboard
-
Listening on a socket, accepting connections on a socket, or using a socket for multicast
-
Setting a socket factory used by ServerSocket, Socket, or the stream handler factory used by the URL class
-
Loading a native library
There are two types of enterprise beans: session beans and entity beans.
|
Session Bean
|
Entity Bean
|
---|
Purpose
|
Performs a task for a client.
|
Represents a business entity object that exists in persistent storage.
|
Shared Access
|
May have one client.
|
May be shared by multiple clients.
|
Persistence
|
Not persistent. When the client terminates its session bean is no longer available.
|
Persistent. Even when the EJB container terminates, the entity state remains in a database.
|
Coding the Enterprise Bean [6/11]
- Every enterprise bean requires the following code:
- Remote interface
- Home interface
- Enterprise bean class
Coding the Remote Interface
A remote interface defines the business methods that a client may call. The business methods are implemented in the enterprise bean code..
import javax.ejb.EJBObject;
import java.rmi.RemoteException;
public interface Converter extends EJBObject {
public double dollarToYen(double dollars) throws RemoteException;
public double yenToEuro(double yen) throws RemoteException;
}
Coding the Home Interface
A home interface defines the methods that allow a client to create, find, or remove an enterprise bean. The ConverterHome
interface contains a single create
method, which returns an object of the remote interface type.
import java.io.Serializable;
import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.EJBHome;
public interface ConverterHome extends EJBHome {
Converter create() throws RemoteException, CreateException;
}
Coding the Enterprise Bean Class
The enterprise bean in our example is a stateless session bean called ConverterEJB
. This bean implements the two business methods, dollarToYen
and yenToEuro
, that the Converter
remote interface defines.
import java.rmi.RemoteException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
public class ConverterEJB implements SessionBean {
public double dollarToYen(double dollars) {
return dollars * 121.6000;
}
public double yenToEuro(double yen) {
return yen * 0.0077;
}
public ConverterEJB() {}
public void ejbCreate() {}
public void ejbRemove() {}
public void ejbActivate() {}
public void ejbPassivate() {}
public void setSessionContext(SessionContext sc) {}
}
A Client [7/11]
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
import Converter;
import ConverterHome;
public class ConverterClient {
public static void main(String[] args) {
try {
Context initial = new InitialContext();
Object objref = initial.lookup("MyConverter");
ConverterHome home =
(ConverterHome)PortableRemoteObject.narrow(objref,
ConverterHome.class);
Converter currencyConverter = home.create();
double amount = currencyConverter.dollarToYen(100.00);
System.out.println(String.valueOf(amount));
amount = currencyConverter.yenToEuro(100.00);
System.out.println(String.valueOf(amount));
currencyConverter.remove();
} catch (Exception ex) {
System.err.println("Caught an unexpected exception!");
ex.printStackTrace();
}
}
}
Servlets [8/11]
- Servlets are programs
that run on a Web server and build Web pages dynamically.
- The Web page is based on data submitted by the user.
- Results pages from search engines
- E-Commerce, check DB for price, availability
- The data changes frequently.
- Weather-reports
- News headlines
- Why is this better than CGI
- Efficient; With traditional CGI, a new process is started for each HTTP request, java uses light weight threads
- Much more powerful (talk directly to the server, maintain memory from client to client, talk to Java backend, DB pooling, etc.)
- More portable, you needn't change the code from server to server
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWorld extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
PrintWriter out = response.getWriter();
out.println("Hello World");
}
}
Dyanamic output from POST
[9/11]
PostForm.html:
A Sample FORM using POST
A Sample FORM using POST
ShowParameter.java:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
/** Shows all the parameters sent to the servlet via either
* GET or POST. Specially marks parameters that have no values or
* multiple values.
*/
public class ShowParameters extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "Reading All Request Parameters";
out.println(ServletUtilities.headWithTitle(title) +
"\n" +
"" + title + "
\n" +
"\n" +
"\n" +
"Parameter Name | Parameter Value(s)");
Enumeration paramNames = request.getParameterNames();
while(paramNames.hasMoreElements()) {
String paramName = (String)paramNames.nextElement();
out.println(" |
" + paramName + "\n | ");
String[] paramValues = request.getParameterValues(paramName);
if (paramValues.length == 1) {
String paramValue = paramValues[0];
if (paramValue.length() == 0)
out.print("No Value");
else
out.print(paramValue);
} else {
out.println("");
for(int i=0; i" + paramValues[i]);
}
out.println(" ");
}
}
out.println(" |
\n