aspose file tools*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Statelessness of stateless session bean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "Statelessness of stateless session bean " Watch "Statelessness of stateless session bean " New topic
Author

Statelessness of stateless session bean

Deepak Bobal
Ranch Hand

Joined: Feb 06, 2008
Posts: 96
this is stateful bean

package com.example;

import com.sun.xml.internal.ws.message.stream.StreamAttachment;
import javax.ejb.Stateless;


@Stateless
public class SprojBean implements SprojRemote {

String myName;
public boolean chek(String name)
{
if(name.equals("Deepak"))
{
myName=name;
return true;
}
else
return false;
}

Servlet



package foo;

import com.example.SprojRemote;
import java.io.IOException;
import java.io.PrintWriter;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class SprojServlet extends HttpServlet {
@EJB
private SprojRemote sprojBean;


protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try
{
String name=request.getParameter("uname");
boolean b=sprojBean.chek(name);
if(b)
{
out.println(name+" is Authenticated to Proceed");
}
else
{
out.println("Sorry "+name +" you are not Authenticated to Proceed");
}
out.println("<br>");
out.println(sprojBean.returnMYName());

} finally {
out.close();
}
}
}



and the jsp Client

<%@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>
<form action="serv.do">
<body bgcolor="orange">
<br>
<br>
<br>

<center>
<h2>
Stateless Bean Demo
<br>
<br>
<hr>
<br>
Enter User Name
<input type="text" name="uname">
<br>
<br>
<input type="Submit" value="Submit">
</h2>
</center>


</body>
</form>
</html>


This is working very good .

But to check the statelessness of the bean i added another web module SecondSproj and there a used another servlet "SecondSprojServlet.java" to call the same bean

Second servlet in Another Module (i am considering this as another client (if i am correct))


package Sfoo;

import com.example.SprojRemote;
import java.io.IOException;
import java.io.PrintWriter;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class SecondSProjServlet extends HttpServlet {
@EJB
private SprojRemote sprojBean;


protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try
{
String Sname=sprojBean.returnMYName();
out.println("Calling From Second Servlet "+Sname);
} finally {
out.close();
}
}
}


if i am giving name as Deepak at index.jsp it show at Servlet "SprojServlet.java " that "Deepak is Authenticated to Proceed"

after this if i run second servlet and call returnMyName()

it shows "Deepak"

but if the bean is stateless so it must return null

as it creates only one object for each client.


Please correct me where i am worng.

Thanking You





Constant dripping hollows out a stone....
Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
It doesn't "must" return null, but it "may" return null.


SCJA 1.0, SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJP 5.0, SCEA 5, SCBCD 5; OCUP - Fundamental, Intermediate and Advanced; IBM Certified Solution Designer - OOAD, vUML 2; SpringSource Certified Spring Professional
Deepak Bobal
Ranch Hand

Joined: Feb 06, 2008
Posts: 96
it's surprising

there are 32 active bean instances in pool

but while running 20 times it picks only the same instance
Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
Deepak Bobal wrote:
there are 32 active bean instances in pool

How do you that there are 32? Is it a maximum number?

You could write a client program to send many requests if you want to prove statelessness.
Deepak Bobal
Ranch Hand

Joined: Feb 06, 2008
Posts: 96
i noticed 32 as max pool size under configuration of glass fish.
Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
Deepak Bobal wrote:i noticed 32 as max pool size under configuration of glass fish.

That is maximum number but there maybe only 1 instance if there are no much requests.
You could write a client program that sends many request to to server.
santosh patil
Greenhorn

Joined: May 14, 2008
Posts: 5
For each request you are getting same bean from pool. While using stateless session bean one should ensure re-initialization of instance variables before returning bean to pool.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Statelessness of stateless session bean