Skip to content

Wizard-hash2/Java_Bean

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Adder Example (JavaBean, JSP, Swing GUI, Applet)

This small example provides multiple ways to add two integers:

  • JavaBean: src/com/example/beans/AdderBean.java
  • JSP pages: web/index.jsp (form) and web/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):

  1. Build classes. From project root run (requires JDK installed and javac on 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/....

  2. Start Tomcat and deploy the web folder as a webapp. If Tomcat is installed in C:\apache-tomcat-<ver> you can copy the web folder to C:\apache-tomcat-<ver>\webapps\adder (rename web to adder) and start Tomcat.

  3. Open in browser:

    http://localhost:8080/adder/index.jsp

Notes and details:

  • The JSP uses <jsp:useBean> and expects the bean class com.example.beans.AdderBean to 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 AdderApplet is 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:

  1. Compile the GUI (if not already compiled)

    javac -d out\classes src\com\example\gui\AdderGUI.java

  2. Run it:

    java -cp out\classes com.example.gui.AdderGUI

alt text

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:

  1. 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.
  2. 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

  1. 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.

About

A JAVA BEAN / EJB / JSP / GUI / applets that perform addition task

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published