Web Technology
Web Technology
What is XHTML
XHTML stands for EXtensible HyperText Markup Language. It is a cross between HTML
and XML language.
XHTML is almost identical to HTML but it is stricter than HTML. XHTML is HTML defined
as an XML application. It is supported by all major browsers.
Difference between HTML and XHTML
HTML XHTML
Introduction to XML
XML is a software- and hardware-independent tool for storing and
transporting data in structured form.
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
1. An XML declaration
2. A DTD
Simple XHTML 1.0 Document with XML Prolog and Document Body
Traversing and modifying a DOM tree means moving up, down, and
sideways through the DOM tree, selecting nodes based on their
relationship to other nodes.
parentNode
childNodes[nodenumber]
firstChild
lastChild
nextSibling
previousSibling
parentNode
In JavaScript, a parent node refers to the node that is immediately higher in the
DOM (Document Object Model) hierarchy relative to another node.
For example, consider the following HTML code:
<div>
<h2>Sample Title</h2>
<p>This is Paragraph</p>
</div>
In this code, the parent node is the div element, which contains two child nodes,
both of which are h2 and p elements.
For example, to access the parent node of the second child p element in the
example above, you could use the following code:
<script>
document.write(para);
console.log(parent);
parent.style.backgroundColor = "red";
parent.style.padding = "10px";
</script>
Modifying a DOM
Adding and Removing Nodes (HTML Elements)
Append method is used to add new HTML elements.
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript HTML DOM</h2>
<p>Add a new HTML Element.</p>
<div id="div1">
<p id="p1">This is a paragraph.</p>
<p id="p2">This is another paragraph.</p>
</div>
<script>
const para = document.createElement("p");
const node = document.createTextNode("This is new.");
para.appendChild(node);
const element = document.getElementById("div1");
element.appendChild(para);
</script>
</body>
</html>
To remove an HTML element, use the remove() method:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript HTML DOM</h2>
<h3>Remove an HTML Element.</h3>
<div>
<p id="p1">This is a paragraph.</p>
<p id="p2">This is another paragraph.</p>
</div>
<button onclick="myFunction()">Remove Element</button>
<script>
function myFunction() {
document.getElementById("p1").remove();
}
</script>
</body>
</html>
DOM collections and styles
The length property defines the number of elements in an HTMLCollection.
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript HTML DOM</h2>
<p>Hello World!</p>
<p>Hello Norway!</p>
<p id="demo"></p>
<script>
const myCollection = document.getElementsByTagName("p");
document.getElementById("demo").innerHTML = "This document contains " + my
Collection.length + " paragraphs.";
</script>
</body>
</html>
Example :- write a javascript program to change the text color of all elem
ents.
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript HTML DOM</h2>
<p>Hello World!</p>
<p>Hello Norway!</p>
<p>Click the button to change the color of all p elements.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
const myCollection = document.getElementsByTagName("p");
for (let i = 0; i < myCollection.length; i++) {
myCollection[i].style.color = "red";
}
}
</script>
</body>
</html>
Dom events
A JavaScript can be executed when an event occurs, like when a user clicks on an H
TML element.
Examples of HTML DOM events:
For example, the content of the <h1> element is changed when a user clicks on
it:
<!DOCTYPE html>
<html>
<body>
</body>
</html>
#Java script events handler
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript HTML Events</h1>
<h2>The onclick Attribute</h2>
<h2 onclick="changeText(this)">hello sir</h2>
<script>
function changeText(id) {
id.innerHTML = "welcome sir";
}
</script>
</body>
</html>
The onload and onunload Events:-The onload and onunload events are
triggered when the user enters or leaves the page.
The onload event can be used to check the visitor's browser type and browser
version, and load the proper version of the web page based on the information.
The onload and onunload events can be used to deal with cookies.
For example:-
<!DOCTYPE html>
<html>
<body onload="checkCookies()">
<p id="demo"></p>
<script>
function checkCookies() {
if (navigator.cookieEnabled == true) {
} else {
document.getElementById("demo").innerHTML = text;
</script>
</body>
</html>
When a web server has sent a web page to a browser, the connection is shut down,
and the server forgets everything about the user.
The oninput Event:-The oninput event is often to some action while the
user input data.
Example-
<!DOCTYPE>
<html>
<body>
<script>
function upperCase() {
const x = document.getElementById("fname");
x.value = x.value.toUpperCase();
</script>
</body>
</html>
Below is an example of how to use the onchange. The upperCase() function will be
called when a user changes the content of an input field.
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript HTML Events</h1>
<script>
function upperCase() {
const x = document.getElementById("fname");
x.value = x.value.toUpperCase();
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
style="background-
color:#D94A38;width:120px;height:20px;padding:40px;">
<script>
function mOver(obj) {
function mOut(obj) {
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
style="background-
color:#D94A38;width:90px;height:20px;padding:40px;">
Click Me</div>
<script>
function mDown(obj) {
obj.style.backgroundColor = "#1ec5e5";
function mUp(obj) {
obj.style.backgroundColor="#D94A38";
obj.innerHTML="Thank You";
</script>
</body>
</html>
Introduction to AJAX
What is AJAX?
AJAX = Asynchronous JavaScript And XML.
Example of AJAX
<html>
<body>
<div id="demo">
<script>
function loadDoc() {
xhttp.onreadystatechange = function() {
document.getElementById("demo").innerHTML =
this.responseText;
};
xhttp.send();
</script>
</body>
</html>
Server-side scripts are programs that run on a web server to generate dynamic
web pages, creating a unique experience for each user. These scripts form the
basic framework for back-end web development.
1. Tier-1
2. Tier -2
3. Tier -3
4. N -tier
Data layer: the data stored in data layer. Application layer communicates
with database layer to retrieve the data. It contains the methods that connect
the database and performs required action.
2. Tier -2
3. Tier -3
4. N -tier
1. Tier-1:-
In 1-Tier Architecture the database is directly available to
the user, the user can directly sit on the DBMS and use it
that is, the client, server, and Database are all present on
the same machine. For Example: to learn SQL we set up an
SQL server and the database on the local system. This
enables us to directly interact with the relational database
and execute operations.
2. Tier -2:-
The 2-tier architecture is similar to a basic client-server
model. The application at the client end directly
communicates with the database on the server side. APIs
like ODBC and JDBC are used for this interaction.
3. Tier -3:-
4. N – tier:
An N-Tier Application program is one that is distributed among three
or more separate computers in a distributed network.
Net.Data
Net.Data is a server-side scripting engine that allows you to easily create dynamic documents using live
data from a variety of sources such as relational and non-relational database management systems
(DBMSs), including DB2® databases that can be accessed through DRDA, files, and native applications
written in programming languages such as RPG, Cobol, Java™, C, C++, and REXX.
Node.js
Node.js is an open source project based on the Google Chrome JavaScript Engine. It provides a platform
for server-side JavaScript applications running without browsers.
PHP
Hypertext Preprocessor (PHP) is one of the world's most popular server-side scripting language for
building dynamic, data-driven Web applications.
Python
Python is an agile, dynamically typed, expressive, open source programming language that supports
multiple programming philosophies, including procedural, object-oriented, and functional.
Head :- The HEAD method asks for a response identical to a GET request, but without
the response body.
Post :- The POST method submits an entity to the specified resource, often causing a
change in state or side effects on the server.
Introduction to JDBC
JDJDBC stands for java “database connectivity” .It is an API that is used to
connect and execute the queries with the respective database.
Example :-
Class jdbc
(1)JDK
(2)Eclipse IDE
(3)oracle 11g
JSP-BASICS
o page directive
o include directive
o taglib directive
1. <html>
2. <body>
3.
4. <%@ include file="header.html" %>
5.
6. Today is: <%= java.util.Calendar.getInstance().getTime() %>
7.
8. </body>
9. </html>
1. <html>
2. <body>
3.
4. <%@ taglib uri="http://www.google.com/tags" prefix="mytag" %>
5.
6. <mytag:currentDate/>
7.
8. </body>
9. </html>
Jsp elements
pageEncoding="ISO-8859-1"%>
<html>
<head>
<title>Scriptlets</title>
</head>
<body>
<h3>--Welcome--</h3>
<%
int a = 3;
int b = 4;
int c = 5;
out.println("a is: " + a + "<br>" + "b is:" + b + "<br>" + "c is:" + c + "<br>");
%>
</body>
</html>
JSP Expressions
This is scripting element are often wont to evaluate the only java expression and display that
expression resultant value onto the client browser.
Syntax: <%=Java Expression%>
pageEncoding="ISO-8859-1"%>
<html>
<body>
Current Time:
<%=java.util.Calendar.getInstance().getTime()%>
</body>
</html>
Jsp variables
JSP Declaration Tag
The JSP declaration tag is used to declare fields and methods.
The code written inside the jsp declaration tag is placed outside the service() method of
auto generated servlet.
The jsp scriptlet tag can only declare variables The jsp declaration tag can declare variables as well
not methods. as methods.
The declaration of scriptlet tag is placed inside The declaration of jsp declaration tag is placed
the _jspService() method. outside the _jspService() method.
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
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>
JSP Implicit Objects
There are 9 jsp implicit objects. These objects are created by the web container that are
available to all the jsp pages.
The available implicit objects are out, request, config, session, application etc.
Object Type
Out JspWriter
Request HttpServletRequest
Response HttpServletResponse
Config ServletConfig
Application ServletContext
Session HttpSession
pageContext PageContext
Page Object
Exception Throwable
1. <form action="welcome.jsp">
2. <input type="text" name="uname">
3. <input type="submit" value="go"><br/>
4. </form>
welcome.jsp
1. <%
2. response.sendRedirect("http://www.google.com");
3. %>
1. <form action="welcome">
2. <input type="text" name="uname">
3. <input type="submit" value="go"><br/>
4. </form>
web.xml file
1. <web-app>
2.
3. <servlet>
4. <servlet-name>sonoojaiswal</servlet-name>
5. <jsp-file>/welcome.jsp</jsp-file>
6. </servlet>
7.
8. <servlet-mapping>
9. <servlet-name>sonoojaiswal</servlet-name>
10. <url-pattern>/welcome</url-pattern>
11. </servlet-mapping>
12.
13. <context-param>
14. <param-name>dname</param-name>
15. <param-value>sun.jdbc.odbc.JdbcOdbcDriver</param-value>
16. </context-param>
17.
18. </web-app>
welcome.jsp
1. <%
2.
3. out.print("Welcome "+request.getParameter("uname"));
4.
5. String driver=application.getInitParameter("dname");
6. out.print("driver name is="+driver);
7.
8. %>
index.jsp
<form action="process.jsp">
No1:<input type="text" name="n1" /><br/><br/>
No2:<input type="text" name="n2" /><br/><br/>
<input type="submit" value="divide"/>
</form>
process.jsp
String num1=request.getParameter("n1");
String num2=request.getParameter("n2");
int a=Integer.parseInt(num1);
int b=Integer.parseInt(num2);
int c=a/b;
out.print("division of numbers is: "+c);
%>
error.jsp
<web-app>
<error-page>
<exception-type>java.lang.Exception</exception-type>
<location>/error.jsp</location>
</error-page>
</web-app>
web.xml file if you want to handle the exception for a specific error code
<web-app>
<error-page>
<error-code>500</error-code>
<location>/error.jsp</location>
</error-page>
</web-app>
process.jsp
Now, you don't need to specify the errorPage attribute of page directive in the jsp page.
<%
String result;
try {
// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(mailSession);
// Send message
Transport.send(message);
result = "Sent message successfully....";
} catch (MessagingException mex) {
mex.printStackTrace();
result = "Error: unable to send message....";
}
%>
<html>
<head>
<title>Send Email using JSP</title>
</head>
<body>
<center>
<h1>Send Email using JSP</h1>
</center>
OrderForm.jsp
<HTML>
<HEAD>
<TITLE>A Catalog Order Form</TITLE>
</HEAD>
<BODY>
<H1 ALIGN="center">A Sample Order Form</H1>
<%!
String item[] = {"DVD", "CD", "Diskette"};
double price[] = {19.99, 12.99, 1.99};
int quantity[] = {2, 9, 24};
%>
<TABLE ALIGN="center" BGCOLOR="lightgray" BORDER="1" WIDTH="75%">
<TR><TD>Item</TD>
<TD>Price</TD>
<TD>Quantity</TD>
<TD>Total Price</TD>
</TR>
<% for (int i=0; i<3; i++) { %>
<TR><TD><%= item[i] %></TD>
<TD><%= price[i] %></TD>
<TD><%= quantity[i] %></TD>
<TD><%= price[i] * quantity[i] %></TD>
</TR>
<% } //end for loop %>
</TABLE>
</BODY>
</HTML>
Example 2
Write a JSP program calculates factorial values for an integer number, while the input is taken
from an HTML form.
input.html
<html>
<body>
<form action="Factorial.jsp">
Enter a value for n: <input type="text" name="val">
<input type="submit" value="Submit">
</form>
</body>
</html>
Factorial.jsp
<html>
<body>
<%!
long n, result;
String str;
long fact(long n) {
if(n==0)
return 1;
else
return n*fact(n-1);
}
%>
<%
str = request.getParameter("val");
n = Long.parseLong(str);
result = fact(n);
%>
<b>Factorial value: </b> <%= result %>
</body>
</html>
Example 3
Write a JSP program shows the Fibonacci series up to a particular term, while the input is taken
from an HTML form.
input.html
<html>
<body>
<form action="Fibonacci.jsp">
Enter a value for n: <input type="text" name="val">
<input type="submit" value="Submit">
</form>
</body>
</html>
Fibonacci.jsp
<html>
<body>
<%!
int n;
String str;
int fibo(int n) {
if(n<2)
return n;
else
return fibo(n-1) + fibo(n-2);
}
%>
<b>Fibonacci series: </b><br>
<%
str = request.getParameter("val");
n = Integer.parseInt(str);
Example 4
Write a JSP program calculates Powers of 2 for integers in the range 0-10.
PowersOf2.jsp
<html>
<head>
<title>Powers of 2</title>
</head>
<body>
<center>
<table border="2" align="center">
<th>Exponent</th>
<th>2^Exponent</th>
<% for (int i=0; i<=10; i++) { //start for loop %>
<tr>
<td><%= i%></td>
<td><%= Math.pow(2, i) %></td>
</tr>
<% } //end for loop %>
</table>
</center>
</body>
</html>
For