Você está na página 1de 31

Steps for creating stateful bean using Netbeans 6.

Create a MS Access Database. Create a table named “list”

The list must have the following attributes


1. username - text
2. password - text
3. bal - number
create a DSN(Data source Name ) for MS access database

 Click Start > Control Panel > Administrative Tools > Data Sources (ODBC).
The ODBC Data Source Administrator window appears.
Click “Driver do Micorsoft Access (*.mdb) option . click Finish.
 Select Microsoft Access Driver (*.mdb) and click Finish. The ODBC Microsoft
Access Setup window appears.

ODBC Microsoft Access Setup Window

 Enter the Data Source Name. In this example we created a data source called sss.
Client and server program using Netbeans

In this program, we go to create a JSP client page which consists of

1. a text box to enter an amount


2. submit button

3. Then we create a bean titled “Stateful_session_guna_example”. It consists of the


conversational state “balance” and it is initialized to RS.5000. It has a method
named “deposit” which updates the balance amount whenever the client page
submits an amount.

4. The request and reply is controlled by a SERVLET.


Let’s see the steps one by one

Step 1:

1. Goto File Menu - New project

2. In the New Project wizard,

Choose Java EE - Enterprise Application


Step 2:

Give the Project Name as “Stateful_new_example.

If you wish , u can choose a dedicated Project location using browse option .

Otherwise click “ Next”


Step 3:
Next the New Enterprise application wizard, is enabled. Now, click “Finish”.
Step 4:

A new project is created titled “ stateful_new_example.


Step 5:

In the Project windows ,


Explore Stateful_new_example-war - web page - index.jsp

Step 6:

Type the following code inside the <body> tag and save the file.

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form action="">
<input type="text" name="amount">
<input type="submit" value="submit the amount">

</form>

</body>
</html>
Step 7:

Goto Project window

Choose stateful_new_example_war - web page - New--- Servlet


Step 8;
New servlet window will open . Give the class name as
“Stateful_example_guna_servlet”
Click Next
Step 9:
In the next window, u can change the URL pattern of the servlet (if required).

Otherwise, click finish.


Step 10:

Servlet opens.

Copy the url of the servlet and paste it in the index.jsp file.

Paste the url inside the index.jsp


Step 11:

Go back once again to stateful_example_guna_servlet.java

Get the text box (amount) value of web page through the following code

int a=Integer.parseInt(request.getParameter("amount"));

Step 12(creating a Enterprise bean)

Goto Project window  click stateful_new_example-ejb-- New--Session Bean


Step 13:

Choose a bean name.


choose the option stateful
click the check box “Remote in Project
click Finish
Step 14:

Right click in the mouse  click Insert code


Step 16: click Add Bussiness Method

Step 17:
Add the business method “deposit”
Give the return types as “int”

Add the parameter ‘amount ‘ and its return data type as “int”
Click “ok”
Step 18:

Add the following code inside the class file

Here the conversational state is bal and it is initialized to Rs 5000


Parameter passed to the method is amount. The value of the amount is get through
servlets.

