Download 3ca1437-ajt-lop

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Nirma University
Institute of Technology
MCA-IV
3CA1437 Advanced Java Technologies (Elective - II)
List of Practical
Sr
.
1
2
3
Practical
Develop the following client-server applications using stream & datagram
sockets.
a. An Echo application
b. Factorial application
c. A DateTime application (*)
d. A Prime number application (*)
e. A file transfer application (*)
a. Using HttpSession, write a CounterServlet which counts and displays the
number of times :
(i) a particular visitor has visited the CounterServlet.
(ii) total number of times the CounterServlet has been visited by all users.
b. Develop an application showing printing of all parameters received by a
servlet. Create an HTML page containing various HTML controls such as
radio button, checkboxes, multi-select list box etc. The page, when
submitted, should send all values of these controls to a ParametersServlet.
The ParameterServlet should print all the received parameters.
c. Perform the above CounterServlet program using cookies. (*)
d. Develop a DatabaseServlet that performs various table row operations
such as – insert row, delete row, update row and search records. (*)
Develop the following RMI applications :
Relate
d CLO
CLO1
Mark
s
10
Hrs
CLO3
10
4
CLO2
10
4
4
1
4
5
6
7
a. A StringUpperCase application – the RMI client sends a String message to
RMI server. The server converts the received message to upper case, and
returns the resultant message back to client.
b. A Palindrome application – the RMI client sends a number to RMI server.
The server decides whether the received number is palindrome or not and
returns the decision (boolean) back to client.
a. Write a Filter which is called whenever a LoginServlet is called. The
Filter should display the client’s IP address in the Messagedialog Box,
and then should forward the request to the LoginServlet.
b. Write a Filter that converts output generated by some servlet into upper
case and delivers it to client. (*)
Write an appropriate event listener class, which can handle an event which is
raised whenever an HttpSession object is created in the SampleServlet Class.
This Listener should handle the event by displaying the "session Created
message and the Session ID value in the Message Dialog Box (write one
Listener Class and SampleServlet Servlet Class)
Develop a login.jsp page which takes UserId and password from user using
suitable HTML controls and submits the controls’ values to
CheckLoginServlet.
The CheckLoginServlet validates the user. If the user is valid, the servlet
forwards request to welcome.jsp page. Otherwise, the login.jsp page (with
appropriate “invalid UserId/password” error message) is shown again to
recollect the UserId & password.
Develop a Javabean named Student with appropriate data members, member
functions and constructors.
Develop a JSP page source.jsp for the following tasks:
(i) Use the above developed Student java bean.
(ii) Set properties of the java bean.
CLO3
10
2
CLO3
10
2
CLO3
10
4
CLO4
10
2
2
(iii) Pass it to another JSP page destination.jsp
The destination.jsp should receive the javabean which is sent by source.jsp,
and should get and print all the data members of javabean.
Note: Use action alements: <jsp:useBean>, <jsp:getProperty>,
<jsp:setProperty>, <jsp:param>, <jsp:forward> etc.
8
(i) Develop stateless/stateful session bean say CalculatorBean representing
general calculator. Also develop suitable servlet to test the CalculatorBean.
(ii) Develop a stateless session bean that takes a word and returns it spelled
backwards. Also develop suitable jsp to test your bean.
(iii) Write a stateful bean that takes one word at a time and appends it o
the previous words received to make a sentence. Return the entire
sentence each time a word is added. (*)
9
Create an entity bean for a demo Audio CD collection application named
CompactDiscTitle. Attributes should be name (String, and it will be the
primary key) and price (double). Write a stateless session bean that allows
you to get, add, update and remove a CD title. Write a client application to
test the beans.
10 Develop a StringWebService. The service should provide the following web
methods:
(i) int stringLength(String message)
(ii) String convertStringToUpperCase(String message)
(iii) boolean checkPalindrome(String message)
Also develop a Java client to test the web methods of above StringWebService.
11 Write a servlet class with a synchronized doGet() or doPost() method. Put a
sleep() method in the class. Open several browser windows and use them to
submit several simultaneous requests to the servlet. Record the response
time for each request. Now remove the synchronization keyword from the
doGet() and doPost() method. Submit the same number of simultaneous
CLO4
10
4
CLO4
10
4
CLO5
10
4
CLO3
4
3
requests, and compare the response time of the unsynchronized servlet to
that of the synchronized servlet. (*)
12 A DatabaseRMIApplication – the RMI client sends an object of Student class
to RMI server. The server extracts the received Student object and inserts an
appropriate row into Student table in MySQL database. (*)
13 Develop chat application using tcp/udp. (*)
TOTAL
*Extra Practicals
CLO2
4
CLO2
4
40
4
Date of Performance:
Date of Submission:
Exp. No.: 1
Title: Develop the following client-server applications using stream & datagram sockets.
a. An Echo application
b. Factorial application
c. A DateTime application (*)
d. A Prime number application (*)
e. A file transfer application (*)
______________________________________________________________________________
Objectives: To apply the knowledge of Socket Programming to develop various applications.
Learning Outcomes:
After this practical, students will able to:
 Understand how socket and server socket programming works
 Develop client/server applications and TCP/IP socket programming
