Skip to content

Commit 5aea169

Browse files
author
Arun Gupta
committed
New sample to show stateful EJB
1 parent fbd9b57 commit 5aea169

File tree

5 files changed

+304
-0
lines changed

5 files changed

+304
-0
lines changed

ejb/stateful/nb-configuration.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project-shared-configuration>
3+
<!--
4+
This file contains additional configuration written by modules in the NetBeans IDE.
5+
The configuration is intended to be shared among all the users of project and
6+
therefore it is assumed to be part of version control checkout.
7+
Without this configuration present, some functionality in the IDE may be limited or fail altogether.
8+
-->
9+
<properties xmlns="http://www.netbeans.org/ns/maven-properties-data/1">
10+
<!--
11+
Properties that influence various parts of the IDE, especially code formatting and the like.
12+
You can copy and paste the single properties, into the pom.xml file and the IDE will pick them up.
13+
That way multiple projects can share the same settings (useful for formatting rules for example).
14+
Any value defined here will override the pom.xml file value but is only applicable to the current project.
15+
-->
16+
<org-netbeans-modules-maven-j2ee.netbeans_2e_hint_2e_deploy_2e_server>gfv3ee6</org-netbeans-modules-maven-j2ee.netbeans_2e_hint_2e_deploy_2e_server>
17+
</properties>
18+
</project-shared-configuration>

