3. 5. J2EE Coding Conventions Rules
The sources for these J2EE coding conventions are the different API specifications. Many of the J2EE coding rules can be verified with the J2EE Verifier tool included in the J2EE reference implementation from Sun Microsystems.
Overview
Table A.5. J2EE Coding Conventions Overview
| Rules |
| JEC_001: Do Not Create A Class Loader (High) |
| JEC_002: Do Not Read Or Write A File Descriptor (High) |
| JEC_003: Do Not Use A AWT, Swing Or Other UI API In An EJB (High) |
| JEC_004: Do Not Attempt To Load A Native Library In An EJB (High) |
| JEC_005: Do Not Attempt To Obtain The Security Policy Information In An EJB (High) |
| JEC_006: Do Not Attempt To Set The Socket Factory In An EJB (High) |
| JEC_007: Do Not Attempt To Listen On A Socket In An EJB (High) |
| JEC_008: Do Not Attempt To Use The Subclass Or Object Substitution Features Of The Java Serialization Protocol (High) |
| JEC_009: Do Not Attempt To Manage A Thread (High) |
| JEC_010: Do Not Use The java.io Package To Access The File System (Normal) |
| JEC_011: Do Not Pass An EJB's this Reference As An Argument Or Method Result (Enforced) |
| JEC_012: Do Not Throw A RemoteException From An EJB Implementation Method (High) |
| JEC_013: Make All EJB interfaces and classes public (High) |
| JEC_014: Do Not Make A EJB implementation class final (High) |
| JEC_015: Provide An ejbCreate Method For A Session Bean (High) |
| JEC_016: Provide A public Default Constructor For A EJB implementation class (High) |
| JEC_017: Do Not Override The finalize Method For A EJB implementation class (High) |
| JEC_018: Return void For An ejbCreate Method Of A Session Bean (High) |
| JEC_019: Return The EJB Remote Interface For A create Method Of An EJB Remote Home Interface (Enforced) |
| JEC_020: Make A create Method Of An EJB Home Interface Throw An javax.ejb.CreateException (Enforced) |
| JEC_022: Do Not Declare A Finder Method For A CMP EJB (High) |
| JEC_024: Match An ejbPostCreate Method To An ejbCreate Method For An Entity Bean (High) |
| JEC_025: Declare An ejbCreate Method public For An Entity Bean (High) |
| JEC_026: Return The Primary Key For An ejbCreate[Name] Method Of An Entity Bean (High) |
| JEC_027: Do Not Declare An ejbCreate Or ejbPostCreate Method As final Or static For An Entity Bean (High) |
| JEC_028: Return void For An ejbPostCreate Method Of An Entity Bean (High) |
| JEC_029: Declare An ejbFindByPrimaryKey Method For A BMP EJB (High) |
| JEC_030: Declare A ejbFind Method public For A BMP EJB (High) |
| JEC_031: Do Not Declare A ejbFind Method final Or static (High) |
| JEC_032: Make A find Method Of An EJB Home Interface throw javax.ejb.FinderException (High) |
| JEC_033: Declare An ejbSelect Method Of An Entity Bean public (High) |
| JEC_034: Make An ejbSelect Method Of An Entity Bean throw javax.ejb.FinderException (High) |
| JEC_035: Return The EJB Local Interface For A create Method In An EJB Local Home Interface (Enforced) |
| JEC_036: Declae An ejbPostCreate Method Of An Entity Bean public (High) |
| JEC_037: Make A Method Of An EJB Remote Interface Throw java.rmi.RemoteException (Enforced) |
| JEC_038: Make A Method Of An EJB Remote Home Interface Throw java.rmi.RemoteException (Enforced) |
| JEC_039: Do Not Make A Method Of An EJB Local Interface Throw java.rmi.RemoteException (Enforced) |
| JEC_040: Do Not Make A Method Of An EJB Local Home Interface Throw java.rmi.RemoteException (Enforced) |
| JEC_042: Declare An ejbSelect Method Of A CMP EJB abstract (High) |
| JEC_043: Make An ejbCreate Method Of An Entity Bean Throw javax.ejb.CreateException (High) |
| JEC_044: Declare An ejbHome Method Of An Entity Bean public (High) |
| JEC_045: Do Not Declare An ejbHome Method Of An Entity Bean final Or static (High) |
| JEC_046: Do Not Make An ejbHome Method Of An Entity Bean Throw java.rmi.RemoteException (High) |
| JEC_047: Make A Message Bean Implementation Implement javax.jms.MessageListener (High) |
| JEC_052: Provide An ejbCreate Method For A Message Bean (High) |
| JEC_053: Return void For An ejbCreate Method Of A Message Bean (High) |
| JEC_054: Do Not Declare Arguments For An ejbCreate Method Of A Message Bean (High) |
| JEC_055: Provide A Valid RMI Method Signature For An EJB Remote Home Interface (High) |
| JEC_056: Do No Print Unnecessary Static Content From A Servlet (High) |
| JEC_057: Use Custom JSP Tags Instead Of Scriptlets (High) |
| JEC_058: Do Not Forward A Request From A JSP Page (High) |
JEC_001: Do Not Create A Class Loader (High)
Allowing the enterprise bean to access information about other classes and to access the classes in a manner that is normally disallowed by the Java programming language could compromise security. The enterprise bean must not attempt to create a class loader; obtain the current class loader; set the context class loader; set security manager; create a new security manager; stop the JVM; or change the input, output, and error streams. (See EJB20SPEC - C.1.2 - Programming restrictions)
These functions are reserved for the EJB Container. Allowing the enterprise bean to use these functions could compromise security and decrease the Container's ability to properly manage the runtime environment.
JEC_002: Do Not Read Or Write A File Descriptor (High)
The enterprise bean must not attempt to directly read or write a file descriptor. Allowing the enterprise bean to read and write file descriptors directly could compromise security. (See EJB20SPEC - C.1.2 - Programming restrictions)
JEC_003: Do Not Use A AWT, Swing Or Other UI API In An EJB (High)
An enterprise bean must not use the AWT functionality to attempt to output information to a display, or to input information from a keyboard. Most servers do not allow direct interaction between an application program and a keyboard/display attached to the server system. (See EJB20SPEC - C.1.2 - Programming restrictions)
JEC_004: Do Not Attempt To Load A Native Library In An EJB (High)
The enterprise bean must not attempt to load a native library. This function is reserved for the EJB Container. Allowing the enterprise bean to load native code would create a security hole. (See EJB20SPEC - C.1.2 - Programming restrictions)
JEC_005: Do Not Attempt To Obtain The Security Policy Information In An EJB (High)
The enterprise bean must not attempt to obtain the security policy information for a particular code source. Allowing the enterprise bean to access the security policy information would create a security hole. (See EJB20SPEC - C.1.2 - Programming restrictions)
JEC_006: Do Not Attempt To Set The Socket Factory In An EJB (High)
The enterprise bean must not attempt to set the socket factory used by ServerSocket, Socket, or the stream handler factory used by URL. These networking functions are reserved for the EJB Container. Allowing the enterprise bean to use these functions could compromise security and decrease the Container's ability to properly manage the runtime environment. (See EJB20SPEC - C.1.2 - Programming restrictions)
JEC_007: Do Not Attempt To Listen On A Socket In An EJB (High)
An enterprise bean must not attempt to listen on a socket, accept connections on a socket, or use a socket for multicast. The EJB architecture allows an enterprise bean instance to be a network socket client, but it does not allow it to be a network server. Allowing the instance to become a network server would conflict with the basic function of the enterprise bean-- to serve the EJB clients. (See EJB20SPEC - C.1.2 - Programming restrictions)
JEC_008: Do Not Attempt To Use The Subclass Or Object Substitution Features Of The Java Serialization Protocol (High)
The enterprise bean must not attempt to use the subclass and object substitution features of the Java Serialization Protocol. Allowing the enterprise bean to use these functions could compromise security. (See EJB20SPEC - C.1.2 - Programming restrictions)
JEC_009: Do Not Attempt To Manage A Thread (High)
The enterprise bean must not attempt to manage threads. The enterprise bean must not attempt to start, stop, suspend, or resume a thread; or to change a thread's priority or name. The enterprise bean must not attempt to manage thread groups. These functions are reserved for the EJB Container. Allowing the enterprise bean to manage threads would decrease the Container's ability to properly manage the runtime environment. (See EJB20SPEC - C.1.2 - Programming restrictions)
JEC_010: Do Not Use The java.io Package To Access The File System (Normal)
An enterprise bean must not use the java.io package to attempt to access files and directories in the file system. The file system APIs are not well-suited for business components to access data. Business components should use a resource manager API, such as JDBC API, to store data. (See EJB20SPEC - C.1.2 - Programming restrictions)
JEC_011: Do Not Pass An EJB's this Reference As An Argument Or Method Result (Enforced)
The enterprise bean must not attempt to pass 'this' as an argument or method result. The enterprise bean must pass the result of the methods SessionContext.getEJBObject() or EntityContext.getEJBObject() instead. (See EJB20SPEC - C.1.2 - Programming restrictions)
JEC_012: Do Not Throw A RemoteException From An EJB Implementation Method (High)
The EJB 2.0 specification no longer allows for methods in the EJB implementation to throw a java.rmi.RemoteException. This only goes for methods that are defined in the EJBs remote or home interface.
If the enterprise bean method encounters a system-level exception or error that does not allow the method to successfully complete, the method should throw a suitable non-application exception that is compatible with the method's throws clause. While the EJB specification does not prescribe the exact usage of the exception, it encourages the Bean Provider to follow these guidelines:
If the bean method encounters a RuntimeException or error, it should simply propagate the error from the bean method to the container (i.e., the bean method does not have to catch the exception).
If the bean method performs an operation that results in a checked exception that the bean method cannot recover, the bean method should throw the javax.ejb.EJBException that wraps the original exception.
JEC_013: Make All EJB interfaces and classes public (High)
Enterprise bean classes must be defined as public . (See EJB20SPEC - 7.10.2, 10.6.2, 12.2.2, 15.7.2)
WRONG
class FooBean implements SessionBean {
}
RIGHT
public class FooBean implements SessionBean {
}
JEC_014: Do Not Make A EJB implementation class final (High)
Enterprise bean implementation classes must not be declared final. (See EJB20SPEC - 7.10.2, 15.7.2)
WRONG
public final class FooBean implements SessionBean {
}
RIGHT
public class FooBean implements SessionBean {
}
JEC_015: Provide An ejbCreate Method For A Session Bean (High)
Enterprise session beans must implement one or more ejbCreate method) whose signatures must also be public and return void. (See EJB20SPEC - 7.10.3.)
WRONG
public class FooBean implements SessionBean {
}
RIGHT
public class FooBean implements SessionBean {
public void ejbCreate() throws CreateException {
}
}
 | Note See also rule JEC_018: Return void For An ejbCreate Method Of A Session Bean (High). |