Pre-Lab questions:
 Difference between Socket and Server Socket
 Difference between TCP/IP and UDP
Theory Involved
Socket class
A socket is simply an endpoint for communications between the machines. The Socket class can be used to
create a socket.
Important methods
Method
Description
1) public InputStream getInputStream()
returns the InputStream attached with this socket.
2) public OutputStream getOutputStream()
returns the OutputStream attached with this socket.
5
3) public synchronized void close()
closes this socket
ServerSocket class
The ServerSocket class can be used to create a server socket. This object is used to establish communication
with the clients.
Important methods
Method
Description
1) public Socket accept()
2) public
close()
synchronized
returns the socket and establish a connection between server and
client.
void
closes the server socket.
Procedure to do Socket Programming
When programming a client, you must follow these four steps:
 Open a socket.
 Open an input and output stream to the socket.
 Read from and write to the socket according to the server's protocol.
 Clean up.
Post Lab Quiz:
(i)
How datagram socket differed from other socket.
Critical Findings and Conclusion:
Remarks and Signature of Faculty with date:
6
Date of Performance:
Date of Submission:
Exp. No.: 2
Title:
a) Using HttpSession, write a CounterServlet which counts and displays the number of times :
(i) a particular visitor has visited the CounterServlet.
(ii) total number of times the CounterServlet has been visited by all users.
b) Develop an application showing printing of all parameters received by a servlet. Create an HTML page
containing various HTML controls such as radio button, checkboxes, multi-select list box etc. The page,
when submitted, should send all values of these controls to a ParametersServlet. The ParameterServlet
should print all the received parameters.
c) Perform the above CounterServlet program using cookies. (*)
d) Develop a DatabaseServlet that performs various table row operations such as – insert row, delete row,
update row and search records. (*)
______________________________________________________________________________
Objectives: To create servlet based various applications.
Learning Outcomes:
After this practical, students will able to:
 Understand the concept of servlet
 Develop server side programs in the form of servlets.
Pre-Lab questions:
 What is Servlet and What are the advantages of Servlet?
Theory Involved
Servlets provide a component-based, platform-independent method for building Web-based applications,
without the performance limitations of CGI programs. Servlets have access to the entire family of Java APIs,
including the JDBC API to access enterprise databases.
7
What are Servlets?
Java Servlets are programs that run on a Web or Application server and act as a middle layer between a request
coming from a Web browser or other HTTP client and databases or applications on the HTTP server.
Using Servlets, you can collect input from users through web page forms, present records from a database or
another source, and create web pages dynamically.
Servlets Tasks:
Servlets perform the following major tasks:
 Read the explicit data sent by the clients (browsers). This includes an HTML form on a Web page or it
could also come from an applet or a custom HTTP client program.
 Read the implicit HTTP request data sent by the clients (browsers). This includes cookies, media types
and compression schemes the browser understands, and so forth.
 Process the data and generate the results. This process may require talking to a database, executing an
RMI or CORBA call, invoking a Web service, or computing the response directly.
 Send the explicit data (i.e., the document) to the clients (browsers). This document can be sent in a
variety of formats, including text (HTML or XML), binary (GIF images), Excel, etc.
 Send the implicit HTTP response to the clients (browsers). This includes telling the browsers or other
clients what type of document is being returned (e.g., HTML), setting cookies and caching parameters,
and other such tasks.
 Servlets
are
Java
classes
which
service
HTTP
requests
and
implement
the javax.servlet.Servlet interface. Web application developers typically write servlets that extend
javax.servlet.http.HttpServlet, an abstract class that implements the Servlet interface and is specially
designed to handle HTTP requests.
Steps of Doing Practical
Sample Code for Hello World:
Following is the sample source code structure of a servlet example to write Hello World:



// Import required java libraries
import java.io.*;
import javax.servlet.*;
8

import javax.servlet.http.*;



// Extend HttpServlet class
public class HelloWorld extends HttpServlet {


private String message;






public void init() throws ServletException
{
// Do required initialization
message = "Hello World";
}







public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
// Set response content type
response.setContentType("text/html");

// Actual logic goes here.
PrintWriter out = response.getWriter();
out.println("<h1>" + message + "</h1>");




}




public void destroy()
{
// do nothing.
9
}


}
Post Lab Quiz:
1. When init() method of servlet gets called?
2. When service() method of servlet gets called?
3. When doGet() method of servlet to be called?
4. When doPost() method of servlet to be called?
5. When destroy() method of servlet gets called?
6. For what purpose init() method of a servlet is used?
7. For what purpose destroy() method of a servlet is used?
8. For what purpose doGet() method of a servlet is used?
Critical Findings and Conclusion:
Remarks and Signature of Faculty with date:
10
Date of Performance:
Date of Submission:
Exp. No.: 3
Title:
Develop the following RMI applications :
a) A StringUpperCase application – the RMI client sends a String message to RMI server. The server
converts the received message to upper case, and returns the resultant message back to client.
b) A Palindrome application – the RMI client sends a number to RMI server. The server decides whether
the received number is palindrome or not and returns the decision (boolean) back to client.
______________________________________________________________________________
Objectives: Creating A RMI based application
Learning Outcomes:
After this practical, students will able to:
 Develop distributed applications using RMI.
Pre-Lab questions:
 What is RMI?
 What is the difference between stub and skeleton?
Theory Involved
The RMI (Remote Method Invocation) is an API that provides a mechanism to create distributed application in
java. The RMI allows an object to invoke methods on an object running in another JVM.
The RMI provides remote communication between the applications using two objects stub and skeleton.
Understanding stub and skeleton
RMI uses stub and skeleton object for communication with the remote object.
A remote object is an object whose method can be invoked from another JVM. Let's understand the stub and
skeleton objects:
stub
11
The stub is an object, acts as a gateway for the client side. All the outgoing requests are routed through it. It
resides at the client side and represents the remote object. When the caller invokes method on the stub
object, it does the following tasks:
1. It initiates a connection with remote Virtual Machine (JVM),
2. It writes and transmits (marshals) the parameters to the remote Virtual Machine (JVM),
3. It waits for the result
4. It reads (unmarshals) the return value or exception, and
5. It finally, returns the value to the caller.
skeleton
The skeleton is an object, acts as a gateway for the server side object. All the incoming requests are routed
through it. When the skeleton receives the incoming request, it does the following tasks:
1. It reads the parameter for the remote method
2. It invokes the method on the actual remote object, and
3. It writes and transmits (marshals) the result to the caller.
12
In the Java 2 SDK, an stub protocol was introduced that eliminates the need for skeletons.
Procedure to do Practical
Steps to write the RMI program
The is given the 6 steps to write the RMI program.
1. Create the remote interface
2. Provide the implementation of the remote interface
3. Compile the implementation class and create the stub and skeleton objects using the rmic tool
4. Start the registry service by rmiregistry tool
5. Create and start the remote application
6. Create and start the client application
Post Lab Quiz:
 How to start the registry service?
13

How stub and skeleton are created in RMI?
Critical Findings and Conclusion:
Remarks and Signature of Faculty with date:
14
Date of Performance:
Date of Submission:
Exp. No.: 4
Title:
a. Write a Filter which is called whenever a LoginServlet is called. The Filter should display the client’s IP
address in the Messagedialog Box, and then should forward the request to the LoginServlet.
b. Write a Filter that converts output generated by some servlet into upper case and delivers it to client. (*)
______________________________________________________________________________
Objectives: Manipulate Filters in and its use in Servlet.
Learning Outcomes:
After this practical, students will able to:
 Develop server side programs in the form of servlets.
 Apply filters in servlet
Pre-Lab questions:
 How to define filter.
 What is the usage of filter.
 What are the advantages of filer.