ejb/stateful/pom.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<groupId>org.javaee7.ejb</groupId>
6+
<artifactId>ejb-samples</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
<relativePath>../pom.xml</relativePath>
9+
</parent>
10+
11+
<groupId>org.javaee7.ejb</groupId>
12+
<artifactId>stateful</artifactId>
13+
<version>1.0-SNAPSHOT</version>
14+
<packaging>war</packaging>
15+
</project>
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved.
5+
*
6+
* The contents of this file are subject to the terms of either the GNU
7+
* General Public License Version 2 only ("GPL") or the Common Development
8+
* and Distribution License("CDDL") (collectively, the "License"). You
9+
* may not use this file except in compliance with the License. You can
10+
* obtain a copy of the License at
11+
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
12+
* or packager/legal/LICENSE.txt. See the License for the specific
13+
* language governing permissions and limitations under the License.
14+
*
15+
* When distributing the software, include this License Header Notice in each
16+
* file and include the License file at packager/legal/LICENSE.txt.
17+
*
18+
* GPL Classpath Exception:
19+
* Oracle designates this particular file as subject to the "Classpath"
20+
* exception as provided by Oracle in the GPL Version 2 section of the License
21+
* file that accompanied this code.
22+
*
23+
* Modifications:
24+
* If applicable, add the following below the License Header, with the fields
25+
* enclosed by brackets [] replaced by your own identifying information:
26+
* "Portions Copyright [year] [name of copyright owner]"
27+
*
28+
* Contributor(s):
29+
* If you wish your version of this file to be governed by only the CDDL or
30+
* only the GPL Version 2, indicate your decision by adding "[Contributor]
31+
* elects to include this software in this distribution under the [CDDL or GPL
32+
* Version 2] license." If you don't indicate a single choice of license, a
33+
* recipient has the option to distribute your version of this file under
34+
* either the CDDL, the GPL Version 2 or to extend the choice of license to
35+
* its licensees as provided above. However, if you add GPL Version 2 code
36+
* and therefore, elected the GPL Version 2 license, then the option applies
37+
* only if the new code is made subject to such option by the copyright
38+
* holder.
39+
*/
40+
package org.javaee7.ejb.stateful;
41+
42+
import java.util.ArrayList;
43+
import java.util.List;
44+
import javax.ejb.Remove;
45+
import javax.ejb.Stateful;
46+
47+
/**
48+
* @author Arun Gupta
49+
*/
50+
@Stateful
51+
public class Cart {
52+
53+
List<String> items;
54+
55+
public Cart() {
56+
items = new ArrayList<>();
57+
}
58+
59+
public void addItem(String item) {
60+
items.add(item);
61+
}
62+
63+
public void removeItem(String item) {
64+
items.remove(item);
65+
}
66+
67+
public void purchase() {
68+
//. . .
69+
}
70+
71+
public List<String> getItems() {
72+
return items;
73+
}
74+
75+
@Remove
76+
public void remove() {
77+
items = null;
78+
}
79+
}
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved.
5+
*
6+
* The contents of this file are subject to the terms of either the GNU
7+
* General Public License Version 2 only ("GPL") or the Common Development
8+
* and Distribution License("CDDL") (collectively, the "License"). You
9+
* may not use this file except in compliance with the License. You can
10+
* obtain a copy of the License at
11+
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
12+
* or packager/legal/LICENSE.txt. See the License for the specific
13+
* language governing permissions and limitations under the License.
14+
*
15+
* When distributing the software, include this License Header Notice in each
16+
* file and include the License file at packager/legal/LICENSE.txt.
17+
*
18+
* GPL Classpath Exception:
19+
* Oracle designates this particular file as subject to the "Classpath"
20+
* exception as provided by Oracle in the GPL Version 2 section of the License
21+
* file that accompanied this code.
22+
*
23+
* Modifications:
24+
* If applicable, add the following below the License Header, with the fields
25+
* enclosed by brackets [] replaced by your own identifying information:
26+
* "Portions Copyright [year] [name of copyright owner]"
27+
*
28+
* Contributor(s):
29+
* If you wish your version of this file to be governed by only the CDDL or
30+
* only the GPL Version 2, indicate your decision by adding "[Contributor]
31+
* elects to include this software in this distribution under the [CDDL or GPL
32+
* Version 2] license." If you don't indicate a single choice of license, a
33+
* recipient has the option to distribute your version of this file under
34+
* either the CDDL, the GPL Version 2 or to extend the choice of license to
35+
* its licensees as provided above. However, if you add GPL Version 2 code
36+
* and therefore, elected the GPL Version 2 license, then the option applies
37+
* only if the new code is made subject to such option by the copyright
38+
* holder.
39+
*/
40+
package org.javaee7.ejb.stateful;
41+
42+
import java.io.IOException;
43+
import java.io.PrintWriter;
44+
import javax.inject.Inject;
45+
import javax.servlet.ServletException;
46+
import javax.servlet.annotation.WebServlet;
47+
import javax.servlet.http.HttpServlet;
48+
import javax.servlet.http.HttpServletRequest;
49+
import javax.servlet.http.HttpServletResponse;
50+
51+
/**
52+
* @author Arun Gupta
53+
*/
54+
@WebServlet(urlPatterns = {"/TestServlet"})
55+
public class TestServlet extends HttpServlet {
56+
@Inject Cart bean;;
57+
58+
/**
59+
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
60+
* methods.
61+
*
62+
* @param request servlet request
63+
* @param response servlet response
64+
* @throws ServletException if a servlet-specific error occurs
65+
* @throws IOException if an I/O error occurs
66+
*/
67+
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
68+
throws ServletException, IOException {
69+
response.setContentType("text/html;charset=UTF-8");
70+
try (PrintWriter out = response.getWriter()) {
71+
out.println("<!DOCTYPE html>");
72+
out.println("<html>");
73+
out.println("<head>");
74+
out.println("<title>Adding/Removing items from Stateful Bean</title>");
75+
out.println("</head>");
76+
out.println("<body>");
77+
out.println("<h1>Adding/Removing items from Stateful Bean</h1>");
78+
out.println("<h2>Adding items</h2>");
79+
bean.addItem("apple");
80+
bean.addItem("banana");
81+
bean.addItem("mango");
82+
bean.addItem("kiwi");
83+
bean.addItem("passion fruit");
84+
out.println("added");
85+
out.println("<h2>Listing items</h2>");
86+
out.println(bean.getItems());
87+
out.println("<h2>Removing item</h2>");
88+
bean.removeItem("banana");
89+
out.println("removed");
90+
out.println("<h2>Listing items</h2>");
91+
out.println(bean.getItems());
92+
out.println("</body>");
93+
out.println("</html>");
94+
}
95+
}
96+
97+
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
98+
/**
99+
* Handles the HTTP <code>GET</code> method.
100+
*
101+
* @param request servlet request
102+
* @param response servlet response
103+
* @throws ServletException if a servlet-specific error occurs
104+
* @throws IOException if an I/O error occurs
105+
*/
106+
@Override
107+
protected void doGet(HttpServletRequest request, HttpServletResponse response)
108+
throws ServletException, IOException {
109+
processRequest(request, response);
110+
}
111+
112+
/**
113+
* Handles the HTTP <code>POST</code> method.
114+
*
115+
* @param request servlet request
116+
* @param response servlet response
117+
* @throws ServletException if a servlet-specific error occurs
118+
* @throws IOException if an I/O error occurs
119+
*/
120+
@Override
121+
protected void doPost(HttpServletRequest request, HttpServletResponse response)
122+
throws ServletException, IOException {
123+
processRequest(request, response);
124+
}
125+
126+
/**
127+
* Returns a short description of the servlet.
128+
*
129+
* @return a String containing servlet description
130+
*/
131+
@Override
132+
public String getServletInfo() {
133+
return "Short description";
134+
}// </editor-fold>
135+
136+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<!--
2+
/*
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4+
*
5+
* Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved.
6+
*
7+
* The contents of this file are subject to the terms of either the GNU
8+
* General Public License Version 2 only ("GPL") or the Common Development
9+
* and Distribution License("CDDL") (collectively, the "License"). You
10+
* may not use this file except in compliance with the License. You can
11+
* obtain a copy of the License at
12+
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
13+
* or packager/legal/LICENSE.txt. See the License for the specific
14+
* language governing permissions and limitations under the License.
15+
*
16+
* When distributing the software, include this License Header Notice in each
17+
* file and include the License file at packager/legal/LICENSE.txt.
18+
*
19+
* GPL Classpath Exception:
20+
* Oracle designates this particular file as subject to the "Classpath"
21+
* exception as provided by Oracle in the GPL Version 2 section of the License
22+
* file that accompanied this code.
23+
*
24+
* Modifications:
25+
* If applicable, add the following below the License Header, with the fields
26+
* enclosed by brackets [] replaced by your own identifying information:
27+
* "Portions Copyright [year] [name of copyright owner]"
28+
*
29+
* Contributor(s):
30+
* If you wish your version of this file to be governed by only the CDDL or
31+
* only the GPL Version 2, indicate your decision by adding "[Contributor]
32+
* elects to include this software in this distribution under the [CDDL or GPL
33+
* Version 2] license." If you don't indicate a single choice of license, a
34+
* recipient has the option to distribute your version of this file under
35+
* either the CDDL, the GPL Version 2 or to extend the choice of license to
36+
* its licensees as provided above. However, if you add GPL Version 2 code
37+
* and therefore, elected the GPL Version 2 license, then the option applies
38+
* only if the new code is made subject to such option by the copyright
39+
* holder.
40+
*/
41+
-->
42+
<%@page contentType="text/html" pageEncoding="UTF-8"%>
43+
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
44+
"http://www.w3.org/TR/html4/loose.dtd">
45+
46+
<html>
47+
<head>
48+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
49+
<title>EJB : Stateful</title>
50+
</head>
51+
<body>
52+
<h1>EJB : Stateful</h1>
53+
54+
<a href="${pageContext.request.contextPath}/TestServlet"/>Call beans</a>.
55+
</body>
56+
</html>

0 commit comments

Comments
 (0)