注入到 servlet 中的 ConversationScoped bean 的行为类似于 RequestScoped bean
ConversationScoped bean injected in servlet behaves like a RequestScoped bean
我的意图只是通过 servlet 调用会话范围的 bean。 bean 本身应该将其 属性 ticketnumber
增加到 4.
这是 servlet:
@WebServlet
public class ConversationServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Inject
private GreetingTicket ticket;
@Override
public void init() throws ServletException {
System.out.println("Init");
ticket.beginConversation();
}
@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
System.out.println("DoGet");
PrintWriter writer = response.getWriter();
response.setContentType("text/html");
ticket.addTicketNumber();
System.out.println(ticket.getTicketNumber());
writer.println("<h1>" + ticket.getTicketNumber() + "</h1>");
if (ticket.getTicketNumber() == 4) {
System.out.println("END!");
ticket.endConversation();
}
writer.flush();
writer.close();
}
}
class GreetingTicket
是会话范围的 CDI-Bean:
@ConversationScoped
public class GreetingTicket implements Serializable {
@Inject
Conversation conversation;
private static final long serialVersionUID = 1L;
private int ticketNumber;
public GreetingTicket() {
System.out.println("greeting Ticket");
}
public int getTicketNumber() {
return ticketNumber;
}
public void setTicketNumber(int ticketNumber) {
this.ticketNumber = ticketNumber;
}
public void addTicketNumber() {
ticketNumber++;
System.out.println("TicketNumber increased to "+ticketNumber);
}
public void beginConversation() {
if (conversation.isTransient()) {
System.out.println("Conversation Begin!");
conversation.begin();
}
}
public void endConversation() {
if (!(conversation.isTransient())) {
System.out.println("Conversation End!");
conversation.end();
}
}
}
现在的结果是 GreetingTicket
的 ticketnumber
每次请求总是只计为 1 - 为什么?
@John Ament 的评论是正确的。您需要从客户端向服务器传递 cid
参数。
因为使用以下代码,您总是可以开始新的对话:
@Override
public void init() throws ServletException {
System.out.println("Init");
ticket.beginConversation();
}
您需要传递名为 cid
的请求参数,例如:
@WebServlet
public class ConversationServlet extends HttpServlet {
@Inject
private GreetingTicket ticket;
@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
final String cid = request.getParameter("cid");
// TODO: pass above cid to the conversation
// ...
}
}
我的意图只是通过 servlet 调用会话范围的 bean。 bean 本身应该将其 属性 ticketnumber
增加到 4.
这是 servlet:
@WebServlet
public class ConversationServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Inject
private GreetingTicket ticket;
@Override
public void init() throws ServletException {
System.out.println("Init");
ticket.beginConversation();
}
@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
System.out.println("DoGet");
PrintWriter writer = response.getWriter();
response.setContentType("text/html");
ticket.addTicketNumber();
System.out.println(ticket.getTicketNumber());
writer.println("<h1>" + ticket.getTicketNumber() + "</h1>");
if (ticket.getTicketNumber() == 4) {
System.out.println("END!");
ticket.endConversation();
}
writer.flush();
writer.close();
}
}
class GreetingTicket
是会话范围的 CDI-Bean:
@ConversationScoped
public class GreetingTicket implements Serializable {
@Inject
Conversation conversation;
private static final long serialVersionUID = 1L;
private int ticketNumber;
public GreetingTicket() {
System.out.println("greeting Ticket");
}
public int getTicketNumber() {
return ticketNumber;
}
public void setTicketNumber(int ticketNumber) {
this.ticketNumber = ticketNumber;
}
public void addTicketNumber() {
ticketNumber++;
System.out.println("TicketNumber increased to "+ticketNumber);
}
public void beginConversation() {
if (conversation.isTransient()) {
System.out.println("Conversation Begin!");
conversation.begin();
}
}
public void endConversation() {
if (!(conversation.isTransient())) {
System.out.println("Conversation End!");
conversation.end();
}
}
}
现在的结果是 GreetingTicket
的 ticketnumber
每次请求总是只计为 1 - 为什么?
@John Ament 的评论是正确的。您需要从客户端向服务器传递 cid
参数。
因为使用以下代码,您总是可以开始新的对话:
@Override
public void init() throws ServletException {
System.out.println("Init");
ticket.beginConversation();
}
您需要传递名为 cid
的请求参数,例如:
@WebServlet
public class ConversationServlet extends HttpServlet {
@Inject
private GreetingTicket ticket;
@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
final String cid = request.getParameter("cid");
// TODO: pass above cid to the conversation
// ...
}
}