• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Statelessness of stateless session bean

 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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




 
Ranch Hand
Posts: 1936
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It doesn't "must" return null, but it "may" return null.
 
Deepak Bobal
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1936
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i noticed 32 as max pool size under configuration of glass fish.
 
Hong Anderson
Ranch Hand
Posts: 1936
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic