package src;
import javax.ejb.Remote;
@Remote
public interface Hello {
public String sayHello(String name);
}
HelloLocal
package src;
import javax.ejb.Remote;
@Remote
public interface Hello {
public String sayHello(String name);
}
HelloBean.java
package src;
import java.util.Properties;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import org.jboss.aop.microcontainer.aspects.jndi.JndiBinding;
import src.count.Count;
/**
* Session Bean implementation class HelloBean
*/
@Stateless(name="HelloBean")
@JndiBinding(name="HelloBean")
public class HelloBean implements Hello, HelloLocal {
@EJB(name="CountBean") Count count;
//Count count = null;
/**
* Default constructor.
*/
public HelloBean() {
// TODO Auto-generated constructor stub
}
@PostConstruct
public void init()
{
}
public String sayHello(String name)
{
//count = lookupCountBean();
count.addVisitor(name);
count.saveVisitor();
return "Hello, " + name;
}
private Count lookupCountBean() {
try {
Properties properties = new Properties();
properties.put("java.naming.factory.initial",
"org.jnp.interfaces.NamingContextFactory");
properties.put("java.naming.factory.url.pkgs",
"org.jboss.naming rg.jnp.interfaces");
properties.put("java.naming.provider.url", "jnp://localhost:1091");
Context ctx = new InitialContext(properties);
System.out.println("Got context");
return (Count) ctx.lookup("CountBean/remote");
}
catch(NamingException ne) {
throw new RuntimeException(ne);
}
}
}
Count.java
package src.count;
import java.util.ArrayList;
import javax.ejb.Remote;
@Remote
public interface Count {
public void saveVisitor();
public void addVisitor(String user);
public int totalVisitor();
public int onlineVisitor();
public ArrayList
}
CountLocal.java
Same as above Count.java
Contents
CountBean.java
package src.count;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Vector;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.Stateful;
import org.jboss.aop.microcontainer.aspects.jndi.JndiBinding;
/**
* Session Bean implementation class CountBean
*/
@Stateful(name="CountBean")
@JndiBinding(name="CountBean")
public class CountBean implements Count, CountLocal {
/**
* Default constructor.
*/
Vector
int onlineUsers = 0;
int totalUsers = 0;
public CountBean() {
}
@PostConstruct
public void init(String name) {
// TODO Auto-generated constructor stub
visitors.add(name);
onlineUsers ++;
totalUsers ++;
}
public CountBean(String name) {
// TODO Auto-generated constructor stub
visitors.add(name);
onlineUsers ++;
totalUsers ++;
}
@PreDestroy
public void remove() {
onlineUsers --;
FileWriter file = null;
BufferedWriter bFile = null;
try
{
file = new FileWriter(new File("c:\\users.txt"));
bFile = new BufferedWriter(file);
for( String strName : visitors)
{
bFile.write(strName+"\n");
}
}
catch(Exception ex)
{
}
finally
{
try {
if (bFile!= null) bFile.close();
if (file != null) file.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public int totalVisitor() {return onlineUsers; }
public int onlineVisitor() { return totalUsers; }
public ArrayList
public void addVisitor(String user) {
visitors.add(user);
}
public void saveVisitor() {
FileWriter file = null;
BufferedWriter bFile = null;
try
{
file = new FileWriter(new File("c:\\users.txt"));
bFile = new BufferedWriter(file);
for( String strName : visitors)
{
bFile.write(strName+"\n");
System.out.println("Name = " + strName);
}
}
catch(Exception ex)
{
}
finally
{
try {
if (bFile!= null) bFile.close();
if (file != null) file.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
index.jsp
InitialContext ctx = new InitialContext();
try
{
Hello hello = (Hello) ctx.lookup("HelloBean/remote");
String name = "";
if (request.getParameter("tname") == null)
{
name = (String) session.getValue("name");
}
else
name = request.getParameter("tname");
out.println( hello.sayHello( name ));
}
catch(Exception ex)
{
ex.printStackTrace();
}
No comments:
Post a Comment