伙计们,大家好,我对要为我的应用程序使用哪个会话 bean 感到困惑。我正在尝试构建一个像 Facebook 这样的移动网络应用程序,它可以同时允许多个用户。我上网以获取更多信息。从我从堆栈溢出和其他教程中收集的信息来看,有状态会话 bean 在事务内部和事务之间保持状态(会话状态),并且它对客户端意味着。Stateless 不支持多个客户端来池化 bean 实例。虽然 Singleton 有点类似于无状态 bean。
我的问题是我要为应用程序使用哪个会话 bean。谢谢你快速的回复。
注意:客户端(手机)与 servlet 通信,servlet 与 EJB 通信以从数据库中提取数据。
public class LoginServlet extends HttpServlet {
@EJB
CampusianDataBaseBeanLocal campusianDataBaseBean;
Campusian campusian;
Gson gson;
@Override
public void init() throws ServletException {
super.init();
campusian = new Campusian();
gson = new Gson();
}
/**
* 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();
try {
String username = request.getParameter("campusianUserName");
String password = request.getParameter("campusianPassword");
String gsonString;
campusian.setUserName(username);
campusian.setPassword(password);
System.out.println("First time: "+username);
/**
* This check if the username and password entered by user is correct.
* If yes set campusian setSuccess to true and convert the object to string using gson
* Else set object campusian method to false
*/
if (campusianDataBaseBean.login(campusian)) {
campusian.setSuccess(true);
System.out.println("Connected to the database");
/**
try {
connection.connect();
connection.login(username, password);
if (connection.isAuthenticated()) {
System.out.println("Connected: "+connection.getServiceName());
campusian.setConnection(connection);
campusian.setSuccess(true);
}else {
campusian.setSuccess(false);
}
} catch (XMPPException ex) {
Logger.getLogger(LoginServlet.class.getName()).log(Level.SEVERE, null, ex);
}
**/
gsonString = gson.toJson(campusian);
}else {
campusian.setSuccess(false);
gsonString = gson.toJson(campusian);
}
//this sends the gson string to the mobile user
out.print(gsonString);
} finally {
out.close();
}
}
}