Vous êtes sur la page 1sur 12

Sessi on Bean i n NetBeans

Step 1: Select the project Tab ->right Click ->Select : New Project >
J ava EE>Enterprise Application >Next>

Project Name: StudentSession ->Next ->Check for GlassFish V2.1 server is selected
or not - >Finish


Step 2: Click on the WAR module. double click on index.jsp file. write the following
code in the body tag.

<h1>Calculator</h1>
<hr>
<form action="callcalculator">
<p>Enter first value:
<input type="text" name="num1" size="25"></p>
<br>
<p>Enter second value:
<input type="text" name="num2" size="25"></p>
<br>
<b>Select your choice:</b><br>
<input type="radio" name="operation" value ="add">Addition<br>
<input type="radio" name="operation" value ="sub">Subtraction<br>
<input type="radio" name="operation" value ="multi">Multiplication<br>
<input type="radio" name="operation" value ="div">Division<br>
<p>
<input type="submit" value="Submit">
<input type="reset" value="Reset"></p>
</form>


Step 3: In the War Module Right Click On ->New>Servlet>

Class Name:callcalculator : Package : com ->Next>Finish


Step 4: Right Click On EJ B module and New ->Session Bean ->
Class Name : callogic
Package : com
Select Stateless option
Check the box of Both Remote and Local
Click On Finish


Step 5:Right click on the class <callogic>and Select the option Insert Code -> Add
Bussiness method :
MethodName: add
Type double
// To add parameters
Click On the add button
Name :num1
Type double
Click On the add button
Name :num2
Type double
Use of Interface Both
Press OK Button


Step 6 : Write this statement in add function : return num1+num2;


Step 7: Right click on the class and Select the option Insert Code -> Add Bussiness
Method :
Method name: sub
Type double
// To add parameters
Click On the add button
Name :num1
Type double
Click On the add button
Name :num2
Type double
Use of Interface Both
Press OK Button


Step 8:Write this statement in sub function : return num1-num2;

Step 9:Right click on the class and Select the option Insert Code ->Add Bussiness
Method
Name: multi
Type double
// To add parameters
Click On the add button
Name :num1
Type double
Click On the add button
Name :num2
Type double
Use of Interface Both
Press OK Button
Step 10: Write this statement in multi function : return num1*num2;


Step 11: Right click on the class and Select the option Insert Code ->Add
Bussiness Method
Name: div
Type double
// To add parameters
Click On the add button
Name :num1
Type double
Click On the add button
Name :num2
Type double
Use of Interface Both
Press OK Button


Step 12: Write this statement in div function : return num1/num2;


Step 13: In the War Module ->source packages ->com ->callEntity(Servlet Class)

Double Click on the file
Right Click inside the Class and Insert Code ->Call Enterprise Bean ->A new Window
will be poped up ->Select callogic Bean(Session Bean)
Press OK Button


Step 14: In the processRequest method just write the following code

String num1=request.getParameter("num1");
String num2=request.getParameter("num2");
String opt=request.getParameter("operation");
double result;
if(opt.equals("add"))
{
result=callogicBean.add(Double.valueOf(num1),Double.valueOf(num2));
out.println("Result is "+result);
}
if(opt.equals("sub"))
{
result=callogicBean.sub(Double.valueOf(num1),Double.valueOf(num2));
out.println("Result is "+result);
}
if(opt.equals("multi"))
{
result=callogicBean.multi(Double.valueOf(num1),Double.valueOf(num2));
out.println("Result is "+result);
}
if(opt.equals("div"))
{
result=callogicBean.div(Double.valueOf(num1),Double.valueOf(num2));
out.println("Result is "+result);
}


Step 15: Save all the files and Right Click on the Project ->Clean And Bulid ->Deploy
-> Run.

Entity Bean wi th MySql i n NetBeans

Step 1: Create database with a name student command is
create database student;
use student;
create table stud(id varchar(10) primary key,name varchar(10),marks
varchar(5));
note : while creating a table there should be a primary key.

Step 2: Go to Services Tab(if not available go to window>services then services tab will
be available on left side of the netbeans)
Click on Databases >Right Click >New Connection>A newWindow will pop up.
New Database Connection window will be opened
Name : MySQL(Connector/J Driver)
Host: localhost
Port:3306
Database :student
User Name : root
Password: mysql(specify your mysql password)
Check the check box of Remember Password
Check the check box of Show J BDC
Click On OK BUTTON


