0% found this document useful (0 votes)
4 views6 pages

Librarysessionbeanremote: Java - Util.List

The document describes a Java EE application with a stateless EJB for managing a library system. It includes an interface for adding and retrieving books, an implementation of that interface, and a client class for interacting with the EJB. The client allows users to add books and view the list of books stored in the library system.

Uploaded by

Shivam Wagh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views6 pages

Librarysessionbeanremote: Java - Util.List

The document describes a Java EE application with a stateless EJB for managing a library system. It includes an interface for adding and retrieving books, an implementation of that interface, and a client class for interacting with the EJB. The client allows users to add books and view the list of books stored in the library system.

Uploaded by

Shivam Wagh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

}

LibrarySessionBeanRemote

package com.gaurav.stateless;

import java.util.List;
import javax.ejb. Remote;

@Remote
public interface LibrarySessionBeanRemote {
void addBook (String bookName);
List getBooks ();
}

LibrarySession Bean

package com.gaurav.stateless;

import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;

@Stateless
public class LibrarySessionBean implements LibrarySessionBeanRemote
{

List<String> bookShelf;

public LibrarySessionBean() {

}
bookShelf = new ArrayList<String>();

public void addBook (String bookName) {


bookShelf.add (bookName);
}

public List<String> getBooks () {


return bookShelf;

EJBTester.java

package com. tutorialspoint.test;

import com. tutorialspoint.stateless. LibrarySessionBeanRemote;


import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException; import
java.io.InputStreamReader; import
java.util.List;
import java.util.Properties;
import javax.naming. InitialContext;
import javax.naming.NamingException;

public class EJBTester {


BufferedReader brConsoleReader = null;

Properties props;
InitialContext ctx;

}
props = new Properties();
try {
props.load(new FileInputStream("jndi.properties"));
} catch (IOException ex) {

}
ex.printStackTrace();

try {
ctx = new InitialContext (props);
} catch (NamingException ex) {

}
ex.printStackTrace();

brConsoleReader =

new BufferedReader (new InputStreamReader (System.in));

public static void main(String[] args) {


EJBTester ejbTester
=

new EJBTester ();

ejbTester.testStatelessEjb();
}
private void showGUI () {
System.out.println("******
);

*");
System.out.println("Welcome to Book Store");
System.out.println(
System.out.print ("Options \nl. Add Book\n2. Exit \nEnter
Choice: ");
}
private void testStatelessEjb() {
try {
int choice = 1;
LibrarySessionBeanRemote libraryBean

(LibrarySessionBeanRemote) ctx.lookup ("LibrarySessionBean/remote");


while (choice = 2) {

}
String bookName;
showGUI();
String strChoice = brConsoleReader.readLine(); choice =
Integer.parseInt(strChoice);
if (choice
==

1) {
System.out.print ("Enter book name: "); bookName =
brConsoleReader.readLine(); libraryBean. addBook
(bookName);
}else if (choice == 2) {
}
break;

List<String> booksList = libraryBean.getBooks ();


System.out.println("Book(s) entered so far: +
booksList.size());
for (int i = 0; i < booksList.size(); ++i) {
System.out.println((i+1)+". + booksList.get(i));
}

LibrarySessionBeanRemote libraryBean1

(LibrarySessionBeanRemote) ctx.lookup ("LibrarySessionBean/remote");


List<String> booksList1 = libraryBeanl.getBooks ();
System.out.println(

object***");
***Using second lookup to get library stateless

System.out.println(
"Book(s) entered so far: " + booksListl.size()); for (int i = 0;
i < booksListl.size(); ++i) {

}
System.out.println((i+1)+". " + booksListl.get(i));

} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
} finally {
try {
if (brConsoleReader !=null) {

}
brConsoleReader.close();

} catch (IOException ex) {


System.out.println(ex.getMessage());

}
}
}
}

Run Client to Access EJB


output in Netbeans console.
run:

*** **
**

Welcome to Book Store

Options
1. Add Book
2. Exit

Enter Choice: 1
***

Enter book name: Learn Java


**

Welcome to Book Store

Options
1. Add Book
2. Exit
Enter Choice:
2

Book (s) entered so far: 1


1. Learn Java

***Using second lookup to get library stateless object***


Book (s) entered so far: 0
BUILD SUCCESSFUL (total time: 13 seconds)

You might also like