Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

"Online Tweeter Enterprise Application" : Creating a Singleton Session Bean in NetBeans EJB module - Part 7


Lets continue building "Online Tweeter Enterprise Application" in NetBeans. In this section of tutorial, you will create a Singleton Session Bean by name "ActiveFollowersOnline.java". A Singleton Session Bean is a enterprise bean which are instantiated once per application and has lifecycle till application is alive.

Step 1: Open "Tweeter-war" project and right click Source Packages and then select New and than Other as shown in fig below:







Step 2: On clicking Other a dialog box appears by name New File. In the Categories: list select Enterprise JavaBeans and in the File Types: select Session Bean as shown in fig below.







Step 3: Click

.
New Session Bean dialog box gets open. It prompts us to enter EJB Name: , Project: , Location: , Package: , Session Type: and Create Interface: etc. Enter the values as shown in the fig below.



Step 4: Click




 

A new Singleton Session Bean gets created by name "ActiveFollowersOnline.java" in the "Tweeter-war" module in the package provided at the time of creating the bean. It has most of the source code already generated by NetBeans. Kindly add or remove additional code provided below.


package com.hubberspot.ejb;

import javax.ejb.Singleton;
import javax.ejb.LocalBean;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

// @Singleton annotation informs container that
// treat this bean as a Singleton bean.

// @LocalBean annotation informs container that
// this session bean exposes a no-interface view.

// @WebListener annotation informs container that
// this class is a web listener which will listen to 
// various events happening during lifecyle of application
// Here this class listens to Http Session when a session
// is created or destroyed.

// We make class implements HttpSessionListener which has two
// methods sessionCreated() and sessionDestroyed() , which 
// are called by the container whenever a session is created 
// or destroyed

@Singleton
@LocalBean
@WebListener
public class ActiveFollowersOnline implements HttpSessionListener {

    private static int followersOnline = 0;

    @Override
    public void sessionCreated(HttpSessionEvent se) {
        
        System.out.print("Session Created : ");
        followersOnline++;
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
        followersOnline--;
    }

    public int getFollowersOnline() {
        return followersOnline;
    }   
    
}




In the next section of this blog (part 8) you will learn how to create a Servlet in NetBeans for this application in the Web module.



"Online Tweeter Enterprise Application" : Creating a JSP page in NetBeans Web module - Part 6

Lets continue building "Online Tweeter Enterprise Application" in NetBeans. In this section of tutorial, you will create a Java Server Pages (JSP) by name "tweets.jsp". A JSP page is like a User Interface for Web Application. Its like a dynamic web page having html with java embedded into it.

Step 1:
Open "Tweeter-war" project and right click Web Pages and then select New and than Other as shown in fig below.





Step 2: On clicking Other a dialog box appears by name New File. In the Categories: list select Web and in the File Types: select JSP as shown in fig below.




Step 3: Click

.
New JSP dialog box gets open. It prompts us to enter values for the File Name: , Project: , Location: , Folder: , Created File: and Options: .Enter the required values as per fig shown below.




Step 4: Click




 