Step 3: In the databases tab you can see the connection :
jdbc:mysql://localhost:3306/student and if the symbol is broken just right click and say
connect it will be conneected.


Step 4: Select the project Tab and right Click and say New Project >J ava EE>
Enterprise Application >Next>Project Name: StudentEntity>Next>Check for
GlassFish V2.1 server is selected or not >Finish


Step 5: Select the EJ B module and Right Click >New>Entity Classes From
Databases>A new Window will be popped up....


Step 6: Datasource Name :Select the option New Connection a new window will be
poped up.
J NDI Name :teststudent
Database Connection: From the drop down menu select the
jdbc:mysql://localhost:3306/student option
Press the OK Button.
Step 7: In Available Tables, you find all the tables that are available in the database.
Select the stud Table and click on the Add button >Next>type Package : com
Click on the button Create Persistence Unit>a Window will be popped up>just
click on create Button>That window will be closed>Next>Finish


Step 8: Click on The EJ B module >Right Click>New>Session Bean>EJ B Name :
callStudentEntity >Package : com>Click on Stateless>Select the Remote option and
Local Option also Click On Finish


Step 9 : Right click in the class and select the persistence unit>Use Entity Manager

This code will be automatically generated..
@PersistenceContext
private EntityManager em;
public void persist(Object object) {
em.persist(object);
}


Step 10:Click on the WAR module >double click on index.jsp : file write the following
code in the body tag.

<form action="callEntity">
ID :<input type="text" name="id">
NAME :<input type="text" name="name">
MARKS :<input type="text" name="marks">
<input type="radio" name="operation" value="add">ADD
<input type="radio" name="operation" value="delete">DELETE
<input type="radio" name="operation" value="viewall">VIEWALL
<input type="radio" name="operation" value="search">SEARCH
<input type="submit" value="SUBMIT">
</form>


Step 11: Right Click on War Module and Select New>Servlet>Class Name: callEntity >
package com Next>Finish
Right click inside the servlet class and select Insert Code>Call Enterprise Bean
>A New Window will be popped up and then select callStudentEntityBean>
instead of Local Select the Remote Option and >OK





Step 12: Go to ejb module>source packages>select callSudentEntityBeanBean.java
and write the following code inside this class
Inside this class right click>Insert Code >Add Business MethodName: add
Return Type: void
Click on the Add Button : add the parameters Name i=d
Type java.lang.String
Again Click on the Add Button : add the parameters Name =name
Type java.lang.String
Again Click on the Add Button : add the parameters Name =marks
Type java.lang.String
Use of Interface Both and Press OK Button


Step 13: Write the following code inside the add method
Stud s=new Stud();
s.setId(id);
s.setMarks(marks);
s.setName(name);
em.persist(s);


Step 14: inside this class right click>Insert Code >Add Business Methoda >
new window will be popped up >type the following content
Name : viewall
Return Type : List
Use in Interface Both
Press OK Button


Step 15: Write the following code inside viewall() function :

Query q =em.createNativeQuery("select * from stud");
List l =q.getResultList();
return l;


Near List an error will be popped up J ust Right Click on the bulb and You can
find an option Add import java..util.List J ust Select the option
Near Query an error will be popped up J ust Right Click on the bulb and You
can find an option Add import javax.Persistence.Query J ust Select the option
In callStudentEntityRemote and callStudentEntityLocal files you can see error
near List word an error will be popped up J ust Right Click on the bulb and You
can find an option Add import java..util.List J ust Select the option.



Step 16: Inside this class right click>Insert Code >Add Business Method>new window
will be popped up >type the following content :
Name : search
Return Type : List
Click on Add Button : Name =id
Type java.lang.String
select the option Both
Press OK Button
Add the below code in search() method
Query q=em.createNativeQuery("select * from stud where id="+id);
List l=q.getResultList();
return l;


Step 17: inside this class right click>Insert Code >Add Bussiness MethodA >new
window will be popped up : type the following content
Name : delete
Return Type : int
Click on Add Button : Name =id
Type java.lang.String
select the option Both
Press OK Button