JEC_016: Provide A public Default Constructor For A EJB implementation class (High)
Enterprise bean implementation classes must have a public default constructor. The Container uses this constructor to create the bean instances. (See EJB20SPEC - 7.10.2, 10.6.2, 12.2.2 and 15.7.2).
WRONG
public class FooBean implements SessionBean {
public FooBean(int bar) {
}
}
RIGHT
public class FooBean implements SessionBean {
}
 | Note It is recommended not to provide any constructor al all. |
JEC_017: Do Not Override The finalize Method For A EJB implementation class (High)
Enterprise bean implementation classes must not override the finalize method. (See EJB20SPEC - 7.10.2, 10.6.2, 12.2.2 and 15.7.2)
WRONG
public class FooBean implements SessionBean {
protected void finalize() {
super.finalize();
}
}
RIGHT
public class FooBean implements SessionBean {
}
 | Note See also rule JAC_045: Do Not Unnecessary Override The finalize Method (High) |
JEC_018: Return void For An ejbCreate Method Of A Session Bean (High)
Enterprise session beans must declare one or more ejbCreate methods of which the return type must be void. (See EJB20SPEC - 7.10.3.)
WRONG
public class FooBean implements SessionBean {
public String ejbCreate() throws CreateException {
}
}
RIGHT
public class FooBean implements SessionBean {
public void ejbCreate() throws CreateException {
}
}
JEC_019: Return The EJB Remote Interface For A create Method Of An EJB Remote Home Interface (Enforced)
The create methods declared in a remote home interface must return a remote interface of type ejbObject. (See EJB20SPEC - 7.10.6, 10.6.10 and 12.2.9.)
WRONG
public interface FooHome extends EJBHome {
Foo create() throws CreateException, RemoteException;
}
public interface Foo {
}
RIGHT
public interface FooHome extends EJBHome {
Foo create() throws CreateException, RemoteException;
}
public interface Foo extends EJBObject {
}
JEC_020: Make A create Method Of An EJB Home Interface Throw An javax.ejb.CreateException (Enforced)
The create methods declared in a home interface (remote or local) must specify javax.ejb.CreateException in the throws clause. (See EJB20SPEC - 7.10.6, 10.6.10, 10.6.12, 12.2.9 and 12.2.11.)
WRONG
import java.rmi.RemoteException;
public interface FooHome extends EJBHome {
public abstract Foo create() throws RemoteException;
}
RIGHT
import java.rmi.RemoteException;
import javax.ejb.CreateException;
public interface FooHome extends EJBHome {
public abstract Foo create() throws CreateException, RemoteException;
}
JEC_022: Do Not Declare A Finder Method For A CMP EJB (High)
Enterprise entity beans that are declared abstract use container managed persistency. Such beans must not declare finder methods (ejbFindXXX) as they are provided by the EJB container. (See EJB20SPEC - 10.5.2.)
WRONG
public abstract class FooBean implements EntityBean {
public FooBean() {
}
public Collection ejbFindBar(int index) {
}
}
RIGHT
public abstract class FooBean implements EntityBean {
public FooBean() {
}
}
public interface FooHome extends EJBHome {
public abstract Collection findBar(int index)
throws FinderException, RemoteException;
}
JEC_024: Match An ejbPostCreate Method To An ejbCreate Method For An Entity Bean (High)
Each ejbPostCreate method in an enterprise entity bean must match an ejbCreate method with the same arguments. (See EJB20SPEC - 10.6.5 and 12.2.4.)
WRONG
public String ejbCreate(String fooId, String bar) throws CreateException {
}
public void ejbPostCreate(String fooId) throws CreateException {
}
RIGHT
public String ejbCreate(String fooId, String bar) throws CreateException {
}
public void ejbPostCreate(String fooId, String bar) throws CreateException {
}
JEC_025: Declare An ejbCreate Method public For An Entity Bean (High)
Enterprise beans must declare ejbCreate methods with public access. (See EJB20SPEC - 7.10.3, 10.6.4, 10.6.5, 12.2.3, 12.2.4 and 15.7.3.)
WRONG
private String ejbCreate(String fooId, String bar) throws CreateException {
}
RIGHT
public String ejbCreate(String fooId, String bar) throws CreateException {
}
JEC_026: Return The Primary Key For An ejbCreate[Name] Method Of An Entity Bean (High)
Enterprise entity beans must declare ejbCreate methods to return the bean's primary key type. (See EJB20SPEC - 10.6.4 and 12.2.3.)
[Caution] Caution
CMP Entity beans should return null as their primary key value (See EJB20SPEC - 10.6.2.)
WRONG
private void ejbCreate(String fooId, String bar) throws CreateException {
}
RIGHT
public String ejbCreate(String fooId, String bar) throws CreateException {
return fooId; }
JEC_027: Do Not Declare An ejbCreate Or ejbPostCreate Method As final Or static For An Entity Bean (High)
The ejbCreate and ejbPostCreate methods defined in an enterprise bean must not be declared final or static. (See EJB20SPEC - 7.10.3, 10.6.4, 10.6.5, 12.2.3, 12.2.4 and 15.7.3.)
WRONG
public static String ejbCreate(String fooId, String bar) throws CreateException {
}
public final void ejbPostCreate(String fooId, String bar) throws CreateException {
}
RIGHT
public String ejbCreate(String fooId, String bar) throws CreateException {
}
public void ejbPostCreate(String fooId, String bar) throws CreateException {
}
JEC_028: Return void For An ejbPostCreate Method Of An Entity Bean (High)
Enterprise entity beans must declare ejbPostCreate methods that return void. (See EJB20SPEC - 10.6.5 and 12.2.4.)
WRONG
public String ejbPostCreate(String fooId, String bar) throws CreateException {
}
RIGHT
public void ejbPostCreate(String fooId, String bar) throws CreateException {
}
JEC_029: Declare An ejbFindByPrimaryKey Method For A BMP EJB (High)
Enterprise entity beans with Bean Managed Persistency must implement the ejbFindByPrimaryKey method. (See EJB20SPEC - 12.2.5.)
RIGHT
import javax.ejb.ObjectNotFoundException;
public String ejbFindByPrimaryKey(String pk) throws ObjectNotFoundException {
return pk;
}
JEC_030: Declare A ejbFind Method public For A BMP EJB (High)
Enterprise entity beans with Bean Managed Persistence must implement finder methods (ejbFindXXX) with public access. (See EJB20SPEC - 12.2.5.)
WRONG
Collection ejbFindBigFoos(double greaterThan) {
return collection;
}
RIGHT
public Collection ejbFindBigFoos(double greaterThan) {
return collection;
}
JEC_031: Do Not Declare A ejbFind Method final Or static (High)
Finder methods (ejbFindXXX) defined in an enterprise entity bean with Bean Managed Persistence must not be declared as final or static. (See EJB20SPEC - 12.2.5.)
WRONG
public static String ejbFindByPrimaryKey(String pk) throws ObjectNotFoundException {
return pk;
}
public final Collection ejbFindBigFoos(double greaterThan) {
return collection;
}
RIGHT
public String ejbFindByPrimaryKey(String pk) throws ObjectNotFoundException {
return pk;
}
public Collection ejbFindBigFoos(double greaterThan) {
return collection;
}
JEC_032: Make A find Method Of An EJB Home Interface throw javax.ejb.FinderException (High)
Finder methods (findXXX) defined in a home interface (remote or local) must include javax.ejb.FinderException in the throws clause. (See EJB20SPEC - 10.6.10, 10.6.12, 12.2.9 and 12.2.11.)
WRONG
public interface FooHome extends EJBHome {
public Foo findByPrimaryKey(String pk) throws RemoteException;
}
RIGHT
public interface FooHome extends EJBHome {
public Foo findByPrimaryKey(String pk) throws FinderException, RemoteException;
}
JEC_033: Declare An ejbSelect Method Of An Entity Bean public (High)
The ejbSelectXXX methods defined in an enterprise entity bean must be declared as public and abstract. (See EJB20SPEC - 10.6.7.)
WRONG
import javax.ejb.FinderException;
private Set ejbSelectAfterDateFoo(Date givenDate) throws FinderException;
RIGHT
import javax.ejb.FinderException;
public abstract Set ejbSelectAfterDateFoo(Date givenDate) throws FinderException;
JEC_034: Make An ejbSelect Method Of An Entity Bean throw javax.ejb.FinderException (High)
Select methods (ejbSelectXXX) defined in an enterprise entity bean must include javax.ejb.FinderException in the throws clause. (See EJB20SPEC - 10.6.7.)
WRONG
private Set ejbSelectAfterDateFoo(Date givenDate);
RIGHT
import javax.ejb.FinderException;
public abstract Set ejbSelectAfterDateFoo(Date givenDate) throws FinderException;
JEC_035: Return The EJB Local Interface For A create Method In An EJB Local Home Interface (Enforced)
The create methods declared in a local home interface must return a local interface type (a type that implements ejbLocalObject). (See EJB20SPEC - 10.6.12 and 12.2.11.)
JEC_036: Declae An ejbPostCreate Method Of An Entity Bean public (High)
Enterprise entity beans must declare ejbPostCreate methods with public access. (See EJB20SPEC - 10.6.5 and 12.2.4.)
WRONG
private void ejbPostCreate(String fooId, String bar) throws CreateException {
}
RIGHT
public void ejbPostCreate(String fooId, String bar) throws CreateException {
}
JEC_037: Make A Method Of An EJB Remote Interface Throw java.rmi.RemoteException (Enforced)
All methods declared in a remote interface type must include java.rmi.RemoteException in the throws clause. (See EJB20SPEC - 7.10.5, 10.6.9 and 12.2.8.)
WRONG
import java.rmi.RemoteException;
import javax.ejb.EJBObject;
public interface Foo extends EJBObject {
void doSomething();
}
RIGHT
import java.rmi.RemoteException;
import javax.ejb.EJBObject;
public interface Foo extends EJBObject {
void doSomething() throws RemoteException;
}
JEC_038: Make A Method Of An EJB Remote Home Interface Throw java.rmi.RemoteException (Enforced)
All methods declared in a remote home interface type must include java.rmi.RemoteException in the throws clause. (See EJB20SPEC - 7.10.6, 10.6.10 and 12.2.9.)
WRONG
import javax.ejb.CreateException;
import javax.ejb.EJBHome;
public interface FooHome extends EJBHome {
Foo create(String fooId, String bar) throws CreateException;
}
RIGHT
import javax.ejb.CreateException;
import javax.ejb.EJBHome;
public interface FooHome extends EJBHome {
Foo create(String fooId, String bar)
throws CreateException, RemoteException;
}
JEC_039: Do Not Make A Method Of An EJB Local Interface Throw java.rmi.RemoteException (Enforced)
No method declared in a local bean interface type should include java.rmi.RemoteException in the throws clause. (See EJB20SPEC - 7.10.7 and 12.2.1)
WRONG
import javax.ejb.CreateException;
import javax.ejb.EJBLocalHome;
public interface FooHome extends EJBLocalHome {
Foo create(String fooId, String bar) throws CreateException;
}
RIGHT
import javax.ejb.CreateException;
import javax.ejb.EJBLocalHome;
public interface FooHome extends EJBLocalHome {
Foo create(String fooId, String bar)
throws CreateException, RemoteException;
}
JEC_040: Do Not Make A Method Of An EJB Local Home Interface Throw java.rmi.RemoteException (Enforced)
No method declared in a local home interface type should include java.rmi.RemoteException in the throws clause. (See EJB20SPEC - 7.10.8 and 12.2.11.)
WRONG
import javax.ejb.EJBLocalObject;
import javax.ejb.RemoteException;
public interface Foo extends EJBLocalObject {
public String getFooId() throws RemoteException;
}
RIGHT
import javax.ejb.EJBLocalObject;
public interface Foo extends EJBLocalObject {
public String getFooId();
}
JEC_042: Declare An ejbSelect Method Of A CMP EJB abstract (High)
Enterprise entity beans with container-managed persistence must declare each ejbSelectXXX method as abstract as they will be implemented by the EJB container. (See EJB20SPEC - 10.6.2.)
WRONG
import javax.ejb.FinderException;
public Set ejbSelectAfterDateFoo(Date givenDate) throws FinderException;
RIGHT
import javax.ejb.FinderException;
public abstract Set ejbSelectAfterDateFoo(Date givenDate) throws FinderException;
 | Note See also rule JEC_033: Declare An ejbSelect Method Of An Entity Bean public (High). |