@Stateful
public class Stateful_session_guna_example implements
Stateful_session_guna_exampleRemote {

int bal=5000;
@Override
public int deposit(int amount) {
bal=bal+amount;
return bal;
}

// Add business logic below. (Right-click in editor and choose


// "Insert Code > Add Business Method")

************4444444

Step 19:
Once again goto the same set of options to create a new session bean.
Step 20

Choose a bean name within the same package


choose the option stateful
click the check box “Remote in Project
click Finish
Step 21:

Right click in the mouse  click Insert code


Step 22:

click Add Bussiness Method

Step 23:
Add the business method “update_query”
Give the return types as “String”

Add the parameter ‘amount ‘


Click “ok”

Add the following code:


package ejbb;

import javax.ejb.Stateful;
import java.sql.*;
/**
*
* @author gunaseeli
*/
@Stateful
public class NewSessionBean_q_viji implements NewSessionBean_q_vijiRemote {
@Override
public String update(int bal) {
try{
Statement st;
Connection con=null;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:vvv");
st=con.createStatement();
String sql="insert into list values('"+bal+"')";
st.executeUpdate(sql);
st.close();
}

catch (Exception e)
{

}
return "it's saved";

// Add business logic below. (Right-click in editor and choose


// "Insert Code > Add Business Method")

888888888888888888888888888888888888888888888
Step 24:

Click the save button.


Goto the Project Menu.
Do the Right click in the the mouse
Click the ‘build option’.

Now the bean is built.


Step 25:
Now Go back to “Stateful_example_guna_servlet.java”.

Click “alt+insert “ inside the process request method.

Click “Call Enterprise Bean”.

STEP 26:
Choose the beans from the list.
Step 27:

Stateful_session_guna_exampleRemote stateful_session_guna_example
= lookupStateful_session_guna_exampleRemote();

The remote interface stateful_session_guna_example is generated by IDE.


Stateful_session_guna_exampleRemote stateful_session_guna_example =
lookupStateful_session_guna_exampleRemote();

Two Remote interface will be created. One for balance updation and another for
Updation the balance amount in the database.
Step 28:

Now remove the comment line from try block.

Call the business method “deposit” and “update” through the remote interfaces
int x= Integer.parseInt(request.getParameter("amt"));
int y=newSessionBean_viji.deposit(x);
String s=newSessionBean_q_viji.update(y);print the value of s
****
Print s

step 29:
Go to the project window. Right click on the project
Stateful_new_example - Run
Step 30:

Web page opens. Enter the an amount and click the submit button.

Step 26:

OUTPUT window

In the output window , u will get the output as “it is saved”

Now go to the table list in ms access database and check out the whether the
updated balance is inserted or not.

Once again go the client page and give some other amount.For eg Rs.2000

And click the submit button

Once again go to the table list in ms access database and check out the whether the
newly updated balance is inserted or not.
**********************************************************************

Complete servlet program:

import ejbb.Stateful_session_guna_exampleRemote;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
*
* @author gunaseeli
*/
@WebServlet(name="Stateful_example_guna_servlet",
urlPatterns={"/Stateful_example_guna_servlet"})
public class Stateful_example_guna_servlet extends HttpServlet {
Stateful_session_guna_exampleRemote stateful_session_guna_example =
lookupStateful_session_guna_exampleRemote();

/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
methods.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();

int a=Integer.parseInt(request.getParameter("amount"));
try {
int y = stateful_session_guna_example.deposit(a);

out.println("<html>");
out.println("<head>");
out.println("<title>Servlet Stateful_example_guna_servlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>The balance is " + y + "</h1>");
out.println("</body>");
out.println("</html>");

} finally {
out.close();
}
}

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the +


sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/**
* Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

/**
* Returns a short description of the servlet.
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>

private Stateful_session_guna_exampleRemote
lookupStateful_session_guna_exampleRemote() {
try {
Context c = new InitialContext();
return (Stateful_session_guna_exampleRemote)
c.lookup("java:global/Stateful_new_example/Stateful_new_example-
ejb/Stateful_session_guna_example!ejbb.Stateful_session_guna_exampleRemote");
} catch (NamingException ne) {
Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught",
ne);
throw new RuntimeException(ne);
}
}

***********************************************************
Index.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form action="Stateful_example_guna_servlet">
<input type="text" name="amount">
<input type="submit" value="submit the amount">

</form>

</body>
</html>
******************************************************************
Bean
package ejbb;

import javax.ejb.Stateful;

/**
*
* @author gunaseeli
*/
@Stateful
public class Stateful_session_guna_example implements
Stateful_session_guna_exampleRemote {

int bal=5000;
@Override
public int deposit(int amount) {
bal=bal+amount;
return bal;
}

// Add business logic below. (Right-click in editor and choose


// "Insert Code > Add Business Method")

}
******************************************************************
Update query program
package ejbb;

import javax.ejb.Stateful;
import java.sql.*;
/**
*
* @author gunaseeli
*/
@Stateful
public class NewSessionBean_q_viji implements NewSessionBean_q_vijiRemote {
@Override
public String update(int bal) {
try{
Statement st;
Connection con=null;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:vvv");
st=con.createStatement();
String sql="insert into list values('"+bal+"')";
st.executeUpdate(sql);
st.close();

catch (Exception e)
{

}
return "it's saved";

// Add business logic below. (Right-click in editor and choose


// "Insert Code > Add Business Method")

Você também pode gostar