XML and JSP
XML and JSP
XML and JSP
XML Tutorial
XML stands for eXtensible Markup Language.
XML was designed to store and transport data.
XML was designed to be both human- and machine-readable.
XML is a software- and hardware-independent tool for storing and transporting data.
What is XML?
XML stands for eXtensible Markup Language
XML is a markup language much like HTML
XML was designed to store and transport data
XML was designed to be self-descriptive
XML is a W3C Recommendation
XML Example 1
<?xml version="1.0" encoding="UTF-8"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
XML is Extensible
Most XML applications will work as expected even if new data is added (or removed).
Imagine an application designed to display the original version of note.xml (<to> <from>
<heading> <body>).
Then imagine a newer version of note.xml with added <date> and <hour> elements, and a removed
<heading>.
The way XML is constructed, older version of the application can still work:
<note>
<date>2015-09-01</date>
<hour>08:30</hour>
<to>Tove</to>
<from>Jani</from>
<body>Don't forget me this weekend!</body>
</note>
Transaction Data
Thousands of XML formats exist, in many different industries, to describe day-to-day data
transactions:
Stocks and Shares
Financial transactions
Medical data
Mathematical data
Scientific measurements
News information
Weather services
"Opening and closing tags" are often referred to as "Start and end tags". Use whatever you prefer. It
is exactly the same thing.
XML Elements Must be Properly Nested
In HTML, you might see improperly nested elements:
<b><i>This text is bold and italic</b></i>
In XML, all elements must be properly nested within each other:
<b><i>This text is bold and italic</i></b>
In the example above, "Properly nested" simply means that since the <i> element is opened inside
the <b> element, it must be closed inside the <b> element.
Entity References
Some characters have a special meaning in XML.
If you place a character like "<" inside an XML element, it will generate an error because the parser
interprets it as the start of a new element.
This will generate an XML error:
<message>salary < 1000</message>
To avoid this error, replace the "<" character with an entity reference:
<message>salary < 1000</message>
There are 5 pre-defined entity references in XML:
Comments in XML
The syntax for writing comments in XML is similar to that of HTML:
<!-- This is a comment -->
Two dashes in the middle of a comment are not allowed:
<!-- This is an invalid -- comment -->
XML Elements
An XML document contains XML Elements.
Naming Conventions
Some commonly used naming conventions for XML elements:
MESSAGE
To: Tove
From: Jani
Don't forget me this weekend!
Imagine that the author of the XML document added some extra information to it:
<note>
<date>2008-01-10</date>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
Should the application break or crash?
No. The application should still be able to find the <to>, <from>, and <body> elements in the XML
document and produce the same output.
This is one of the beauties of XML. It can be extended without breaking applications.
XML DOM
Example
<h1 id="demo">This is a Heading</h1>
<button type="button"
onclick="document.getElementById('demo').innerHTML = 'Hello World!'">Click Me!
</button>
Books.xml
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book category="cooking">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="children">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
</bookstore>
This code retrieves the text value of the first <title> element in an XML document:
Example
txt = xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue;
The XML DOM is a standard for how to get, change, add, and delete XML elements.
This example loads a text string into an XML DOM object, and extracts the info from it with
JavaScript:
Example
<html>
<body>
<p id="demo"></p>
<script>
var text, parser, xmlDoc;
text = "<bookstore><book>" +
"<title>Everyday Italian</title>" +
"<author>Giada De Laurentiis</author>" +
"<year>2005</year>" +
"</book></bookstore>";
document.getElementById("demo").innerHTML =
xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue;
</script>
</body>
</html>
JSP
Java Server Pages (JSP) is a server-side programming technology that enables the creation of
dynamic, platform-independent method for building Web-based applications. JSP have access to the
entire family of Java APIs, including the JDBC API to access enterprise databases.
index.jsp
Let's see the simple example of JSP where we are using the scriptlet tag to put Java code in the JSP
page. We will learn scriptlet tag later.
1. <html>
2. <body>
3. <% out.print(2*5);%>
4. </body>
5. </html>
It will print 10 on the browser.
JSP Tags *Important
****IMPORTANT
index.jsp
1. <html>
2. <body>
3. <%! int data=50; %>
4. <%= "Value of the variable is:"+data %>
5. </body>
6. </html>
Example of JSP declaration tag that declares method
In this example of JSP declaration tag, we are defining the method which returns the cube of given
number and calling this method from the jsp expression tag. But we can also use jsp scriptlet tag to
call the declared method.
index.jsp
1. <html>
2. <body>
3. <%!
4. int cube(int n){
5. return n*n*n*;
6. }
7. %>
8. <%= "Cube of 3 is:"+cube(3) %>
9. </body>
10. </html>
welcome.jsp
1. <%
2. String name=request.getParameter("uname");
3. out.print("welcome "+name);
4. %>
Output
GET method
The GET method sends the encoded user information appended to the page request. The page and
the encoded information are separated by the ? character as follows −
http://www.test.com/hello?key1=value1&key2=value2
The GET method is the default method to pass information from the browser to the web server and
it produces a long string that appears in your browser's Location:box. It is recommended that the
GET method is better not used. if you have password or other sensitive information to pass to the
server.
The GET method has size limitation: only 1024 characters can be in a request string.
This information is passed using QUERY_STRING header and will be accessible through
QUERY_STRING environment variable which can be handled using getQueryString() and
getParameter() methods of request object.
POST method
A generally more reliable method of passing information to a backend program is the POST
method.
This method packages the information in exactly the same way as the GET method, but instead of
sending it as a text string after a ? in the URL it sends it as a separate message. This message comes
to the backend program in the form of the standard input which you can parse and use for your
processing.
JSP handles this type of requests using getParameter() method to read simple parameters and
getInputStream() method to read binary data stream coming from the client.
<body>
<h1>Using GET Method to Read Form Data</h1>
<ul>
<li><p><b>First Name:</b>
<%= request.getParameter("first_name")%>
</p></li>
<li><p><b>Last Name:</b>
<%= request.getParameter("last_name")%>
</p></li>
</ul>
</body>
</html>
</body>
</html>
First Name:
Last Name:
SUBMIT
< p>Try to enter the First Name and the Last Name and then click the submit button to see the result
on your local machine where tomcat is running. Based on the input provided, it will generate similar
result as mentioned in the above example.
After you fill the fields and press the submit button, the data is sent to “main.jsp” which reads
the data and stores them in variables and displays them as a list as written in “main.jsp”.
What are Web Services? *Important
A web service is a collection of open protocols and standards used for exchanging data
between applications or systems. Software applications written in various programming
languages and running on various platforms can use web services to exchange data over
computer networks like the Internet in a manner similar to inter-process communication on
a single computer. This interoperability (e.g., between Java and Python, or Windows and
Linux applications) is due to the use of open standards.
To summarize, a complete web service is, therefore, any service that −
Is available over the Internet or private (intranet) networks
Uses a standardized XML messaging system
Is not tied to any one operating system or programming language
Is self-describing via a common XML grammar
Is discoverable via a simple find mechanism
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.
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.
What is Service?
A service is a well-defined, self-contained function that represents a unit of functionality. A service
can exchange information from another service. It is not dependent on the state of another service. It
uses a loosely coupled, message-based communication model to communicate with applications and
other services.
Service Connections
The figure given below illustrates the service-oriented architecture. Service consumer sends a
service request to the service provider, and the service provider sends the service response to the
service consumer. The service connection is understandable to both the service consumer and
service provider.
Service-Oriented Terminologies
• Services - The services are the logical entities defined by one or more published interfaces.
• Service provider - It is a software entity that implements a service specification.
• Service consumer - It can be called as a requestor or client that calls a service provider. A
service consumer can be another service or an end-user application.
• Service locator - It is a service provider that acts as a registry. It is responsible for
examining service provider interfaces and service locations.
• Service broker - It is a service provider that pass service requests to one or more additional
service providers.
Characteristics of SOA
The services have the following characteristics:
• They are loosely coupled.
• They support interoperability.
• They are location-transparent
• They are self-contained.
Components of service-oriented architecture
The service-oriented architecture stack can be categorized into two parts - functional aspects and
quality of service aspects.
Functional aspects
The functional aspect contains:
• Transport - It transports the service requests from the service consumer to the service
provider and service responses from the service provider to the service consumer.
• Service Communication Protocol - It allows the service provider and the service consumer
to communicate with each other.
• Service Description - It describes the service and data required to invoke it.
• Service - It is an actual service.
• Business Process - It represents the group of services called in a particular sequence
associated with the particular rules to meet the business requirements.
• Service Registry - It contains the description of data which is used by service providers to
publish their services.
Quality of Service aspects
The quality of service aspects contains:
• Policy - It represents the set of protocols according to which a service provider make and
provide the services to consumers.
• Security - It represents the set of protocols required for identification and authorization.
• Transaction - It provides the surety of consistent result. This means, if we use the group of
services to complete a business function, either all must complete or none of the complete.
• Management - It defines the set of attributes used to manage the services.
Advantages of SOA
SOA has the following advantages:
• Easy to integrate - In a service-oriented architecture, the integration is a service specification
that provides implementation transparency.
• Manage Complexity - Due to service specification, the complexities get isolated, and
integration becomes more manageable.
• Platform Independence - The services are platform-independent as they can communicate
with other applications through a common language.
• Loose coupling - It facilitates to implement services without impacting other applications or
services.
• Parallel Development - As SOA follows layer-based architecture, it provides parallel
development.
• Available - The SOA services are easily available to any requester.
• Reliable - As services are small in size, it is easier to test and debug them.