Theory Involved
A filter is an object that is invoked at the preprocessing and postprocessing of a request.
It is mainly used to perform filtering tasks such as conversion, logging, compression, encryption and
decryption, input validation etc.
The servlet filter is pluggable, i.e. its entry is defined in the web.xml file, if we remove the entry of filter from
the web.xml file, filter will be removed automatically and we don't need to change the servlet.
So maintenance cost will be less.
15
Note: Unlike Servlet, One filter doesn't have dependency on another filter.
Usage of Filter
 recording all incoming requests
 logs the IP addresses of the computers from which the requests originate
 conversion
 data compression
 encryption and decryption
 input validation etc.
Advantage of Filter
1. Filter is pluggable.
2. One filter don't have dependency onto another resource.
3. Less Maintenance
Filter API
Like servlet filter have its own API. The javax.servlet package contains the three interfaces of Filter API.
1. Filter
2. FilterChain
16
3. FilterConfig
1) Filter interface
For creating any filter, you must implement the Filter interface. Filter interface provides the life cycle methods
for a filter.
Method
Description
public void init(FilterConfig config)
init() method is invoked only once. It is used to initialize
the filter.
public void doFilter(HttpServletRequest
request,HttpServletResponse response, FilterChain
chain)
doFilter() method is invoked every time when user
request to any resource, to which the filter is mapped.It
is used to perform filtering tasks.
public void destroy()
This is invoked only once when filter is taken out of the
service.
2) FilterChain interface
The object of FilterChain is responsible to invoke the next filter or resource in the chain.This object is passed
in the doFilter method of Filter interface.The FilterChain interface contains only one method:
1. public void doFilter(HttpServletRequest request, HttpServletResponse response): it passes the
control to the next filter or resource.
Procedure to do Practical
How to define Filter
We can define filter same as servlet. Let's see the elements of filter and filter-mapping.
1. <web-app>
2.
3. <filter>
4. <filter-name>...</filter-name>
5. <filter-class>...</filter-class>
6. </filter>
17
7.
8. <filter-mapping>
9. <filter-name>...</filter-name>
10.
<url-pattern>...</url-pattern>
11.
</filter-mapping>
12.
13.
</web-app>
For mapping filter we can use, either url-pattern or servlet-name. The url-pattern elements has an advantage
over servlet-name element i.e. it can be applied on servlet, JSP or HTML.
Simple Example of Filter
In this example, we are simply displaying information that filter is invoked automatically after the post
processing of the request.
index.html
1. <a href="servlet1">click here</a>
MyFilter.java
1. import java.io.IOException;
2. import java.io.PrintWriter;
3.
4. import javax.servlet.*;
5.
6. public class MyFilter implements Filter{
7.
8. public void init(FilterConfig arg0) throws ServletException {}
9.
10.
public void doFilter(ServletRequest req, ServletResponse resp,
11.
FilterChain chain) throws IOException, ServletException {
12.
13.
PrintWriter out=resp.getWriter();
18
14.
out.print("filter is invoked before");
15.
16.
chain.doFilter(req, resp);//sends request to next resource
17.
18.
out.print("filter is invoked after");
19.
}
20.
public void destroy() {}
21.
}
HelloServlet.java
1. import java.io.IOException;
2. import java.io.PrintWriter;
3.
4. import javax.servlet.ServletException;
5. import javax.servlet.http.*;
6.
7. public class HelloServlet extends HttpServlet {
8.
public void doGet(HttpServletRequest request, HttpServletResponse response)
9.
throws ServletException, IOException {
10.
11.
response.setContentType("text/html");
12.
PrintWriter out = response.getWriter();
13.
14.
out.print("<br>welcome to servlet<br>");
15.
16.
}
17.
18.
}
web.xml
19
For defining the filter, filter element of web-app must be defined just like servlet.
1. <web-app>
2.
3. <servlet>
4. <servlet-name>s1</servlet-name>
5. <servlet-class>HelloServlet</servlet-class>
6. </servlet>
7.
8. <servlet-mapping>
9. <servlet-name>s1</servlet-name>
10.
<url-pattern>/servlet1</url-pattern>
11.
</servlet-mapping>
12.
13.
<filter>
14.
<filter-name>f1</filter-name>
15.
<filter-class>MyFilter</filter-class>
16.
</filter>
17.
18.
<filter-mapping>
19.
<filter-name>f1</filter-name>
20.
<url-pattern>/servlet1</url-pattern>
21.
</filter-mapping>
22.
23.
24.
</web-app>
Post Lab Quiz:
 List the elements of filter and filter mapping class.
