@Stateless //表示此Bean为无状态Bean(服务端不存储Bean的状态,可节省Server的内存)
@Remote({Login.class}) //表示遥控(@remote)的接口,本地(@local)必须保证与EJB相关业务逻辑操作者与调用者在同一ApplicationServer上
public class LoginBean implements Login {
public User login(String username, String password) {
// TODO Auto-generated method stub
try {
Connection conn= DBOper.getConnection();
CallableStatement cs=conn.prepareCall("{call up_users_login(?,?)}");
cs.setString(1, username);
cs.setString(2, password);
ResultSet rs= cs.executeQuery();
while(rs.next()){
User user=new User(rs.getString("username"),rs.getString("password"),rs.getString("description"),rs.getBoolean("isadmin"));
return user;
//User对象必须实现序列化,(用于web传播)
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
至此,ejb3登陆功能,已经实现最后可通过以下方法得的想要的对象
public class EJBFactory {
public static Object getEJB(String jndipath) {
try {
Properties props = new Properties();
//
props.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
props.setProperty("java.naming.provider.url", "localhost:1099");
props.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
InitialContext ctx = new InitialContext(props);
return ctx.lookup(jndipath);
} catch (NamingException e) {
e.printStackTrace();
}
return null;
}
}
调用此方法需要注意:
Login loginAdmin= (Login)EJBFactory.getEJB("LoginBean/remote");