Академический Документы
Профессиональный Документы
Культура Документы
THANJAVUR
SCHOOL OF COMPUTING
2013 - 2014
LIST OF EXPERIMENTS
1. Create a COM component in VB to do Email Id validation. 2. Create an ActiveX component in VB for Rapid Application Development. 3. Create a COM+ component for in VB. 4. Create a database application using ADODB Component in VB 5. Create a web application using ADODB Component in ASP.Net 6. Create an application using stateless session Bean in J2EE. 7. Create an application using stateful session Bean in J2EE. 8. Create an Entity Bean to use in an application using J2EE 9. Create an application using Message Driven Bean in J2EE 10. Create a Secured J2EE Application using built in Authentication Components
Suggested IDEs : Visual Studio 2005 / Visual Studio 2008, Netbeans 7.0
Create a Class Library project. Write a function which accepts Email Id as parameter. Check whether the Email Id is valid or not, if valid return true else return false. Build the project to create component (.dll file). Create a Windows application to test the component.
7. Select Build -> Build Solution or press ctrl+shift+B. Now a dll file will be created with the name GreetingCOM.dll. 8. In output window the location of the dll file will be displayed. Remember the path. 9. Now we have successfully created a component with the name GreetingCOM.dll. 10. Goto File Menu -> close solution. Creating client application to use the component: 1. Goto File Menu -> New -> Project. 2. Select Project type Visual Basic and Template Windows Application.
3. Give project name as GreetingCOMClient and click OK. 4. In the solution explorer right click project name GreetingCOMClient and select Add Reference. 5. In Add Reference window, select the Browse tab and choose the dll file created above. 6. In the design form, drag and drop one button control. 7. Double click the button. In the coding window type the following code
Dim a As New GreetingCOM.MyGreeting a.welcome()
8. Press F5 to execute the program. Click the button, Welcome to SASTRA message will be displayed. 9. Based on this example develop the exercise.
Result: COM Component and a client application to test the component are developed and executed.
Prelab :
Data Manipulation programs in VB Greeting Message Generation in VB
Write the coding to calculate the EMI amount. Build the project to generate ActiveX control. Create a Windows application to test the ActiveX
component. Prerequisite: Basic Programming Knowledge in VB, ActiveX components Example: The following example will explain how to create an ActiveX component to add two numbers. Creating ActiveX Component: 1. 2. 3. 4. 5. Open Visual Studio 2005. Go to File Menu -> New -> Project. Select Project Type Visual Basic and Template Windows Control Library. Give project name as GreetingActiveX and click OK. In the design window drag and drop the components and design the form like below.
6. Double click the button and in the coding window write the following coding. Textbox3.Text=val(textbox1.Text)+val(textbox2.Text) Select Build -> 7. Build Solution or press ctrl+shift+B. Now a .dll file will be created with the name GreetingActiveX.dll. 8. In output window the location of the .dll file will be displayed. Remember the path. 9. Now we have successfully created an ActiveX component with the name GreetingActiveX.dll. 10. Goto File Menu -> close solution. Creating client application to use the component: 1. 2. 3. 4. 5. Goto File Menu -> New -> Project. Select Project type Visual Basic and Template Windows Application. Give project name as GreetingActiveXClient and click OK. In the tool box window right click, and select Choose Items. In Choose Items window, select the .Net Components tab, click browse button and choose the .dll file created above. 6. Now the ActiveX control will be displayed in the tool box. Drag and drop the control in the design form. 7. Press F5 to execute the program. Give the values and click the button to get result. 8. Based on this example develop the exercise.
Result: ActiveX Component and a client application to test the component are developed and executed.
Prerequisite: Knowledge about windows component service, COM and COM+. Result: COM+ Component and a client application to test the component are developed and executed.
Do the necessary coding to connect to database and manipulate the dataPrerequisite: Knowledge in MS Access database creation, SQL, cursor location and lock type. Result: Database application using ADODB component created and executed successfully.
Ex. 5 - Create a web based database application in ASP.Net using ADODB Component
Objectives: Create a web based database application for Contacts Management in ASP.Net using ADODB component. Procedure: Design a web page like the below Create a Web site in ASP.net Create a database in MS Access to store the details
Prerequisite: Knowledge in creating web pages, web servers and web application. Result: Database web application using ADODB component created and executed successfully.
Example: The following example will explain how to create a simple stateless session bean. Creating Remote Interface: 1. File -> New Project -> Categories = Java -> Projects = Java Class Library, Click Next 2. Give Project Name as Ex6RemoteInterface, Click Finish 3. Save the project. Creating Statess Session Bean: 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. File -> New Project -> Categories = JavaEE -> Projects = Enterprise Application Give Project Name as Ex6, Click Next. Uncheck Create Web Application Module check box, click Finish. Right click EJB Module (Ex6-ejb) -> New -> Session Bean Give EJB Name as MySession6, package name as ejb Select session type as Stateless, select Remote check box. In the drop down list select the Remote interface name(Ex6RemoteInterface) that we created as first step of this Ex. Click Finish In the coding window declare one String variable String message; Right click in the coding window select insert code, select Add Business Method Give Method name as getMessage, return type as String, click ok. Replace return null by return message Again Right click in the coding window select insert code, select Add Business Method Give Method name as setMessage. Click Add button and give Parameter Name as message click ok. Add the following line in the setMessage method body. this.message=message;
Creating Client 1: 1. File -> New Project -> Categories = JavaEE -> Projects = Enterprise Application Client
10
2. Click Next. Give project name as Ex6Client1, Click Next, Click Finish. 3. Right click in the main method, select insert code, select Call Enterprise Bean, Expand the ejb module (Ex6-ejb), select session bean(MySession6), click ok. 4. Add the following code in main method. mySession6.setMessage("This is Session Bean..."); System.out.println("Client1 Message : "+mySession6.getMessage()); 5. Save the project. Creating Client 2: 1. File -> New Project -> Categories = JavaEE -> Projects = Enterprise Application Client 2. Click Next. Give project name as Ex6Client2, Click Next, Click Finish. 3. Right click in the main method, select insert code, select Call Enterprise Bean, Expand the ejb module (Ex6-ejb), select session bean(MySession6), click ok. 4. Add the following code in main method. System.out.println("Client2 Message : "+mySession6.getMessage()); 5. Save the project. Running the project: 1. Right click Enterprise Application Project and select deploy. 2. Right click client 1 project and select Run a. Output : Client1 Message : This is Session Bean... 3. Right click client 2 project and select Run a. Output : Client2 Message : This is Session Bean...
Prerequisite: Knowledge in Java, Java Beans and web. Result: Stateless Session bean and client applications to test the bean are developed and executed.
11
Example: The following example will explain how to create a simple stateful session bean. Creating Remote Interface: 1. File -> New Project -> Categories = Java -> Projects = Java Class Library, Click Next 2. Give Project Name as Ex7RemoteInterface, Click Finish 3. Save the project. Creating Statess Session Bean: 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. File -> New Project -> Categories = JavaEE -> Projects = Enterprise Application Give Project Name as Ex7, Click Next. Uncheck Create Web Application Module check box, click Finish. Right click EJB Module (Ex7-ejb) -> New -> Session Bean Give EJB Name as MySession7, package name as ejb Select session type as Stateful, select Remote check box. In the drop down list select the Remote interface name(Ex7RemoteInterface) that we created as first step of this Ex. Click Finish In the coding window declare one String variable String message; Right click in the coding window select insert code, select Add Business Method Give Method name as getMessage, return type as String, click ok. Replace return null by return message Again Right click in the coding window select insert code, select Add Business Method Give Method name as setMessage. Click Add button and give Parameter Name as message click ok. Add the following line in the setMessage method body. this.message=message;
Creating Client 1: 1. File -> New Project -> Categories = JavaEE -> Projects = Enterprise Application Client
12
2. Click Next. Give project name as Ex7Client1, Click Next, Click Finish. 3. Right click in the main method, select insert code, select Call Enterprise Bean, Expand the ejb module (Ex7-ejb), select session bean(MySession7), click ok. 4. Add the following code in main method. mySession6.setMessage("This is Session Bean..."); System.out.println("Client1 Message : "+mySession7.getMessage()); 5. Save the project. Creating Client 2: 1. File -> New Project -> Categories = JavaEE -> Projects = Enterprise Application Client 2. Click Next. Give project name as Ex7Client2, Click Next, Click Finish. 3. Right click in the main method, select insert code, select Call Enterprise Bean, Expand the ejb module (Ex7-ejb), select session bean(MySession7), click ok. 4. Add the following code in main method. System.out.println("Client2 Message : "+mySession7.getMessage()); 5. Save the project. Running the project: 1. Right click Enterprise Application Project and select deploy. 2. Right click client 1 project and select Run Output : Client1 Message : This is Session Bean... 3. Right click client 2 project and select Run Output : Client2 Message : null
Prerequisite: Knowledge about Java, Java Beans, web Result: Statelful Session bean and client applications to test the bean are developed and executed.
13
1. Goto Services Tab -> Expand Databases -> right click on 'Java DB' -> select 'Create Database' Database Name : BooksDemoDB username and password set to books 2. The creation will also start the database, if you go back to the services tab, you will be able to open and view the database.
14
Project creation 1. Goto File Menu -> New Project -> Java EE -> Enterprise Application Project Name : BooksDemo 2. No need to change any-thing else. Click next and finish. This will create three projects - BooksDemo 'containing' BooksDemo-ejb and BooksDemo-war Persistence Unit creation 1. Right click 'BooksDemo-ejb' -> new -> other -> category Persistance -> File Type Persistence Unit -> Next 2. Dont change default name (BooksDemo-ejbPU) 3. Datasource -> select 'New Data Source' at the bottom of the dropdown. Then select the BooksDemoDB database you created earlier and make the JNDI name 'jdbc/booksDemo', leave the rest as default and finish. Create the Entity Bean 1. Right click 'BooksDemo-ejb', -> new -> other -> Persistance -> Entity Class > Next Classname : 'Book', package 'com.book.model' 2. Dont change any-thing else, click finish. 3. In the Book class, in member variable declaration section, right click and select insert code -> select Add Property -> Give property name as title and click ok. 4. The same way add one more property called author Create the SessionBean 1. Right click 'BooksDemo-ejb', -> new -> other ->Java EE -> Session Bean -> Next Name: 'BookService', Package: 'com.example.book.service', No need to change any-thing else, click finish. Now we are going to create two 'business' methods: getAll - > retrieving all books from database addBook -> add a book to the database. <ALT>INSERT -> Add Business Method with name 'getAll' with return type 'List<Book>' <ALT>INSERT -> Add Business Method with name 'addBook' and add a parameters called 'book' of type 'Book'. Add the following import statements in the import section of the class.
2. 3.
4. 5. 6.
15
import java.util.List; import com.book.model.Book; 7. Next we need a handle on the persistance unit: Right click in the coding window -> select Use Entity Manager then the following code will be added automatically. @PersistenceContext(unitName = "BooksDemo-ejbPU") private EntityManager em; 8. Now we need to use the EntityManager to store and retrieve the books: Modify the return statement in getAll method by the following line. return em.createQuery("select b from Book b").getResultList(); 9. Add the following line in addBook method. em.persist(book); The Web Tier 1. First we need to create a servlet to receive the values from the index.jsp file and pass the values to the session bean to store it in the database. 2. Right click 'BooksDemo-war' (note its 'war' not ejb) -> new -> other -> Web -> Servlet -> Next Class Name: 'BookAdd' package: 'com.book.web.action' the rest is default, click 'next' and 'finish' 3. Add the SessionBean by Right click in the coding window ->Insert Code -> select Call Enterprise Bean and select the BookService bean under 'BooksDemo-ejb'and OK. 4. Edit the 'processRequest' method, replace the try block code with the following code. Book book = new Book(); book.setTitle(request.getParameter("title")); book.setAuthor(request.getParameter("author")); bookService.addBook(book); List<Book> listOfBooks=bookService.getAll(); out.println("<html>"); out.println("<head>"); out.println("<title>Servlet BookAdd</title>"); out.println("</head>"); out.println("<body>");
out.println("<h1>List of Books</h1>"); out.println("<table border=1>");
16
for(Book b:listOfBooks){ out.println("<tr><td>"+b.getTitle()+"</td>"); out.println("<td>"+b.getAuthor()+"</td></tr>"); } out.println("</table>"); out.println("</body>"); out.println("</html>"); 5. The index.jsp We are going to edit the index.jsp. Under 'Web Pages' in the 'BooksDemo-war' project you will find the index.jsp page, open it. Replace '<h1>Hello World!</h1>': by the following code. <h1>Book Store</h1> Add Book Details<br/> <form method="post" action="BookAdd"> Title : <input type="text" name="title"></input></br> Author : <input type="text" name="author"></input></br> <button type="submit">add</button> </form>
Save it. Right click BooksDemo Project and select Run. Give input values and click the add button. Input values will be added to the database and displayed in the next page. You can see the inserted values in your database also(Service -> Data Store - > Java DB -> BookDemoDB->Tables right click and select view Data)
Prerequisite: Knowledge about Java, Java Beans, web Result: Entity bean and client applications to test the bean are developed and executed.
17
Example: The following example will explain how to create a simple message driven bean. Creating Message Driven Bean: 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. File -> New Project -> select Category = JavaEE and Project = EJB Module Click Next Enter the Project Name as Ex9MDB -> Click Next -> Click Finish In Projects window right click on your project Choose New -> Message Driven Bean Enter EJB Name HelloBean and package hello Click the Add button to add project destination In the window give destination name hello and select destination type Queue, click ok. Click Finish. In the coding window add the following code in onMessage method. try { System.out.println(message.getStringProperty("msg"));
} catch (JMSException ex) { Logger.getLogger(MessageBean.class.getName()).log(Level.SEVERE, null, ex); } 11. Save the project. Creating Client Application to Access the Message Driven Bean: 1. File -> New Project -> Category = JavaEE and Project = Enterprise Application Client 2. Click Next 3. Enter the Project Name as Ex9MEBClient -> Click Next -> Click Finish 4. In the coding window before main method copy paste the following code @Resource(mappedName="jms/helloFactory") private static ConnectionFactory connectionFactory; @Resource(mappedName="jms/hello") private static Queue queue; 5. Add the following code to the Main method Connection connection = connectionFactory.createConnection(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
18
6.
7. 8. 9.
MessageProducer producer = session.createProducer(queue); Message message = session.createTextMessage(); message.setStringProperty("msg", "Hello World!"); System.out.println("Message Sent is : " + message.getStringProperty("msg")); producer.send(message); System.exit(0); Add the following header files import javax.annotation.Resource; import javax.jms.Connection; import javax.jms.ConnectionFactory; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageProducer; import javax.jms.Queue; import javax.jms.Session; Save the project Deploy EJB Module project Run the Client project
Output : In Run window message sent will be displayed In GlassFish server log message received will be displayed
Prerequisite: Knowledge about Java, Java Beans, web Result: Message Driven Bean and client applications to test the bean are developed and executed.
19
Result: Secured web application created and deployed using authentication components.
20
ADDITIONAL EXERCISES
1. Create a web application and make use of a JavaBeans to check the validity of users credit card number. 2. Develop a E-commerce application using j2ee under net beans 3. Create a java web service to validate the identity of a customer 4. Develop Lists-Based Application for a Music Store http://www.functionx.com/vbnet/xml/musicstore.htm 5. Create a web application using asp.net to update and view the results of college students
21