代码是HttpServlet
,它使用Stateless
和Statefull
bean来打印从表单获取的输入值。实际上,'bounce'
方法返回我传递给它的字符串。为什么使用Stateless beans
将值返回到html页面?与不使用bean编写程序不一样吗?
@EJB
private StatelessSessionBean sless;
@EJB//(name="StatefulSession", beanInterface=StatefulSession.class)
private StatefulSessionBean sful;
@Override
public void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
try {
out.println("<HTML> <HEAD> <TITLE> Servlet & Stateless/StatefulEJBs</TITLE> </HEAD> <BODY BGCOLOR=white>");
out.println("<CENTER> <FONT size=+1> Servlet2EJBs: Please enter a message </FONT> </CENTER> <p> ");
out.println("<form method=\"POST\">");
out.println("<TABLE>");
out.println("<tr><td>Message for the stateless EJB: </td>");
out.println("<td><input type=\"text\" name=\"sl_msg\"> </td>");
out.println("</tr><tr><td></td>");
out.println("<td><input type=\"submit\" name=\"sub1\"> </td>");
out.println("</tr>");
out.println("</TABLE>");
out.println("</form>");
out.println("<form method=\"POST\">");
out.println("<TABLE>");
out.println("<tr><td>Message for the stateful EJB: </td>");
out.println("<td><input type=\"text\" name=\"sf_msg\"> </td>");
out.println("</tr><tr><td></td>");
out.println("<td><input type=\"submit\" name=\"sub2\"> </td>");
out.println("</tr>");
out.println("</TABLE>");
out.println("</form>");
String val = req.getParameter("sl_msg");
if ((val != null) && (val.trim().length() > 0)) {
out.println("<FONT size=+1 color=red> Message back from StatelessSessionBean: </FONT>"
+ HTMLFilter.filter(sless.bounce(val)) + "<br>");
}
else {
val = req.getParameter("sf_msg");
if ((val != null) && (val.trim().length() > 0)) {
out.println("<FONT size=+1 color=red> Message(s) back from StatefulSessionBean: </FONT>"
+ "<br>"+HTMLFilter.filter(sful.addAndRead(val)).replace("\n", "<br>") + "<br>");
}
}
out.println("</BODY> </HTML> ");
} catch (Exception ex) {
ex.printStackTrace();
System.out.println("webclient servlet test failed");
throw new ServletException(ex);
}
}
}