Stateless -Session Bean

Similar documents
Demonstration of Servlet, JSP with Tomcat, JavaDB in NetBeans

Develop an Enterprise Java Bean for Banking Operations

Session 9. Introduction to Servlets. Lecture Objectives

CREATE A SERVLET PROGRAM TO DISPLAY THE STUDENTS MARKS. To create a servlet program to display the students marks

JAVA SERVLET. Server-side Programming INTRODUCTION

Web Applications 2. Java EE Martin Klíma. WA2 Slide 1

INTRODUCTION TO SERVLETS AND WEB CONTAINERS. Actions in Accord with All the Laws of Nature

Introduction to Servlets. After which you will doget it

Servlet Basics. Agenda

Java Enterprise Edition. Java EE Oct Dec 2016 EFREI/M1 Jacques André Augustin Page 1

Enterprise Java Technologies (Part 1 of 3) Component Architecture. Overview of Java EE. Java Servlets

Session 8. Introduction to Servlets. Semester Project

Java TM. JavaServer Faces. Jaroslav Porubän 2008

To create a view for students, staffs and courses in your departments using servlet/jsp.

Advanced Topics in Operating Systems. Manual for Lab Practices. Enterprise JavaBeans

ServletConfig Interface

Kamnoetvidya Science Academy. Object Oriented Programming using Java. Ferdin Joe John Joseph. Java Session

Principles and Techniques of DBMS 6 JSP & Servlet

Advanced Internet Technology Lab # 4 Servlets

Web based Applications, Tomcat and Servlets - Lab 3 -

Accessing EJB in Web applications

Java servlets CSCI 470: Web Science Keith Vertanen Copyright 2013

SWE642 Oct. 22, 2003

Handout 31 Web Design & Development

III/IV B.Tech (Regular/Supplementary) DEGREE EXAMINATION. Computer Science & Engineering. Answer ONE question from each unit.

Introduction. This course Software Architecture with Java will discuss the following topics:

Session 8. JavaBeans. Reading & Reference. Reading. Reference. Session 8 Java Beans. 2/27/2013 Robert Kelly, Head First Chapter 3 (MVC)

CIS 3952 [Part 2] Java Servlets and JSP tutorial

sessionx Desarrollo de Aplicaciones en Red A few more words about CGI CGI Servlet & JSP José Rafael Rojano Cáceres

JavaServer Pages (JSP)

Introduction. Literature: Steelman & Murach, Murach s Java Servlets and JSP. Mike Murach & Associates Inc, 2003

Database Systems Lab. 11. JSP I 충남대학교컴퓨터공학과 데이타베이스시스템연구실

J2ME With Database Connection Program

Introduction. This course Software Architecture with Java will discuss the following topics:

OCP JavaEE 6 EJB Developer Study Notes

Supplement IV.E: Tutorial for Tomcat For Introduction to Java Programming By Y. Daniel Liang

1.264 Lecture 15. Web development environments: JavaScript Java applets, servlets Java (J2EE) Active Server Pages

COMP9321 Web Application Engineering

Servlets. An extension of a web server runs inside a servlet container

2. Follow the installation directions and install the server on ccc. 3. We will call the root of your installation as $TOMCAT_DIR

A.1 JSP A.2 JSP JSP JSP. MyDate.jsp page contenttype="text/html; charset=windows-31j" import="java.util.calendar" %>

Session 20 Data Sharing Session 20 Data Sharing & Cookies

Thu 10/26/2017. Created RESTful Web Service, JavaDB, Java Persistence API, Glassfish server in NetBeans 8

The Structure and Components of

Advanced Web Technology

Introduction. This course Software Architecture with Java will discuss the following topics.

Web Application Services Practice Session #2

Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 2

Introduction to Session beans EJB 3.0

Servlet. Web Server. Servlets are modules of Java code that run in web server. Internet Explorer. Servlet. Fire Fox. Servlet.

Java EE Application Assembly & Deployment Packaging Applications, Java EE modules. Model View Controller (MVC)2 Architecture & Packaging EJB Module

Using the JBoss IDE for Eclipse

SSC - Web applications and development Introduction and Java Servlet (I)