20
 How filters are invoked and in which sequence.
Critical Findings and Conclusion:
Remarks and Signature of Faculty with date:
21
Date of Performance:
Date of Submission:
Exp. No.: 5
Title:
Write an appropriate event listener class, which can handle an event which is raised whenever an HttpSession
object is created in the SampleServlet Class. This Listener should handle the event by displaying the "session
Created message and the Session ID value in the Message Dialog Box (write one Listener Class and
SampleServlet Servlet Class)
______________________________________________________________________________
Objectives: To apply the knowledge of servlet and use it for event handling.
Learning Outcomes:
After this practical, students will able to:
 What an event listener class is.
 How events are handled.
Pre-Lab questions:
 Which are the different request level events?
 Which are the different session level events?
Theory Involved
Event Categories and Listener Interfaces
There are two levels of servlet events:
 Servlet context-level (application-level) event
This event involves resources or state held at the level of the application servlet context object.
 Session-level event
This event involves resources or state associated with the series of requests from a single user session;
that is, associated with the HTTP session object.
Each of these two levels has two event categories:
 Lifecycle changes
 Attribute changes
22
Types of Servlet Events
Request Level events: There are two event listeners for request level events
ServletRequestListener interface is implemented to notify the request coming in scope and going out of
scope for a servlet.
ServletRequestAttributeListener interface is implemented to notify the changes (addition, replacement or
removal) in the request attributes.
Servlet Context Level events:
There are two event listeners for Servlet Context level events
ServletContextListener interface is implemented to notify the initialization or destruction of the Servlet.
ServletContextAttributeListener interface is implemented to notify the changes (addition, replacement or
removal) in the Servlet Context attributes
Servlet Session Level events:
The Servlet session level events refer to events that are used to maintain the client’s session. There are four
event listeners for Servlet Session level events
HttpSessionListener interface is implemented to notify the initialization or destruction of the Http Session.
See the HttpSessionListener example
HttpSessionActivationListener interface is implemented to notify when a sessions object migrates from one
VM to another. The HttpSessionActivationListener only has any relevance when a session is part of a web
application in a distributed environment. When a session is activated, notification that the session has just
been activated will be received by the SessionDidActivate method and when the session is about to be
destroyed, the sessionWillPassivate method is invoked. When the session migrates between servers,
the sessionWillPassivate is called and once it is successfully moved, the sessionDidActivate will be called. It is
interesting to note that the sesssion is not yet ready for service at the time the sessionDidActivate method is
called. See example for implementing HttpSessionActivationListener
HttpSessionAttributeListener interface is implemented to notify the changes (addition, replacement or
removal) in the HttpSession attributes
Procedure to do Practical
23
Typical Event Listener Scenario
Consider a Web application comprising servlets that access a database. A typical use of the event listener
mechanism would be to create a servlet context lifecycle event listener to manage the database connection.
This listener may function as follows:
1. The listener is notified of application startup.
2. The application logs in to the database and stores the connection object in the servlet context.
3. Servlets use the database connection to perform SQL operations.
4. The listener is notified of imminent application shutdown (shutdown of the Web server or removal of the
application from the Web server).
5. Prior to application shutdown, the listener closes the database connection.
Post Lab Quiz:
 Why do we have servlet listener
Critical Findings and Conclusion:
Remarks and Signature of Faculty with date:
24
Date of Performance:
Date of Submission:
Exp. No.: 6
Title:
Develop a login.jsp page which takes UserId and password from user using suitable HTML controls and
submits the controls’ values to CheckLoginServlet.
The CheckLoginServlet validates the user. If the user is valid, the servlet forwards request to welcome.jsp
page. Otherwise, the login.jsp page (with appropriate “invalid UserId/password” error message) is shown
again to recollect the UserId & password.
______________________________________________________________________________
Objectives:
Learning Outcomes:
After this practical, students will able to:
 Develop server side programs in the form of servlets.
 Demonstrate how JSP works
Pre-Lab questions:
 What is JSP?
 What are JSP implicit objects?
 What is the difference between include directive and include action?
Theory Involved
JSP is a webpage scripting language that can generate dynamic content while Servlets are Java programs that
are already compiled which also creates dynamic web content. Servlets run faster compared to JSP. JSP can
be compiled into Java Servlets. It's easier to code inJSP than in Java Servlets.
JSP Processing:
The following steps explain how the web server creates the web page using JSP:
 As with a normal page, your browser sends an HTTP request to the web server.