JEC_043: Make An ejbCreate Method Of An Entity Bean Throw javax.ejb.CreateException (High)
The ejbCreate methods defined in an enterprise entity beans must include javax.ejb.CreateException in the throws clause. (See EJB20SPEC - 10.6.4.)
WRONG
public String ejbCreate(String fooId, String bar) {
}
RIGHT
public String ejbCreate(String fooId, String bar) throws CreateException {
}
JEC_044: Declare An ejbHome Method Of An Entity Bean public (High)
Entity beans must declare home methods (ejbHomeXXX) with public access. (See EJB20SPEC - 10.6.6 and 12.2.6.)
JEC_045: Do Not Declare An ejbHome Method Of An Entity Bean final Or static (High)
Home methods (ejbHomeXXX) declared in an enterprise entity bean must not be declared static. (See EJB20SPEC - 10.6.6 and 12.2.6.)
JEC_046: Do Not Make An ejbHome Method Of An Entity Bean Throw java.rmi.RemoteException (High)
Home methods (ejbHomeXXX) declared in an enterprise entity bean must not include java.rmi.RemoteException in the throws clause. (See EJB20SPEC - 10.6.6 and 12.2.6.)
JEC_047: Make A Message Bean Implementation Implement javax.jms.MessageListener (High)
Enterprise beans that implement javax.ejb.MessageDrivenBean must also, directly or indirectly, implement the javax.jms.MessageListener interface. (See EJB20SPEC - 15.7.2.)
WRONG
import javax.ejb.MessageDrivenBean;
public class FooMDB implements MessageDrivenBean {
}
RIGHT
import javax.ejb.MessageDrivenBean;
import javax.jms.MessageListener;
public class FooMDB
implements MessageDrivenBean
MessageListener {
}
JEC_052: Provide An ejbCreate Method For A Message Bean (High)
Enterprise message beans must define one ejbCreate method. (See EJB20SPEC - 15.7.2.)
WRONG
public class FooBean extends AbstractMessageBean
implements MessageListener {
public FooBean() {
}
public void onMessage(Message msg) {
}
}
RIGHT
public class FooBean extends AbstractMessageBean
implements MessageListener {
public FooBean() {
}
public void ejbCreate() {
}
public void onMessage(Message msg) {
}
}
JEC_053: Return void For An ejbCreate Method Of A Message Bean (High)
The ejbCreate method in an enterprise message bean must be declared to return void. (See EJB20SPEC - 15.7.3.)
WRONG
public class FooBean extends AbstractMessageBean
implements MessageListener {
public FooBean() {
}
public int ejbCreate() {
}
public void onMessage(Message msg) {
}
}
RIGHT
public class FooBean extends AbstractMessageBean
implements MessageListener {
public FooBean() {
}
public void ejbCreate() {
}
public void onMessage(Message msg) {
}
}
JEC_054: Do Not Declare Arguments For An ejbCreate Method Of A Message Bean (High)
The ejbCreate method in an enterprise message bean must not declare any arguments. (See EJB20SPEC - 15.7.3.)
WRONG
public class FooBean extends AbstractMessageBean
implements MessageListener {
public FooBean() {
}
public void ejbCreate(String value) {
}
public void onMessage(Message msg) {
}
}
RIGHT
public class FooBean extends AbstractMessageBean
implements MessageListener {
public FooBean() {
}
public void ejbCreate() {
}
public void onMessage(Message msg) {
}
}
JEC_055: Provide A Valid RMI Method Signature For An EJB Remote Home Interface (High)
The methods defined in the Home interface must follow the rules for RMI/IIOP. This means that their argument and return values must be of valid types for RMI/IIOP, and that their throws clauses must include the java.rmi.RemoteException. (See EJB20SPEC - 7.10.6.)
JEC_056: Do No Print Unnecessary Static Content From A Servlet (High)
Following the Model-View-Controller design pattern, the Servlet should be used as a (Front) Controller and dispatches the Model (in J2EE this could be one or more Transfer Objects) to the Java ServerPages. The JSP handles all View functionality and might get some assistance from some Helper classes. This web design is also known as the model 2 (See MODEL2 ).
WRONG
public class FooServlet extends HttpServlet {
public void doGet(HttpServletRequest req,
HttpServletResponse res) throws IOException {
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<body>");
out.println("Hello World");
out.println("</body>");
out.println("</html>");
out.close();
}
}
RIGHT
public class FooServlet extends HttpServlet {
public void service(HttpServletRequest req,
HttpServletResponse res) {
processRequest(req, res);
}
protected void processRequest(HttpServletRequest req,
HttpServletResponse res) throws IOException {
Command cmd = null;
String page = null;
String param = null;
param = req.getParameter(CMD);
if (null != param) {
cmd = CommandFactory.createCommand(param);
page = command.execute(req, res);
} else {
page = ERROR_PAGE;
}
dispatch(req, res, page);
}
protected void dispatch(HttpServletRequest req,
HttpServletResponse res, String page) throws IOException {
ServletContext ctx = getServletContext();
RequestDispatcher dispatcher = ctx.getRequestDispatcher(page);
dispatcher.forward(req, res);
}
}
helloWorld.jsp
<html>
<body>
HelloWorld
</body>
</html>
JEC_057: Use Custom JSP Tags Instead Of Scriptlets (High)
Avoid the usage of scriptlets in JavaServer Pages because:
- Scriptlet code is not reusable.
- Scriptlets encourage copy and paste coding.
- Scriptlets combines Controller with View code. See also rule JEC_056: Do No Print Unnecessary Static Content From A Servlet (High).
- Scriptlets make JSP pages difficult to read and maintain
- Debugging scriptlet code is not straight forward.
- Scriptlet code is also not easy to test.
WRONG
<html>
<body>
<table>
<% for (int i=0; i < employees.length; i++) { %>
out.println("<TD>"+ employees.getName(i) + "</TD>");
out.println("<TD>"+ employees.getProfile(i) + "</TD>");
<% } %>
</table>
</body>
</html>
RIGHT
<%@ taglib uri="/WEB-INF/jcs.tld" prefix="jcs" %>
<html>
<body>
<table>
<jcs:employeelist id="employee_list">
<tr>
<td><jcs:employee attribute="name"/></td>
<td><jcs:employee attribute="profile"/></td>
</tr>
</jcs:employeelist>
</table>
</body>
</html>
JEC_058: Do Not Forward A Request From A JSP Page (High)
A JSP page should not implement the Command and Controller strategy, this is the responsibility of the Front Controller Servlet.
WRONG
<%
String param = request.getParameter("cmd");
if (param.equals("helloWorld")) { %>
<jsp:forward page="/helloWorld.jsp"/>
<%
} else if (param.equals("Goodbye")) { %>
<jsp:forward page="/goodbye.jsp"/>
<% } %>
RIGHT - See example in rule JEC_056: Do No Print Unnecessary Static Content From A Servlet (High)