Java4570: Session Tracking using Cookies *

Session 10. Form Dataset. Lecture Objectives

Lab session Google Application Engine - GAE. Navid Nikaein

WHITE LABELING IN PROGRESS ROLLBASE PRIVATE CLOUD

Servlets1. What are Servlets? Where are they? Their job. Servlet container. Only Http?

A Fresh Graduate s Guide to Software Development Tools and Technologies

COMP9321 Web Application Engineering

JOURNAL OF OBJECT TECHNOLOGY

Jawaharlal Nehru Engineering College

Servlets by Example. Joe Howse 7 June 2011

DEZVOLTAREA APLICATIILOR WEB CURS 7. Lect. Univ. Dr. Mihai Stancu

Welcome To PhillyJUG. 6:30-7:00 pm - Network, eat, find a seat 7:00-7:15 pm - Brief announcements 7:15-8:30 pm - Tom Janofsky's presentation

Creating a New Project with Struts 2

Contents at a Glance

&' () - #-& -#-!& 2 - % (3" 3 !!! + #%!%,)& ! "# * +,

To follow the Deitel publishing program, sign-up now for the DEITEL BUZZ ON-

Université Antonine - Baabda

Using Java servlets to generate dynamic WAP content

SDN Community Contribution

Getting started with Winstone. Minimal servlet container

Session 21. Expression Languages. Reading. Java EE 7 Chapter 9 in the Tutorial. Session 21 Expression Languages 11/7/ Robert Kelly,

Servlets and JSP (Java Server Pages)

Developing container managed persistence with JPA

V3 EJB Test One Pager

Contents. 1. Session bean concepts. 2. Stateless session beans. 3. Stateful session beans

Java Card 3 Platform. Peter Allenbach Sun Microsystems, Inc.

CE212 Web Application Programming Part 3

Servlet and JSP Review

Information systems modelling UML and service description languages

Basics of programming 3. Java Enterprise Edition

Chettinad College of Engineering and Technology CHETTINAD COLLEGE OF ENGINEERING AND TECHNOLOGY DEPARTMENT OF COMPUTER SCIENCE AND TECHNOLOGY

The Servlet Life Cycle

LearningPatterns, Inc. Courseware Student Guide

Internet Technologies 5-Dynamic Web. F. Ricci 2010/2011

Chapter 17. Web-Application Development

HTTP status codes. Setting status of an HTTPServletResponse

11.1 Introduction to Servlets

Document for Consuming Web-Service In.NET & JAVA

1A. Create a simple calculator application using servlet.

Distributed Systems 1

Stateless Session Bean

Integrating NWDS with a Non-SAP Server (JBoss AS) to Develop and Deploy Java EE Applications

Chapter 2 How to structure a web application with the MVC pattern

Web Fundamentals. Typical Current Web Architecture. Web Server HTTP interface. HTML, Images, cookies, JSON, etc.

jmaki Overview Sang Shin Java Technology Architect Sun Microsystems, Inc.

J2EE Packaging and Deployment

Seam 3. Pete Muir JBoss, a Division of Red Hat

Transcription:

Stateless -Session Bean Prepared by: A.Saleem Raja MCA.,M.Phil.,(M.Tech) Lecturer/MCA Chettinad College of Engineering and Technology-Karur E-Mail: asaleemrajasec@gmail.com Creating an Enterprise Application Project using netbean Let's get started the code itself. The first thing to do is to create the Enterprise Application Project, or simply, EAR. To do that, back to the tab Projects and right mouse click and select option New Project. On the new screen, select the Category Enterprise and then the project Enterprise Application, click next to go to the next screen. On the next screen select the Project name and the Project Location. (In my example, I am going to use the name Test), click next again. On the third screen, select the server (by default Netbeans brings Glasshfish), choose the Java Version (Java EE 5 by default), and select the EJB and Web module. Chettinad College of Engineering and Technology-Karur Page 1