A new jsp page gets created by the name tweets.jsp in the "Tweeter-war" module.Kindly add or remove additional code provided below. 

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        
        <title>Tweeter</title>
    </head>
    <body>
        <table border="0" bgcolor="aquagreen" cellpadding="0" cellspacing="0" width="60%">
            <tr>
                <td>
                    <table border="0" align="center" cellpadding="0" cellspacing="0" width="100%">
                        <tr>
                            <td valign="middle" align="center" style="padding-right:0px; padding-left:0px; padding-bottom:0px; font:24px/30px Georgia; width:228px; color:#FFFFFF; padding-top:0px; height:37px;">
                                What's happening ?. !!!
                            </td>
                        </tr>
                    </table>                    
                </td>
            </tr>
            <tr align="left" valign="top">
                <td height="20">
                    <hr />
                </td>
            </tr>
            <tr>
                <td>
                    <form name="tweetsForm" action="TweetsSubmit" method="post">
                        <table border="0" cellpadding="0" cellspacing="0" width="100%">
                            <tr>
                                <td align="right" valign="top">
                                    UserName:
                                </td>
                                <td>
                                    <input type="text" name="txtUsername" size="50" maxlength="75"/>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <br/>
                                </td>
                            </tr>
                            <tr>
                                <td align="right" valign="top">
                                    Your Tweet:
                                </td>
                                <td>
                                    <textarea name="txtTweet" cols="38" rows="6"></textarea> 
                                </td>                                    
                            </tr>
                            <tr align="left">
                                <td height="20">
                                    <hr />
                                </td>
                                <td height="20">
                                    <hr />
                                </td>
                            </tr>
                            <tr>
                                <td colspan="2" align="left">
                                    <input type="submit" value="Tweet" />
                                </td>
                            </tr>
                        </table>
                    </form>
                </td>
            </tr>            
        </table>
    </body>
</html>



Output of the jsp on browser window : 


tweets.jsp is a simple web page having few html components which prompts user to enter UserName , Tweet he/she wants to publish and than click on Submit button.  

In the next section of this blog (part 7) you will learn how to create a Singleton Session Bean in NetBeans for this application in the Web module. 

"Online Tweeter Enterprise Application" : Creating a Stateless Session Bean in NetBeans EJB module - Part 5


Lets continue building "Online Tweeter Enterprise Application" in NetBeans. In this section of tutorial, you will create a Stateless Session Bean by name "TweetsFinder.java". A Stateless Session Bean is a enterprise bean that is short - lived object which fulfill single client request and remember nothing about the client in subsequent requests.

Step 1: Open "tweeter-ejb" project and right click Source Packages and then select New and than Other as shown in fig below:










Step 2: On clicking Other a dialog box appears by name New File. In the Categories: list select Enterprise JavaBeans and in the File Types: select Session Bean as shown in fig below.































Step 3: Click

.
New Session Bean dialog box gets open. It prompts us to enter EJB Name: , Project: , Location: , Package: , Session Type: and Create Interface: etc. Enter the values as shown in the fig below.

Step 4: Click




 

A new Stateless Session Bean gets created by name "TweetsFinder.java" in the "tweeter-ejb" module in the package provided at the time of creating the bean. It has most of the source code already generated by NetBeans. Kindly add or remove additional code provided below.


package com.hubberspot.ejb;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaQuery;

// @Stateless annotation provides container information to
// treat this Java class as a Stateless Session Bean.
@Stateless
public class TweetsFinder {

    // @PersistenceContext injects dependency for the EntityManager
    // by loading persistence.xml
    // EntityManager provides an interface for database persistence
    // such as to persist, merge , load and query objects.
    @PersistenceContext(name = "Tweeter-ejbPU")
    EntityManager tweetsManager;

    // This Stateless Session Bean has only one method
    // findAllTweets(). 
    public List< Tweet > findAllTweets() {

        // Here, it creates CriteriaQuery object
        // from EntityManager by calling its 
        // getCriteriaBuilder().createQuery() which returns 
        // back instance of CriteriaQuery.
        CriteriaQuery criteriaQuery = tweetsManager.getCriteriaBuilder().createQuery();

        // CriteriaQuery has two methods by name select()
        // and from which fetches data from database as 
        // normal SQL select query.        
        criteriaQuery.select(criteriaQuery.from(Tweet.class));

        // Creates a Query instance using criteriaQuery object
        // and EntityManager's createQuery() method.    
        Query query = tweetsManager.createQuery(criteriaQuery);

        // Finally it returns back list of tweets stored in 
        // database.    
        return query.getResultList();

    }
}





Video tutorial to demonstrate How to create a Java EE Stateless Session Bean (EJB) in an Enterprise Application using NetBeans.








In the next section of this blog (part 6) you will learn how to create a JSP page in NetBeans for this application in the Web module.



 
© 2021 Learn Java by Examples Template by Hubberspot