25
The web server recognizes that the HTTP request is for a JSP page and forwards it to a JSP engine. This
is done by using the URL or JSP page which ends with .jsp instead of .html.
 The JSP engine loads the JSP page from disk and converts it into a servlet content. This conversion is
very simple in which all template text is converted to println( ) statements and all JSP elements are
converted to Java code that implements the corresponding dynamic behavior of the page.
 The JSP engine compiles the servlet into an executable class and forwards the original request to a servlet
engine.
 A part of the web server called the servlet engine loads the Servlet class and executes it. During execution,
the servlet produces an output in HTML format, which the servlet engine passes to the web server inside
an HTTP response.
 The web server forwards the HTTP response to your browser in terms of static HTML content.
 Finally web browser handles the dynamically generated HTML page inside the HTTP response exactly as
if it were a static page.
Procedure to do Practical
Similar to Common Gateway Interface (CGI) scripts, servlets support a request and response programming
model. When a client sends a request to the server, the server sends the request to the servlet. The servlet
then constructs a response that the server sends back to the client. Unlike CGI scripts, however, servlets run
within the same process as the HTTP server.
When a client request is made, the service method is called and passed a request and response object.
The servlet first determines whether the request is a GET or POST operation.
It then calls one of the following methods: doGet or doPost.
The doGet method is called if the request isGET, and doPost is called if the request is POST.
Both doGet and doPost take request ( HttpServletRequest) and response (HttpServletResponse).
In the simplest terms, then, servlets are Java classes that can generate dynamic HTML content
using print statements.

What is important to note about servlets, however, is that they run in a container, and the APIs provide
session and object life-cycle management. Consequently, when you use servlets, you gain all the benefits from
26
the Java platform, which include the sandbox (security), database access API via JDBC, and cross-platform
portability of servlets.
Post Lab Quiz:
 How can I prevent the output of my JSP or Servlet pages from being cached by the browser?
 How can we forward the request from jsp page to the servlet
Critical Findings and Conclusion:
Remarks and Signature of Faculty with date:
27
Date of Performance:
Date of Submission:
Exp. No.: 7
Title:
Develop a Javabean named Student with appropriate data members, member functions and constructors.
Develop a JSP page source.jsp for the following tasks:
1.
Use the above developed Student java bean.
2.
Set properties of the java bean.
3.
Pass it to another JSP page destination.jsp
The destination.jsp should receive the javabean which is sent by source.jsp, and should get and print all the
data members of javabean.
Note: Use action alements: <jsp:useBean>, <jsp:getProperty>, <jsp:setProperty>, <jsp:param>, <jsp:forward>
etc.
EXP NO: 8
Title:
1. Develop stateless/stateful session bean say CalculatorBean representing general calculator. Also
develop suitable servlet to test the CalculatorBean.
2. Develop a stateless session bean that takes a word and returns it spelled backwards. Also develop
suitable jsp to test your bean.
3. Write a stateful bean that takes one word at a time and appends it o the previous words received to
make a sentence. Return the entire sentence each time a word is added. (*)
EXP NO: 9
Title:
Create an entity bean for a demo Audio CD collection application named CompactDiscTitle. Attributes should
be name (String, and it will be the primary key) and price (double). Write a stateless session bean that allows
you to get, add, update and remove a CD title. Write a client application to test the beans.
______________________________________________________________________________
Objectives: To create and manipulate entity java beans, stateful and stateless session beans.
Learning Outcomes:
28
After this practical, students will able to:
 Develop an application based on Enterprise JavaBeans technology and the Java Persistence API
Pre-Lab questions:
 What is EJB in java?
 What is session bean?
 What is entity bean?
 What is message driven bean?
Theory Involved
Enterprise Java Beans (EJB) is a development architecture for building highly scalable and robust enterprise
level applications to be deployed on J2EE compliant Application Server such as JBOSS, Web Logic etc.
Types
EJB are primarily of three types which are briefly described below:
Type
Description
Session Bean
Session bean stores data of a particular user for a
single session. It can be stateful or stateless. It is
less resource intensive as compared to entity
beans. Session bean gets destroyed as soon as user
session terminates.
Entity Bean
Entity beans represents persistent data storage.
User data can be saved to database via entity
beans and later on can be retrived from the
database in the entity bean.
Message Driven Bean
Message driven beans are used in context of JMS
(Java Messaging Service). Message Driven Beans
29
can consumes JMS messages from external entities
and act accordingly.
Procedure to do Practical
Following are the steps required to create a stateless ejb.
 Create a remote/local interface exposing the business methods.
 This interface will be used by the ejb client application.


