Web Technology
Web Technology
JSP Servlet
JSP is a webpage scripting language Servlets are Java programs that are
that can generate dynamic content. already compiled which also creates
dynamic web content
JSP run slower compared to Servlet as Servlets run faster compared to JSP.
2. it takes compilation time to convert 2 CO-4 K-4
into Java Servlets.
It’s easier to code in JSP than in Java Its little much code to write here.
Servlets.
The advantage of JSP programming There is no such custom tag facility in
over servlets is that we can build servlets.
custom tags which can directly call
Java beans.
JavaBeans are classes that encapsulate many objects into a single object (the bean).
It is a java class that should follow following conventions:
4. 2 CO-4
1. Must implement Serializable.
2. It should have a public no-argument constructor.
3. All properties in java bean must be private with public getters and setter
methods.
// Java program to illustrate the structure of JavaBean class
public classTestBean {
private String name;
public void setName(String name)
{
this.name = name;
}
public String getName()
{
returnname;
}
}
5. Web Services are software systems that are designed to be accessed using web 2 CO-5 K-3
protocols and technologies that are intended to be used by other software
applications rather than directly by end users.
Explain about WSDL?
Service endpoint interface is a Java interface that specifies the operations that will be provided by the
service which is essentially an API for the service.
PART B - (3 X 12 = 36 marks)
8. (a) With suitable example explain how XSLT can be used to transform a document from XML to XHTML.
12 CO-4 K-5
Start with an XML document "cdcatalog.xml" to transform into XHTML:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="detail.xsl"?>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd>
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<year>1988</year>
</cd>
2
<cd>
<title>Greatest Hits</title>
<artist>Dolly Parton</artist>
<country>USA</country>
<company>RCA</company>
<price>9.90</price>
<year>1982</year>
</cd>
</catalog>
The root element that declares the document to be an XSL style sheet is <xsl:stylesheet> or <xsl:transform>.
To get access to the XSLT elements, attributes and features, declare the XSLT namespace at the top of the
document.
(b) (i) List and explain the XML syntax rules in detail. 12 CO-4 K-5
XML DTD
Document Type Definition purpose is to define the structure of an XML document. It defines the structure
with a list of defined elements in the xml document.
<!DOCTYPE note
[
<!ELEMENT note (to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>
]>
where PCDATA refers parsed character data. In the above xml document the elements to, from, heading, body
carries some text, so that, these elements are declared to carry text in DTD file.
This definition file is stored
with .dtd extension.
Entity References
- Some characters have a special meaning in XML.
4
- 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>
Comments in XML
The syntax for writing comments in XML is similar to that of HTML:
<!-- This is a comment -->
• CDATA section
– By default, all text inside an XML document is parsed
– You can force text to be treated as unparsed character data by enclosing it in <![CDATA[ ...
]]>
– CDATA means, Character Data defined as blocks of text that are not parsed by the parser, but
are otherwise recognized as markup.
– The entire content of a CDATA section is interpreted as character data, even if it appears to be
markup.
– Begins with <![CDATA[
– Ends with ]]>
– Any characters, even & and <, can occur inside a CDATA
– Whitespace inside a CDATA is (usually) preserved.
<message>
The markup
<![CDATA[
<message>Welcome to XML</message>
]]>
In the above syntax, everything between <message> and </message> is treated as character data and not as
markup.
The following figure illustrates the interaction between Model, View and Controller.
5
MVC Architecture
MVC stands for Model, View and Controller. MVC separates application into three components - Model, View
and Controller.
Model: Model represents shape of the data and business logic. It maintains the data of the application. Model
objects retrieve and store model state in a database.
Model is a data and business logic.
View: View is a user interface. View display data using model to the user and also enables them to modify the
data.
View is a User Interface.
Controller: Controller handles the user request. Typically, user interact with View, which in-turn raises
appropriate URL request, this request will be handled by a controller. The controller renders the appropriate
view with the model data as a response.
Controller is a request handler.
9. (a) Develop an XML document that will hold player(like cricket) collection with field for player-name, age,
batting-average and highest-score. Write suitable DTD and XML schema. 12 CO-4 K-6
In HTML, a browser can check HTML because it knows all about legal HTML.
In XML, user defines what's legal and what's not by specifying the syntax for an XML document.
Document Type Definitions (DTD) provides the original way to validate XML documents, and the syntax for
DTDs is built right in XML 1.0 specification.
Create an external DTD file and its name must be specified in the corresponding XML file. Following
is the DTD file cricketplayer.dtd
<?xml version = "1.0" encoding="UTF-8" ?>
<!DOCTYPE cricketplayer [
<!ELEMENT cricketplayer (player)*>
<!ELEMENT player (playername, age, battingaverage, highestscore)>
<!ELEMENT playername (#PCDATA)>
<!ELEMENT age(#PCDATA)>
<!ELEMENT battingaverage (#PCDATA)>
<!ELEMENT highestscore(#PCDATA)>
]>
Just like a DTD, the XML schema is used to describe the structure of an XML document.
An XML document validated against the XML schema is called “Well Formed”
7
• The SOAP Envelope element is the root element of a SOAP message. This element defines the XML
document as a SOAP message. The xmlns:soap namespace defines the Envelope as a SOAP Envelope.
The encodingStyle attribute is used to define the data types used in the document.
• The SOAP Header element contains header information. If the Header element is present, it must be the
first child element of the Envelope element.
• The SOAP Body element contains the actual SOAP message intended for the ultimate endpoint of the
message.
• The SOAP Fault element holds errors and status information for a SOAP message.
• Serialization is the process of translating data structures or object state into a writable byte stream
format that can be stored (for example, in a file or memory buffer) or transmitted (for example, across
a network connection link) and reconstructed later as an actual Java object (possibly in a different
computer environment).
• This process of serializing an object is also called marshalling an object. The opposite operation,
extracting a data structure or object from a series of bytes, is deserialization(also
called unmarshalling).
• This mechanism is used to persist the object.
• A Java object is serializable if its class or any of its superclasses implement either
the java.io.Serializable interface
• The Serializable interface defines no methods; it is simply used to indicate to the Java run-time system
that the class author intends that objects belonging to the class can be serialized.
Advantages of Serialization
10. (a) Discover a JSP code to access a table and records from a student database to obtain the result of a
student. 12 CO-5 K-4
8
For displaying student mark list. Assume that student information and result is available in a
database which has been stored in a database server.
Database in MS-Access studentdata with student table having fields studid as Number, Studname as
Text, class as Text, WT as Number, CN as Number, ST as Number, MPMC as Number, ANT as
Number.
index.html
<HTML>
<HEAD>
<TITLE>Database Lookup</TITLE>
</HEAD>
<BODY>
<H1>Database Lookup</H1>
<FORM ACTION="basic.jsp" METHOD="POST">
Please enter the stud id you want to find to get result: <BR>
<INPUT TYPE="TEXT" NAME="id"><BR>
<INPUT TYPE="SUBMIT" value="Submit">
</FORM>
</BODY>
<HTML>
basic.jsp
<%@ page language="java" import="java.sql.*" %>
<%@ page import="java.io.*" %>
<%@ page import="java.util.*" %>
<HTML>
<HEAD>
<TITLE>Fetching Data From a Database</TITLE>
</HEAD>
<BODY>
<H1>Fetching Student result from the Database</H1>
<%
Connection connection=null;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc:odbc:studentDB1";
connection = DriverManager.getConnection(url," "," ");
Statement statement = connection.createStatement();
String id = request.getParameter("id");
9
ResultSet resultset = statement.executeQuery("select * from student where studid = ' " + id + " ' ");
if(!resultset.next())
{
out.println("Sorry, could not find that student. ");
}
else
{
%>
<TABLE BORDER="1">
<TR>
<TH>ID</TH>
<TH>Student Name</TH>
<TH>Class</TH>
<TH>WT</TH>
<TH>CN</TH>
<TH>ST</TH>
<TH>MPMC</TH>
<TH>ANT</TH>
</TR>
<TR>
<TD><%= resultset.getString(1) %></TD>
<TD><%= resultset.getString(2) %></TD>
<TD><%= resultset.getString(3) %></TD>
<TD><%= resultset.getString(4) %></TD>
<TD><%= resultset.getString(5) %></TD>
<TD><%= resultset.getString(6) %></TD>
<TD><%= resultset.getString(7) %></TD>
<TD><%= resultset.getString(8) %></TD>
</TR>
<%
}
resultset.close();
statement.close();
connection.close();
%>
</TABLE>
<BR>
<%
}
10
%>
</BODY>
</HTML>
10 (b) i) Discuss AJAX architecture and compare it with traditional web application architecture.
12 CO-5 K-6
Asynchronous Javascript and XML is a client side techniques that combines a set of known
technologies in order to create faster and more user friendly web pages.
User sends a request to server and continue interaction with the web page without waiting for server
response. Once the response arrives, a designated area in UI will update itself and reflect the response
information. Whole page need not be reloaded.
Ajax Architecture
Sl.no. Traditional Web Application Architecture Ajax based web application architecture.
1 At the client side the browser client has only At the client side the browser client has user
one component and that is user interface. interface and AJAX Engine due to which client
gets quick response from the server
2. It makes use of Synchronous It makes use of asynchronous communication.
communication. The client has to wait to get By adding new layer of AJAX Engine at the
the response from the server then only client client side, it eliminates start-stop-start-stop kind
can make the request to the server. It is of communication.
start-stop-start-stop kind of communication.
3 It is less responsive It is more responsive
4 User cannot get rich user interface User gets rich UI experience with ajax
experience architecture
5 Limited use of javascript, CSS and XML Ajax incorporates several technologies
technologies. Most user action in the
interface trigger an HTTP rewuest back to a standards-based presentation using
web server. XHTML, CSS
dynamic display and interaction using
11
DOM
data interchange and manipulation using
XML
asynchronous data retrieval using
XMLHttpRequest
and JavaScript binding everything
together.
The XMLHttpRequest object allows client-side JavaScript to make HTTP requests (both GET and
POST) to the server without reloading pages in the browser.
This JavaScript object was originally introduced in Internet Explorer 5 by Microsoft and it is the
enabling technology that allows asynchronous requests
By performing screen updates on the client, you have a great amount of flexibility when it comes to
creating your Web site :
Eliminate page refreshes every time there is user input
Edit data directly in place, without requiring the user to navigate to a new page to edit the data
Increase site performance by reducing the amount of data downloaded from the server
Properties:
readystate
- used to identify the state of the request. Possible values 0-4.
- 0 UNOPENED open() is not called.
- 1 OPENED open is called but send() is not called.
- 2 HEADERS_RECEIVED send() is called, and headers and status are available.
- 3 LOADING Downloading data; responseText holds the data.
- 4 DONE The operation is completed fully.
onreadystatechange
- It is called whenever readystate attribute changes. It must not be used with synchronous
requests.
status
• -Numeric code return by server.Eg.404,200
responseText
- the string data returned by the server process.
responseXML
- the DOM type of document returned by the server process.
12
Example:
CREATE XMLHTTPREQUEST OBJECT
<html>
<head>
<script type="text/javascript">
function fun1()
{
var request;
if (window.ActiveXObject)
{
/*supported in IE*/
request = new ActiveXObject("Microsoft.XMLHTTP");
}
else if (window.XMLHttpRequest)
{
/* supported in Safari and Mozilla*/
request = new XMLHttpRequest();
}
else
{
request = null; }
req.onreadystatechange=function()
{
if (req.readyState==4 && req.status==200)
{
document.getElementById("myDiv").innerHTML=req.responseText;
}
}
req.open("POST",”welcome.html",true);
req.send();
} // fun1() close
</script>
</head>
<body>
<button type="button" onclick="fun1()">Change Content</button><br><br>
<div id="myDiv">Click on the button above, this text can be changed</div>
</body>
</html>
13