状态会话bean

本文介绍了一个使用Stateful EJB和Servlet的Java Web应用案例,演示了如何在Stateful会话Bean中存储和操作数据,并通过Servlet接收用户输入,更新数据并返回结果。

 

 

StatefulExample

 

NewBean.java

 

 

ExpandedBlockStart.gifView Code
 1 /*
 2  * To change this template, choose Tools | Templates
 3  * and open the template in the editor.
 4  */
 5 package beans;
 6 
 7 import java.util.ArrayList;
 8 import java.util.List;
 9 import javax.annotation.PostConstruct;
10 import javax.ejb.Stateful;
11 import javax.ejb.LocalBean;
12 
13 /**
14  *
15  * @author teemu
16  */
17 @Stateful
18 @LocalBean
19 public class NamesBean {
20 
21     private List<String> names;
22     
23     @PostConstruct
24     private void initialize(){
25         names = new ArrayList<String>();
26     }    
27     
28     public List<String> getNames() {
29         return names;
30     }
31 
32     public void addName(String name) {
33         names.add(name);
34     }
35 
36     
37     
38 }

 

 

index.jsp

 

ExpandedBlockStart.gifView Code
 1 <%@page contentType="text/html" pageEncoding="UTF-8"%>
 2 <!DOCTYPE html>
 3 <html>
 4     <head>
 5         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 6         <title>JSP Page</title>
 7     </head>
 8     <body>
 9          <form action="NamesServlet" method="get">
10             Name: <input type="text" name="name" /> <br/>
11             <input type="submit" value="Add name" />                        
12         </form>
13     </body>
14 </html>

 

 

NamesServlet.java

 

ExpandedBlockStart.gifView Code
  1 /*
  2  * To change this template, choose Tools | Templates
  3  * and open the template in the editor.
  4  */
  5 package servlets;
  6 
  7 import beans.NamesBean;
  8 import java.io.IOException;
  9 import java.io.PrintWriter;
 10 import java.util.List;
 11 import javax.ejb.EJB;
 12 import javax.servlet.ServletException;
 13 import javax.servlet.annotation.WebServlet;
 14 import javax.servlet.http.HttpServlet;
 15 import javax.servlet.http.HttpServletRequest;
 16 import javax.servlet.http.HttpServletResponse;
 17 
 18 /**
 19  *
 20  * @author teemu
 21  */
 22 @WebServlet(name = "NamesServlet", urlPatterns = {"/NamesServlet"})
 23 public class NamesServlet extends HttpServlet {
 24 
 25     @EJB
 26     private NamesBean bean;    
 27     private List<String> list;
 28     
 29     /**
 30      * Processes requests for both HTTP
 31      * <code>GET</code> and
 32      * <code>POST</code> methods.
 33      *
 34      * @param request servlet request
 35      * @param response servlet response
 36      * @throws ServletException if a servlet-specific error occurs
 37      * @throws IOException if an I/O error occurs
 38      */
 39     protected void processRequest(HttpServletRequest request, HttpServletResponse response)
 40             throws ServletException, IOException {
 41         response.setContentType("text/html;charset=UTF-8");
 42         PrintWriter out = response.getWriter();
 43         try {
 44             
 45             bean.addName(request.getParameter("name"));
 46             list = bean.getNames();
 47             
 48             /*
 49              * TODO output your page here. You may use following sample code.
 50              */
 51             out.println("<html>");
 52             out.println("<head>");
 53             out.println("<title>Servlet NamesServlet</title>");            
 54             out.println("</head>");
 55             out.println("<body>");
 56             out.println("<h1>List of names: </h1>");
 57             for(String s:list)  {
 58                 out.println(s+"<br>");
 59             }
 60             out.println("</body>");
 61             out.println("</html>");
 62         } finally {            
 63             out.close();
 64         }
 65     }
 66 
 67     // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
 68     /**
 69      * Handles the HTTP
 70      * <code>GET</code> method.
 71      *
 72      * @param request servlet request
 73      * @param response servlet response
 74      * @throws ServletException if a servlet-specific error occurs
 75      * @throws IOException if an I/O error occurs
 76      */
 77     @Override
 78     protected void doGet(HttpServletRequest request, HttpServletResponse response)
 79             throws ServletException, IOException {
 80         processRequest(request, response);
 81     }
 82 
 83     /**
 84      * Handles the HTTP
 85      * <code>POST</code> method.
 86      *
 87      * @param request servlet request
 88      * @param response servlet response
 89      * @throws ServletException if a servlet-specific error occurs
 90      * @throws IOException if an I/O error occurs
 91      */
 92     @Override
 93     protected void doPost(HttpServletRequest request, HttpServletResponse response)
 94             throws ServletException, IOException {
 95         processRequest(request, response);
 96     }
 97 
 98     /**
 99      * Returns a short description of the servlet.
100      *
101      * @return a String containing servlet description
102      */
103     @Override
104     public String getServletInfo() {
105         return "Short description";
106     }// </editor-fold>
107 }

 

 C:\Users\iltaek\Documents\NetBeansProjects\StatefullApp\StatefullApp-war\nbproject\build-impl.xml:752: The module has not been deployed.
See the server log for details.
BUILD FAILED (total time: 1 second)

 

转载于:https://www.cnblogs.com/jinrize/archive/2012/04/15/2450759.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值