Use @Local annotation if ejb client is in same environment where ejb session bean is to be deployed.

Use @Remote annotation if ejb client is in different environment where ejb session bean is to be deployed.
 Create a stateless session bean implementing the above interface.
 Use @Stateless annotation to signify it a stateless bean. EJB Container automatically creates the relevant
configurations or interfaces required by reading this annotation during deployment.
Following are the steps required to create a stateful ejb.
 Create a remote/local interface exposing the business methods.
 This interface will be used by the ejb client application.
 Use @Local annotation if ejb client is in same environment where ejb session bean is to be deployed.
 Use @Remote annotation if ejb client is in different environment where ejb session bean is to be deployed.
 Create a stateful session bean implementing the above interface.
 Use @Stateful annotation to signify it a stateful bean. EJB Container automatically creates the relevant
configurations or interfaces required by reading this annotation during deployment.

Post Lab Quiz:
 What are the differences between stateful session beans and stateless session beans?
 Is Message Driven bean a stateless bean?
Critical Findings and Conclusion:
30
Remarks and Signature of Faculty with date:
31
Date of Performance:
Date of Submission:
Exp. No.: 10
Title:
Develop a StringWebService. The service should provide the following web methods:
(i)
int stringLength(String message)
(ii)
String convertStringToUpperCase(String message)
(iii) boolean checkPalindrome(String message)
Also develop a Java client to test the web methods of above StringWebService.
______________________________________________________________________________
Objectives: To create web service and Manipulate it using Java Client.
Learning Outcomes:
After this practical, students will able to:
 Develop a basic web service with the Java EE platform.
Pre-Lab questions:
 What is web service?
 Explain different types of web service and its component?
Theory Involved
What is Web Service
A Web Service is can be defined by following ways:
o is a client server application or application component for communication.
o method of communication between two devices over network.
o is a software system for interoperable machine to machine communication.
o is a collection of standards or protocols for exchanging information between two devices or application.
What is Web Service
A Web Service is can be defined by following ways:
32
o
o
o
o
Let's
is a client server application or application component for communication.
method of communication between two devices over network.
is a software system for interoperable machine to machine communication.
is a collection of standards or protocols for exchanging information between two devices or application.
understand it by the figure given below:
Types of Web Services
There are mainly two types of web services.
1. SOAP web services.
2. RESTful web services.
33
Web Service Components
There are three major web service components.
1. SOAP
2. WSDL
3. UDDI
SOAP
SOAP is an acronym for Simple Object Access Protocol.
SOAP is a XML-based protocol for accessing web services.
SOAP is a W3C recommendation for communication between applications.
SOAP is XML based, so it is platform independent and language independent. In other words, it can be used
with Java, .Net or PHP language on any platform.
WSDL
WSDL is an acronym for Web Services Description Language.
WSDL is a xml document containing information about web services such as method name, method parameter
and how to access it.
WSDL is a part of UDDI. It acts as a interface between web service applications.
WSDL is pronounced as wiz-dull.
34
UDDI
UDDI is an acronym for Universal Description, Discovery and Integration.
UDDI is a XML based framework for describing, discovering and integrating web services.
UDDI is a directory of web service interfaces described by WSDL, containing information about web services.
Procedure to do Practical in Eclipse
In short, the steps you will take to produce and test your first web service are as follows:
1 Create a project in the Eclipse workspace of type 'Dynamic Web Project', which will host your web service.
2 Write the Java code (the 'business logic') that implements your web service functionality.
3 Use Eclipse to automatically generate the components (WSDL etc.) that will transform the Java code into a
web service, and then ask Eclipse to run that web service for you.
4 Create another project of type 'Dynamic Web Project', which will host the client application that you will use
to access and test the web service. 5 Use Eclipse to automatically generate a set of web pages that function as
a client interface to call the web service.
6 Use the web page client to send a request to the web service and observe the web service's response.
Post Lab Quiz:
 What is WSDL, UDDI, SOAP?
 What is the difference between SOAP and Restful Web services.
Critical Findings and Conclusion:
Remarks and Signature of Faculty with date:
35