Step 18: Write the following Code
Query q =em.createNativeQuery("delete from stud where id =" +id );
int flag =q.executeUpdate();
return flag;


Step 19: Go to War module >Source packages>com>callEntity(double click on this
file) and write the following code in the try block

Try
{
String name =request.getParameter("name");
String id =request.getParameter("id");
String marks =request.getParameter("marks");
String opt =request.getParameter("operation");
if(opt.equals("add"))
{
callStudentEntityBean.add(id,name,marks);
response.sendRedirect("index.jsp");
}



if(opt.equals("search"))
{
List l =callStudentEntityBean.search(id);
Iterator i=l.listIterator();
while(i.hasNext())
{
out.println(i.next());
}
}

if(opt.equals("viewall"))
{
List l =callStudentEntityBean.viewall();
Iterator i=l.listIterator();
while(i.hasNext())
{
out.println(i.next());
out.println("\n");
}
}

if(opt.equals("delete"))
{
int flag =callStudentEntityBean.delete(id);
if(flag ==1)
{
out.println("sucessfully deleted");
}
else
out.println("deletion failed");
}

Step 20: Right Click on Project Tab>Clean and Build>then>Deploy>Run>

Step 21: Cross check with the database.
Using Message Driven Bean with Glassfish Server


Step 1:
Open the services Tab it will be available on left side of the netbeans window
if not available the go to Window>Services (then it will be available on left side
of netbeans window)

Step 2:
You can find Servers (click on +symbol) you can find GlassFish and apache been
listed SELECT Glassfish v2
Right Click on the GlassFish v2 and select the option Start
Again Right Click And select the option View Admin Console

Step 3:
Browser will be opened.
Enter the username : admin and password: adminadmin

Step 4: This new page will be opened




Step 5: Resource>Destination Resources>New
Type J NDI Name:jms/testq
Physical Destination Name: testing
ResorceType:javax.jms.Queue
Click on OK button


Step 6: Select Resources Option which is available on left side of the page.
Select J MS Resources
Select Connection Factories
A new window will be opened on right side after selecting connection factories
option
Click on New Button
Type J NDI Name:jms/testqueue
ResorceType:javax.QueueConnectionFactory
Click on OK button


Step 7: Minimize the browser and open Netbeans window and select on project window
Right Click NewProject>J avaEE>Enterprise Application>Next>Project Name
:TestMDB>Next>Finish
Select EJ B module and right click New>Message Driven Bean>
EJ B Name:processMSG
Package com
Check the Server Destinations: jms/testq (this will be listed select this destination
name)
Press Finish Button

Step 8: Now select the war module
Right Click >New>Servlet>ServletName: callMDB
Package com
Next>Finish

In this servlet file just right Click and select the option Insert Code>
send J MS_Message option
A new window will be opened type Connection factory as: jms/testqueue (the
connection factory name which you had specified initially).
And click OK Button.

In the same class you have processRequest method Write this code
this.sendJ MSMessageToTestq(request);
An error will be poped up just click on that bulb and fix the error (Surround the try
with catch block) this option you need to select.
Save the file press ctrl+s

In createJ MSMessageForjmsTestq() method write this code
TextMessage tm =session.createTextMessage();
HttpServletRequest rq=(HttpServletRequest)messageData;
tm.setText(rq.getParameter("msg"));
return tm;

Step 9: Go do war module >click on +symbol you can find Web Pages >
Select index.jsp file
Type the following code inside the <body>tag
<form action="callMDB">
SEND MSG:<input type="text" name="msg"><br>
<input type="submit" name="SEND"><br>
</form>
Save the file press ctrl+s

Step 10: Go to ejb module>click on +symbol>find source package. find com
folder and double click on processMSGBean file

There will be onMessage() function type the following code
TextMessage t =null;
t=(TextMessage)message;
try
{
System.out.println(t.getText());
}
catch (J MSException ex) {
Logger.getLogger(processMSGBean.class.getName()).log(Level.SEVERE, null,
ex);
}
Errors will be popped up fix the errors and select the option add import
javax.jms.J MSExeption;

Step 11 : Run the program and output will be displayed in output window

Vous aimerez peut-être aussi