Blame
| ee8ebb | Melisha Dsouza | 2026-01-15 09:01:48 | 1 | # **Java EE** |
| 2 | ||||
| 478115 | Melisha Dsouza | 2026-01-16 09:55:54 | 3 | ### **1. Revision History** |
| 4 | ||||
| 5 | | Date | Version | Author | Description of Changes | |
|||
| 6 | | ---------- | ------- | ------ | --------------------------------------------------------------------------------------------- | |
|||
| 7 | | 2026-01-12 | 1.0 | YS | Initial official release combining Enovate legacy standards and modern Jakarta EE guidelines. | |
|||
| 8 | ||||
| 9 | ||||
| 10 | ### **2. General Java Naming Conventions** |
|||
| ee8ebb | Melisha Dsouza | 2026-01-15 09:01:48 | 11 | |
| 12 | These conventions ensure modules and components are easy to identify and organize, enhancing maintenance. All conventions are compliant with standard Java Programming Language code conventions. |
|||
| 13 | * **Classes & Interfaces**: Use PascalCase (mixed case with the first letter of each internal word capitalized). |
|||
| 14 | * **Example**: `UserRegistrationService, ContactInfoBean.` |
|||
| 15 | * **Methods**: Use camelCase (mixed case with a lower case first letter) and start with a verb. |
|||
| 16 | * **Example**: `calculateTotal(), performLogin().` |
|||
| 17 | * **Variables & Attributes**: Use camelCase; avoid single-letter names except for loop counters. |
|||
| 18 | * **Example**: `isAccountActive.` |
|||
| 19 | * **Constants & Enumerated Values**: Use UPPER_SNAKE_CASE (upper case with words separated by underscores). |
|||
| 20 | * **Example**: `MAX_RETRY_ATTEMPTS, VISA.` |
|||
| 21 | * **Packages**: Use all-lowercase ASCII letters. |
|||
| 22 | * **Example**: `com.enovate.paymentsystem.service.` |
|||
| 23 | ||||
| 478115 | Melisha Dsouza | 2026-01-16 09:55:54 | 24 | ### **3. J2EE Application, Module, and Component Names** |
| ee8ebb | Melisha Dsouza | 2026-01-15 09:01:48 | 25 | This includes archive names and display names in deployment descriptors. |
| 26 | * **J2EE Application (EAR):** Packaged as an Enterprise Archive with a .ear extension. |
|||
| 27 | * **Archive Name:** All-lowercase ASCII letters. |
|||
| 28 | * **Example**: `paymentsystem.ear.` |
|||
| 29 | * **Display Name**: Expanded application name in mixed case followed by "EAR". |
|||
| 30 | * **Example**: `<display-name>PaymentSystemEAR</display-name>.` |
|||
| 31 | * **EJB Modules:** Packaged as JAR files with a .jar extension. |
|||
| 32 | * **Archive Name**: `<module-name>-ejb.jar.` |
|||
| 33 | * **Client Archive**: `<module-name>-ejb-client.jar.` |
|||
| 34 | * **Display Name**: `<expanded-module-name> JAR.` |
|||
| 35 | * **Web Modules:** Packaged as Web Archives with a .war extension. |
|||
| 36 | * **Archive Name:** `<module-name>.war.` |
|||
| 37 | * **Display Name**: `<expanded-module-name> WAR.` |
|||
| 38 | ||||
| 478115 | Melisha Dsouza | 2026-01-16 09:55:54 | 39 | ### **4. EJB Component Naming Standards** |
| ee8ebb | Melisha Dsouza | 2026-01-15 09:01:48 | 40 | Conventions for the different parts of an enterprise bean. |
| 41 | * **Entity & Session Beans:** |
|||
| 42 | * **Local Interface:** `<component-name> or <component-name>Local.` |
|||
| 43 | * **Local Home Interface:** `<component-name>Home or <component-name>LocalHome.` |
|||
| 44 | * **Remote Interface:** `<component-name>Remote.` |
|||
| 45 | * Implementation Class: `<component-name>Bean.` |
|||
| 46 | * Primary Key Class: `<component-name>PK.` |
|||
| 47 | * **Message Driven Beans (MDB)**: |
|||
| 48 | * **Implementation Class:** `<component-name>Bean.` |
|||
| 49 | * **Display Name:** `<expanded-component-name> MDB.` |
|||
| 50 | ||||
| 478115 | Melisha Dsouza | 2026-01-16 09:55:54 | 51 | ### **5. Web Component Naming Standards** |
| ee8ebb | Melisha Dsouza | 2026-01-15 09:01:48 | 52 | Includes Servlets, JSPs, filters, and listeners. |
| 53 | ||||
| 54 | * **Servlets: ** |
|||
| 55 | * **Implementation Class: **`<component-name>Servlet.` |
|||
| 56 | * **Display Name:** `<expanded-component-name> Servlet.` |
|||
| 57 | * **Filters:** |
|||
| 58 | * **Implementation Class:** `<component-name>Filter.` |
|||
| 59 | * **Listeners:** Implementation class should be `<component-name>Listener.` |
|||
| 60 | * **JavaServer Pages (JSP):** |
|||
| 61 | * **File Name:** Must begin with a lower-case letter; avoid verb-only names. |
|||
| 62 | * **Example:** `performLogin.jsp.` |
|||
| 63 | * **JSP Segments: **`/WEB-INF/jspf/file.jspf.` |
|||
| 64 | * **Tag Files:** `/WEB-INF/tags/file.tag.` |
|||
| 65 | ||||
| 478115 | Melisha Dsouza | 2026-01-16 09:55:54 | 66 | ### **6. Web Service Naming (JAX-RPC & Endpoint)** |
| ee8ebb | Melisha Dsouza | 2026-01-15 09:01:48 | 67 | Naming for clients, endpoints, and configuration files. |
| 68 | * **Service Name:** PascalCase base name of the EJB or JAX-RPC endpoint. |
|||
| 69 | * Example: `WeatherService.` |
|||
| 70 | * **Endpoints:** |
|||
| 71 | * **Service Endpoint Interface:** `<service-name>SEI.` |
|||
| 72 | * **EJB Implementation:** `<service-name>Bean.` |
|||
| 73 | * **JAX-RPC Implementation:** `<service-name>Impl.` |
|||
| 74 | * Configuration: |
|||
| 75 | * **WSDL File:** `<service-name>.wsdl.` |
|||
| 76 | * **Mapping File:** `<all-lower-case-service-name>-mapping.xml.` |
|||
| 77 | ||||
| 478115 | Melisha Dsouza | 2026-01-16 09:55:54 | 78 | ### **7. Reference Names (JNDI & Resources)** |
| ee8ebb | Melisha Dsouza | 2026-01-15 09:01:48 | 79 | All reference names are relative to java:comp/env. |
| 80 | * **EJB References**: Logical name for JNDI lookup of home interfaces. |
|||
| 81 | * Format: `ejb/<component-name>.` |
|||
| 82 | * **Web Service References:** `service/<service-name>.` |
|||
| 83 | * **Resource References:** |
|||
| 84 | * **JDBC**: `jdbc/<resource-name>.` |
|||
| 85 | * **JMS**: `jms/<resource-name> [Queue|Topic].` |
|||
| 86 | * **Mail**: `mail/<resource-qualifier>.` |
|||
| 87 | * **Environment Entries**: `param/<parameter-name>.` |
|||
| 88 | ||||
| 478115 | Melisha Dsouza | 2026-01-16 09:55:54 | 89 | ### **8. Database & XML Document Naming** |
| ee8ebb | Melisha Dsouza | 2026-01-15 09:01:48 | 90 | * **Database Naming:** Name CMP databases after the application (e.g., `PaymentSystemDB`). |
| 91 | * **Database schema:** Use lower_case_with_underscores (e.g. `payment_system_db`). |
|||
| 92 | * **Database tables:** Use lower_case_with_underscores (e.g. `payment_records`). |
|||
| 93 | * **Database columns:** Use lower_case_with_underscores (e.g. `payment_id`). |
|||
| 94 | * **XML Elements:** Follow class naming (`PascalCase`). |
|||
| 95 | * **XML Attributes:** Follow variable naming (`camelCase`). |
|||
| 96 | * **Alternative XML Notation:** Element names in lower case separated by hyphens (e.g., `<credit-card>`). |
|||
| 97 | ||||
| 478115 | Melisha Dsouza | 2026-01-16 09:55:54 | 98 | ### **9. Deployment Descriptor & Manifest Standards** |
| ee8ebb | Melisha Dsouza | 2026-01-15 09:01:48 | 99 | * **Standard Descriptors:** |
| 100 | * **Application**: `application.xml.` |
|||
| 101 | * **Web**: `web.xml.` |
|||
| 102 | * **EJB**: `ejb-jar.xml.` |
|||
| 103 | * **Manifest Files:** Prototype names used in source directories to distinguish files before they are renamed to `MANIFEST.MF` in the archive. |
|||
| 104 | * EJB Manifest: `ejb-jar-manifest.mf.` |
|||
| 105 | ||||
| 478115 | Melisha Dsouza | 2026-01-16 09:55:54 | 106 | ### **10. Modern Jakarta EE Compatibility** |
| ee8ebb | Melisha Dsouza | 2026-01-15 09:01:48 | 107 | While maintaining legacy code, new development must align with modern Jakarta EE practices. |
| 108 | * **Dependency Injection (CDI)**: Use @Inject instead of manual JNDI lookups (relative to java:comp/env) where supported. |
|||
| 109 | * **REST Services**: Prefer Jakarta REST (JAX-RS) over new Servlet implementations for service interfaces. |
|||
| 110 | * **Persistence**: Utilize JPA @Entity POJOs to replace legacy CMP Entity Beans. |
|||
| 111 | * **Validation**: Use Bean Validation annotations (@NotNull, @Size) to replace manual validation in Web components. |
|||
| 112 | ||||
| 478115 | Melisha Dsouza | 2026-01-16 09:55:54 | 113 | ### **11. Architecture & Layering Strategy** |
| ee8ebb | Melisha Dsouza | 2026-01-15 09:01:48 | 114 | We adhere to a strict Separation of Concerns (SoC). Business logic must never leak into the Database or Web layers. |
| 115 | * **Controller (JAX-RS):** Handles HTTP requests/responses. Use DTOs (Data Transfer Objects) here; never expose JPA Entities directly to the client. |
|||
| 116 | * **Service Layer (EJB/CDI):** Contains the "What" of the application. Transaction boundaries (@Transactional) start here. |
|||
| 117 | * **Repository Layer (JPA):** Contains the "How" of data retrieval. Purely for CRUD and queries. |
|||
| 118 | To prevent `"Spaghetti Code,"` all Java EE applications must follow a Layered Architecture. |
|||
| 119 | * **Presentation Layer (Web/API):** REST Controllers or Servlets. No business logic. |
|||
| 120 | * **Business Layer (Service):** Core logic and transaction boundaries (@Transactional). |
|||
| 121 | * **Data Access Layer (Repository/DAO):** Database interactions using JPA/Hibernate. |
|||
| 122 | ||||
| 478115 | Melisha Dsouza | 2026-01-16 09:55:54 | 123 | ### **12. General Coding Standards** |
| 124 | #### 12.1. **Dependency Injection (DI)** |
|||
| ee8ebb | Melisha Dsouza | 2026-01-15 09:01:48 | 125 | Use constructor injection instead of @Autowired or field injection to ensure the class remains testable and final. |
| 478115 | Melisha Dsouza | 2026-01-16 09:55:54 | 126 | ```Java |
| ee8ebb | Melisha Dsouza | 2026-01-15 09:01:48 | 127 | |
| 128 | // Recommended |
|||
| 129 | private final UserService userService; |
|||
| 130 | ||||
| 131 | public UserController(UserService userService) { |
|||
| 132 | this.userService = userService; |
|||
| 133 | } |
|||
| 134 | ``` |
|||
| 135 | ||||
| 478115 | Melisha Dsouza | 2026-01-16 09:55:54 | 136 | #### 12.2. **Avoid "Magic Numbers"** |
| ee8ebb | Melisha Dsouza | 2026-01-15 09:01:48 | 137 | Never hardcode numeric literals in logic. Use constants or Enums. |
| 478115 | Melisha Dsouza | 2026-01-16 09:55:54 | 138 | ```Java |
| ee8ebb | Melisha Dsouza | 2026-01-15 09:01:48 | 139 | |
| 140 | // Bad |
|||
| 141 | if (status == 1) { ... } |
|||
| 142 | ||||
| 143 | // Good |
|||
| 144 | public static final int STATUS_ACTIVE = 1; |
|||
| 145 | if (status == STATUS_ACTIVE) { ... } |
|||
| 146 | ``` |
|||
| 147 | ||||
| 478115 | Melisha Dsouza | 2026-01-16 09:55:54 | 148 | #### 12.3. **Use StringBuilder for Loops** |
| ee8ebb | Melisha Dsouza | 2026-01-15 09:01:48 | 149 | For string manipulation inside loops, use StringBuilder to avoid unnecessary object creation in the heap. |
| 150 | ||||
| 478115 | Melisha Dsouza | 2026-01-16 09:55:54 | 151 | ### **13. EJB and CDI (Dependency Injection)** |
| ee8ebb | Melisha Dsouza | 2026-01-15 09:01:48 | 152 | Modern Java EE favors CDI over older EJB styles where possible. |
| 153 | * **Prefer Constructor Injection:** It makes the code immutable and easier to unit test without a container. |
|||
| 478115 | Melisha Dsouza | 2026-01-16 09:55:54 | 154 | ```Java |
| ee8ebb | Melisha Dsouza | 2026-01-15 09:01:48 | 155 | |
| 156 | @ApplicationScoped |
|||
| 157 | public class OrderService { |
|||
| 158 | private final InventoryClient inventoryClient; |
|||
| 159 | ||||
| 160 | @Inject // Required for CDI constructor injection |
|||
| 161 | public OrderService(InventoryClient inventoryClient) { |
|||
| 162 | this.inventoryClient = inventoryClient; |
|||
| 163 | } |
|||
| 164 | } |
|||
| 165 | ``` |
|||
| 166 | ||||
| 167 | * **Scope Awareness:** Use @RequestScoped for web-related beans and @ApplicationScoped for singleton-like services. Avoid @SessionScoped unless strictly necessary for stateful web apps to save memory. |
|||
| 168 | ||||
| 478115 | Melisha Dsouza | 2026-01-16 09:55:54 | 169 | ### **14. JPA (Persistence) Best Practices** |
| ee8ebb | Melisha Dsouza | 2026-01-15 09:01:48 | 170 | Database performance is usually the bottleneck of Java EE apps. |
| 171 | * **Fetch Type:** Always default to FetchType.LAZY for @OneToMany and @ManyToMany relationships to avoid the "N+1" select problem. |
|||
| 172 | * **Use Named Queries:** This allows the JPA provider to pre-compile the query. |
|||
| 478115 | Melisha Dsouza | 2026-01-16 09:55:54 | 173 | ```Java |
| ee8ebb | Melisha Dsouza | 2026-01-15 09:01:48 | 174 | |
| 175 | @Entity |
|||
| 176 | @NamedQuery(name="User.findByEmail", query="SELECT u FROM User u WHERE u.email = :email") |
|||
| 177 | public class User { ... } |
|||
| 178 | ``` |
|||
| 179 | * **Avoid System.out.println for SQL**: Use the persistence property javax.persistence.schema-generation.database.action and standard logging. |
|||
| 180 | ||||
| 478115 | Melisha Dsouza | 2026-01-16 09:55:54 | 181 | ### **15. RESTful API Design (JAX-RS)** |
| ee8ebb | Melisha Dsouza | 2026-01-15 09:01:48 | 182 | * **Use Standard HTTP Verbs**: * `GET (Read)`, `POST (Create)`, `PUT (Update)`, `DELETE (Remove)`. |
| 183 | * **Proper Status Codes**: * 201 Created for successful POSTs. |
|||
| 184 | * `400 Bad Request for validation failures.` |
|||
| 185 | * `404 Not Found for missing resources.` |
|||
| 186 | * **Version your APIs:** Always include a version in the URL path. |
|||
| 187 | * **Example**: `@Path("/v1/orders"` |
|||
| 188 | ||||
| 478115 | Melisha Dsouza | 2026-01-16 09:55:54 | 189 | ### **16. Exception Handling** |
| ee8ebb | Melisha Dsouza | 2026-01-15 09:01:48 | 190 | * **Don't Swallow Exceptions:** Never leave a catch block empty. At least log the error. |
| 191 | * **Specific Exceptions:** Catch FileNotFoundException rather than a generic Exception. |
|||
| 192 | * **Custom Exceptions:** Create domain-specific exceptions (e.g., `UserNotFoundException`) to provide better context to the API consumer. |
|||
| 193 | * **Global Exception Mapper**: Use `ExceptionMapper<T>` to catch internal exceptions and return a clean JSON response to the user. |
|||
| 478115 | Melisha Dsouza | 2026-01-16 09:55:54 | 194 | ```Java |
| ee8ebb | Melisha Dsouza | 2026-01-15 09:01:48 | 195 | |
| 196 | @Provider |
|||
| 197 | public class EntityNotFoundMapper implements ExceptionMapper<EntityNotFoundException> { |
|||
| 198 | @Override |
|||
| 199 | public Response toResponse(EntityNotFoundException ex) { |
|||
| 200 | return Response.status(404).entity(new ErrorDTO(ex.getMessage())).build(); |
|||
| 201 | } |
|||
| 202 | } |
|||
| 203 | ``` |
|||
| 204 | ||||
| 478115 | Melisha Dsouza | 2026-01-16 09:55:54 | 205 | ### **17. Logging** |
| ee8ebb | Melisha Dsouza | 2026-01-15 09:01:48 | 206 | * **SLF4J + Logback:** Use placeholders {} instead of string concatenation to improve performance. |
| 207 | * **Bad**: `logger.info("User " + name + " logged in");` |
|||
| 208 | * **Good**: `logger.info("User {} logged in", name);` |
|||
| 209 | ||||
| 478115 | Melisha Dsouza | 2026-01-16 09:55:54 | 210 | ### **18. Security & Validation Best Practices** |
| ee8ebb | Melisha Dsouza | 2026-01-15 09:01:48 | 211 | * **Input Validation**: Never trust client data. Use Bean Validation `(@NotNull, @Size, @Email).` |
| 212 | * **Principle of Least Privilege: **Class members should be private by default. |
|||
| 213 | * **Sensitive Data**: Never log passwords, credit card numbers, or PII (Personally Identifiable Information). |
|||
| 214 | * **Bean Validation:** Use standard constraints to keep logic clean. |
|||
| 478115 | Melisha Dsouza | 2026-01-16 09:55:54 | 215 | ```Java |
| ee8ebb | Melisha Dsouza | 2026-01-15 09:01:48 | 216 | |
| 217 | public class UserDTO { |
|||
| 218 | @NotBlank(message = "Username is required") |
|||
| 219 | @Size(min = 3, max = 20) |
|||
| 220 | private String username; |
|||
| 221 | ||||
| 222 | @Email |
|||
| 223 | private String email; |
|||
| 224 | } |
|||
| 225 | ``` |
|||
| 226 | * **Encoding**: Always specify UTF-8 encoding for all web responses to prevent character corruption. |
|||
| 227 | * **No Hardcoded Credentials**: All passwords, API keys, and DB URLs must be stored in environment variables or a microprofile-config.properties file. |
|||
| 478115 | Melisha Dsouza | 2026-01-16 09:55:54 | 228 | |
| 229 | **Document** - [JAVA EE](https://docs.google.com/document/d/1IVK8d4CH07CCje1yJsev01vM3Fq-2hpE0VWCPbEXBSk/edit?usp=sharing) |