Sunday, July 9, 2017

13. An EJB application that demonstrates persistence (with appropriate business logic).

Step 1: Creation of Project Application
1. File -> New Project -> Java EE -> Enterprise Application -> click next -> Give name for
project (ex: employee -> set Java EE version as Java EE 5 -> Finish.
2. At the end of creation you can see three modules generated.
Step 2: Create database
1. Select services-> Databases ->right click JavaDB ->create database ->right click
jdbc:derby://….. and connect
Step 3: Creation of Entity Bean
1. Right click on projects ejb module -> new -> Entity class -> Give class name and
package -> click next -> Select data source with your database -> Finish.
2. Change AUTO to IDENTITY and add the following code
String name;
int salary;
3. Select the variables -> right click over the selection -> insert code -> Getter and Setter…
-> select all -> Generate. You can see getter and setter method generated.
Step 4: Creation of Session bean for Entity Class
1. Right click on projects ejb module -> new -> Other -> Enterprise Java beans -> Session
bean for entity classes -> click Add all -> click next -> select local.
Step 5: Creating Servlet file
1. Right click on project war module -> new -> servlet -> Give name for servlet and
package -> next -> finish.
2. Inside the class definition -> Right click -> Insert code -> call enterprise bean -> select
your entity class( ex: EmployeeFacade) -> click ok.
3. Inside processRequest method add the following code,
employee dan=new employee();
dan.setName(request.getParameter("name"));
dan.setSalary(Integer.parseInt(request.getParameter("salary")));
empFacade.create(dan);
4. Left click on the error of first line code -> Add import (for your entity class).
Step 6: Creating jsp file
1. Change the code in index.jsp with your code.
2. Your code should produce two label and 2 text boxes and make sure that mapping servlet
with jsp file done correctly.
(<form action="EmployeeServlet">
Name: <input type="text" name="name" value="">
<br>
Salary: <input type="text" name="salary" value="">
<br><br>
<input type="submit" value="submit">
</form>)
Step 7: Executing the project
1. Right click on Application module -> Clean and bulid -> Right click on Application
module -> Deploy.
2. Under services window -> right click on glassfish server -> start the server -> right click
on glassfish server -> view admin console -> Applications -> click on entity bean class
name -> Launch -> click on first link -> Enter data and submit->
3. You can see the data on database by -> right click on your database -> refresh -> expand
your table -> view data.

No comments:

Post a Comment