Note: By default, the EJB module's name is "EAR name" plus "-ejb". The Web module's name is "EAR name" plus "-war". However you are free to change these names. When the projects are created, they will be shown on the Projects tab. Creating our first EJB component (Stateless Session Beans) On the Projects tab, expand the EJB module (in my case called "Test-ejb"). You will see some options, such as: Enterprise Beans, Configuration Files, Server Resources and so on. We will work around the Enterprise Beans, therefore right mouse click on Enterprise Beans -> New -> Session Bean. Chettinad College of Engineering and Technology-Karur Page 2

On the screen that will come up, choose the EJB Name, Package, Session Type and Interface. For my example, I am using "TestEJB","stateless","Stateless","Remote". Click finish when your fields are filled. After the Session Bean is created, you will see the class (and the interface) in the Source Packages section, as well as the Bean in the Enterprise Beans section. In the main tab, the TestEJBBean.java will be opened automatically. Chettinad College of Engineering and Technology-Karur Page 3

Our Stateless Session Bean was created into the stateless package, but now we have to implement it. Open it (if it is closed) with double click on the TestEJBBean from Enterprise Beans section (you can open the file directly from the Source Package as well). In the body of the class there is a comment " // Add business logic below. (Right-click in editor and choose // "EJB Methods > Add Business Method" or "Web Service > Add Operation")" So right mouse click in the editor and choose EJB Methods > Add Business Method. A new screen will come up. In your example, we will have only one method (called getmessage()) with String return. Hence, put in the field name getmessage and return type: String. Chettinad College of Engineering and Technology-Karur Page 4

Done, our first EJB3 Session Bean object has been created. To verify the code it has created, you can open the class TestEJBBean (implementation) and the interface TestEJBRemote (remote interface). Different than Eclipse, where we put ourself the interface and the class name, Netbeans uses the pattern Object Name plus Remote or Local. Of course you can change it yourself The last thing to do is to implement the business method we just added. It is simple, only return a string message: Hello EJB World. The code looks like below: Chettinad College of Engineering and Technology-Karur Page 5

package stateless; import javax.ejb.stateless; @Stateless public class TestEJBBean implements TestEJBRemote { public String getmessage() { return "Hello EJB World"; } } Using the Web Module as EJB Client We have already created the Web Module previously when the EAR has been created, so let s use it as EJB Client. When the Web module is created, by default Netbeans already creates a file called index.jsp in the Web Pages section. Open this file to add a call to the servlet. The code looks like 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>jsp Page</title> </head> <body> <h2>hello World!</h2> <a href="testservlet">click here to call the EJB component</a> </body> </html> As you can see, we must create a servlet called TestServlet. it will call the EJB component when the link from User Interface is clicked. Chettinad College of Engineering and Technology-Karur Page 6

To create the servlet itself, right click on Web project (in my case Test-web), New -> Servlet. On the new screen, select the Class Name and the Package. On next screen, you can keep all options and click on Finish button. The Servlet will be created and its code will appear on the Main tab. Much easier than JBoss, in Glassfish there is the annotation @EJB. When the container sees this annotation, is inject the component directly in the Servlet, Java Class, Managed Bean, whatever. (In JBoss this annotation has not been implemented yet, so you must use JNDI to call a remote component). The @EJB annotation gets inside the attribute and can be used directly in the code. The code from Servlet looks like below: package servlets; import java.io.*; import javax.ejb.ejb; import javax.servlet.*; import javax.servlet.http.*; import stateless.testejbremote; public class TestServlet extends HttpServlet { //This annotation INJECTS the TestEJBRemove object from EJB //into this attribute @EJB private TestEJBRemote testejb; @Override protected void doget(httpservletrequest request, HttpServletResponse response) throws ServletException, IOException { response.setcontenttype("text/html;charset=utf-8"); PrintWriter out = response.getwriter(); out.println("<html>"); Chettinad College of Engineering and Technology-Karur Page 7

out.println("<head>"); out.println("<title>servlet TestServlet</title>"); out.println("</head>"); out.println("<body>"); out.println(testejb.getmessage()); out.println("</body>"); out.println("</html>"); } } Look at the line #13. It has the @EJB annotation. Also, look at the body of doget method. No JNDI method was used, no code to call the EJB object was used, thus the code gets much clear than JNDI. Chettinad College of Engineering and Technology-Karur Page 8