| = CDI @SessionScoped |
| :index-group: CDI |
| :jbake-type: page |
| :jbake-status: published |
| |
| This example show the use of `@SessionScoped` annotation for injected objects. An object |
| which is defined as `@SessionScoped` is created once for every HTTPSession and is shared by all the |
| beans that inject it throughout the same HTTPSession. |
| |
| == Run the application: |
| |
| mvn clean install tomee:run |
| |
| == Example |
| |
| This example has an endpoint wherein a user provides a request parameter `name` which is persisted as a field in a session scoped bean `SessionBean` and |
| then retrieved through another endpoint. |
| |
| == Request |
| |
| GET http://localhost:8080/cdi-session-scope/set-name?name=Puneeth |
| |
| == Response |
| |
| `done, go to /name servlet` |
| |
| == Request |
| |
| GET http://localhost:8080/cdi-session-scope/name |
| |
| == Response |
| |
| name = {Puneeth} |
| |
| == SessionBean |
| |
| The annotation `@SessionScoped` specifies that a bean is session scoped ie there will be only one instance of the class associated with a particular HTTPSession. |
| |
| [source,java] |
| ---- |
| @SessionScoped |
| public class SessionBean implements Serializable { |
| |
| private String name; |
| |
| public String getName() { |
| return name; |
| } |
| |
| public void setName(String name) { |
| this.name = name; |
| } |
| } |
| ---- |
| |
| == InputServlet |
| |
| `InputServlet` is a generic servlet which is mapped to the url pattern `/set-name`. |
| The session scoped bean `SessionBean` has been injected into this servlet, and the incoming request parameter is set to the field `name` of the bean. |
| |
| [source,java] |
| ---- |
| @WebServlet(name = "input-servlet", urlPatterns = {"/set-name"}) |
| public class InputServlet extends HttpServlet { |
| |
| @Inject |
| private SessionBean bean; |
| |
| @Override |
| protected void service(HttpServletRequest req, HttpServletResponse resp |
| throws ServletException, IOException { |
| final String name = req.getParameter("name"); |
| if (name == null || name.isEmpty()) { |
| resp.getWriter().write("please add a parameter name=xxx"); |
| } else { |
| bean.setName(name); |
| resp.getWriter().write("done, go to /name servlet"); |
| } |
| |
| } |
| } |
| ---- |
| |
| == AnswerBean |
| |
| `AnswerBean` is a request scoped bean with an injected `SessionBean`. It has an `@PostConstruct` method |
| wherein the value from the `SessionBean` is retrieved and set to a field. |
| |
| [source,java] |
| ---- |
| public class AnswerBean { |
| |
| @Inject |
| private SessionBean bean; |
| |
| private String value; |
| |
| @PostConstruct |
| public void init() { |
| value = '{' + bean.getName() + '}'; |
| } |
| |
| public String value() { |
| return value; |
| } |
| } |
| ---- |
| |
| == OutputServlet |
| |
| `OutputServlet` is another servlet with `AnswerBean` as an injected field. When `/name` is called the value from `AnswerBean` is read and written to the response. |
| |
| [source,java] |
| ---- |
| @WebServlet(name = "output-servlet", urlPatterns = {"/name"}) |
| public class OutputServlet extends HttpServlet { |
| |
| @Inject |
| private AnswerBean bean; |
| |
| @Override |
| protected void service(HttpServletRequest req, HttpServletResponse resp) |
| throws ServletException, IOException { |
| final String name = bean.value(); |
| if (name == null || name.isEmpty()) { |
| resp.getWriter().write("please go to servlet /set-name please"); |
| } else { |
| resp.getWriter().write("name = " + name); |
| } |
| } |
| } |
| |
| ---- |
| |