Develop simple EJB application to demonstrate Servlet Hit count using Singleton Session Beans.

 index.html

<!DOCTYPE html>  

<html>  

    <head>  

        <title>Hit Count Demo</title>  

    </head>  

    <body>  

        <form action="CCClient" method="post">  

            <input type="submit" value="Take Hit Count Demo with EJB Session Singleton Bean"/>  

        </form>  

    </body>  

</html> 

CCClient.java

package mypack; 

import java.io.IOException; 

import java.io.PrintWriter; 

import javax.servlet.ServletException; 

import javax.servlet.http.HttpServlet; 

import javax.servlet.http.HttpServletRequest; 

import javax.servlet.http.HttpServletResponse; 

import javax.ejb.EJB;  

import mybeans.HitCntBean;  

public class CCClient extends HttpServlet { 

    @EJB  

    HitCntBean cntBean;  

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)             throws ServletException, IOException {         

response.setContentType("text/html;charset=UTF-8");        

 try (PrintWriter out = response.getWriter()) {             

            out.println("<!DOCTYPE html>");             

            out.println("<html>");             

            out.println("<head>"); 

            out.println("<title>Servlet CCClient</title>");                        

             out.println("</head>");             out.println("<body>"); 

             out.println("<h1>You have visited this page: " + cntBean.incrHitCount() + " time/s</h1>");  

            out.println("</body>");             

            out.println("</html>"); 

        } 

    } 

HitCntBean.java

package mybeans; 

import javax.ejb.Singleton; 

@Singleton public class HitCntBean {      

private int count;      

public synchronized int incrHitCount()  

    {  

        return count++;  

    } 

}



Post a Comment

0 Comments