This small example provides multiple ways to add two integers:
- JavaBean:
src/com/example/beans/AdderBean.java - JSP pages:
web/index.jsp(form) andweb/add.jsp(uses the JavaBean) - Swing GUI:
src/com/example/gui/AdderGUI.java(runnable) - Applet:
src/com/example/applet/AdderApplet.java(legacy)
Structure (created files):
- src/com/example/beans/AdderBean.java
- src/com/example/gui/AdderGUI.java
- src/com/example/applet/AdderApplet.java
- web/index.jsp
- web/add.jsp
Quick deploy (Tomcat) - Windows (cmd.exe):
-
Build classes. From project root run (requires JDK installed and
javacon PATH):javac -d web/WEB-INF/classes src\com\example\beans\AdderBean.java src\com\example\gui\AdderGUI.java src\com\example\applet\AdderApplet.java
This will place compiled classes under
web/WEB-INF/classes/com/example/.... -
Start Tomcat and deploy the
webfolder as a webapp. If Tomcat is installed inC:\apache-tomcat-<ver>you can copy thewebfolder toC:\apache-tomcat-<ver>\webapps\adder(renamewebtoadder) and start Tomcat. -
Open in browser:
Notes and details:
-
The JSP uses
<jsp:useBean>and expects the bean classcom.example.beans.AdderBeanto be available on the webapp classpath (WEB-INF/classes). -
If you use an IDE (NetBeans, Eclipse), add the source folder to a web project and let the IDE build and deploy for you.
-
Applets are deprecated and not supported by modern browsers; the
AdderAppletis for legacy experimentation or running in an applet viewer:appletviewer web\index.jsp
(You may need to create a small HTML page that references the applet class.)
Run Swing GUI locally:
-
Compile the GUI (if not already compiled)
javac -d out\classes src\com\example\gui\AdderGUI.java
-
Run it:
java -cp out\classes com.example.gui.AdderGUI
running applet :
cd "C:\Users\Aron\OneDrive\Desktop\MyCode\JAVA.netBeans\group_assignment"
javac -d out\classes src\com\example\applet\AdderApplet.java src\com\example\applet\AdderAppletLauncher.java
java -cp out\classes com.example.applet.AdderAppletLauncher
running JSP: REM point JAVA_HOME at the real JDK on your machine set "JAVA_HOME=C:\Program Files\Java\jdk-24"
REM confirm the setting and that java/javac exist echo JAVA_HOME=%JAVA_HOME% "%JAVA_HOME%\bin\java.exe" -version "%JAVA_HOME%\bin\javac.exe" -version
REM check catalina using that JAVA_HOME "%CATALINA_HOME%\bin\catalina.bat" version
REM start Tomcat "%CATALINA_HOME%\bin\startup.bat"
RUNNING THE GUI:
javac -d out\classes src\com\example\gui\AdderGUI.java
java -cp out\classes com.example.gui.AdderGUI
RUNNING ejb runs on the website
Explanation:
- Why is it a component Encapsulated functionality — AdderBean and AdderEjb each package a single responsibility (adding two numbers). That makes them reusable building blocks (components). Clear interface — they expose a small, well-defined API: AdderBean → properties setA(int)/setB(int) and getSum() AdderEjb → method add(int a,int b) Container-manageable — the JavaBean is usable by JSP/servlet containers; the EJB is managed by a Jakarta EE server (lifecycle, pooling, concurrency, security). Testable & replaceable — implementation is isolated so you can swap in another implementation or mock it in tests. Low coupling — callers only need the public methods; they don’t depend on internal state or UI logic.
- How it’s accessed / interfaced with the web or package
A. JavaBean (AdderBean) — direct web integration
JSP In add.jsp: <jsp:useBean id="adder" class="com.example.beans.AdderBean" scope="request" /> <jsp:setProperty name="adder" property="a" param="a" /> etc. JSP container instantiates AdderBean from WEB-INF/classes or WEB-INF/lib.
Servlet / Java code Direct instantiation: AdderBean b = new AdderBean(); b.setA(2); b.setB(3); int s = b.getSum(); Location: compiled classes are under WEB-INF/classes/com/example/beans and packaged into a jar in WEB-INF/lib.
B. EJB (AdderEjb) — container-managed access
Injection in server-side component: @EJB private AdderEjb adder; inside a servlet/EJB JNDI lookup InitialContext ctx = new InitialContext(); AdderEjb a = (AdderEjb) ctx.lookup("java:module/AdderEjb"); D. Packaging and classpath rules
For JSP/servlets (Tomcat): .class files is under WEB-INF/classes/com/example
-
Where else can it be used (concrete examples) A. Two websites (different webapps)
Internal Admin Dashboard AdderBean is directly in server-side pages for internal configuration calculations or quick admin tools. Deployment: I include AdderBean.class in WEB-INF/classes and math-utils.jar in WEB-INF/lib
B. Two different packages / modules inside back-end systems
Shared utilities package (library JAR) Create com.example.lib:math-utils:1.0 that contains AdderBean or pure util class Adder. Include this dependency in any Java project (webapp A, webapp B, background worker). Benefit: single source of truth for business logic, easier updates.
