IT LAB PROGRAMS 11-20
11. Design an XML document to store information about a student in a
college and display it.
Xml file
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="style.css"?>
<STUDENT_DATA>
<STUDENT>
<USN>USN: 1111111</USN>
<NAME>NAME: Anurag</NAME>
<COLLEGE>COLLEGE: Presidency</COLLEGE>
<EMAIL>E-MAIL: anurag@email.com</EMAIL>
</STUDENT>
<STUDENT>
<USN>USN: 2222222</USN>
<NAME>NAME: Kevin</NAME>
<COLLEGE>COLLEGE: USC</COLLEGE>
<EMAIL>E-MAIL: kevin@email.com</EMAIL>
</STUDENT>
</STUDENT_DATA>
CSS FILE
*{
display: block;
font-size: 20px;
USN{
color: blue;
font-size: 30px;
margin-top: 20px;
}
12. Design signup form to validate username, password, and phone numbers
etc using Java script.
HTML FILE
<!DOCTYPE html>
<html lang="en">
<head>
<title>Form</title>
</head>
<body>
<script>
function validateForm(){
var name = document.getElementById("name").value;
var password = document.getElementById("password").value;
var phoneno = document.getElementById("phoneno").value;
if(name==null || name==""){
alert("name cant be empyt");
return false;
if(password.length < 6){
alert("password must be 6 character long");
return false
if(phoneno.length != 10){
alert("phone number must be 10 digits");
return false
document.getElementById("output").innerHTML = "You are valid user";
</script>
<form>
Name: <input type="text" id="name"><br>
Password: <input type="password" id="password"><br>
Phone Number: <input type="text" id="phoneno"><br>
<input type="button" onclick="validateForm()" value="submit">
<h1 id="output"></h1>
</form>
</body>
</html>
13. Write a JavaScript program to determine whether a given year is a leap
year.
HTML FILE
<!DOCTYPE html>
<html lang="en">
<head>
<title>JavaScript to check leap year</title>
</head>
<body>
Enter Year: <input type="text" id="year">
<input type="button" id="button" value="check" onclick="leapYear()">
<h2 id="output"></h2>
<script>
function leapYear(){
var year = document.getElementById("year").value;
var result = (year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0));
if(result == true){
document.getElementById("output").innerHTML = "It is a leap year";
else{
document.getElementById("output").innerHTML = "It is a not leap year";
</script>
</body>
</html>
14. Write a JavaScript program to convert temperatures to and from celsius,
Fahrenheit
HTML FILE
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<h2>JS Celcius to Farahnit</h2>
<p>Enter number</p>
<input type="number" id="c" onkeyup="convert('c')"> Degree Celcius<br>
<input type="number" id="f" onkeyup="convert('f')"> Degree Farahnit<br>
<p><b>Note: </b> math.round() is used to return</p>
<script>
function convert(degree){
var x;
if(degree == 'c'){
x = document.getElementById("c").value * 9/5 + 32;
document.getElementById("f").value = x;
else{
x = (document.getElementById("f").value-32) * 5/9;
document.getElementById("c").value = x;
</script>
</body>
</html>
15. Write a JavaScript program to use pi value as global variable and print the
area of circle.
HTML FILE
<!DOCTYPE html>
<html lang="en">
<body>
<h1>Area Of A Circle</h1>
Enter the Radius: <input type="text" name="text" id="radius">
<input type="button" value="calculate" onclick="calculate()">
<script>
var pi = 3.14;
function calculate(){
var radius = document.getElementById("radius").value;
alert("The Area of the circle is " + (radius*radius*pi));
</script>
</body>
</html>
16. Write servlet application to print current date & time.
SERVLET FILE
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
@WebServlet("/DateTime")
public class DateTime extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
java.util.Date date = new java.util.Date();
pw.println("<h2>Current Date and Time" + date.toString() + "</h2>");
pw.close();
17. Write a servlet code to demonstrate session tracking.
FIRST SERVLET FILE
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/FirstServlet")
public class FirstServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String n = request.getParameter("uname");
out.print("Welcome " + n);
Cookie ck = new Cookie("uname", n);
response.addCookie(ck);
out.println("<h1> This is 1st Servlet</h1>");
out.println("<form action='SecondServlet'>");
out.println("<input type='submit' value='go'>");
out.println("</form>");
out.close();
SECOND SERVLET FILE
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/SecondServlet")
public class SecondServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<h1> This is second<h1>");
Cookie ck[] = request.getCookies();
out.print("Hello " + ck[0].getValue());
out.close();
HTML FILE
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="FirstServlet" method="post">
Name:<input type="text" name="uname"><br>
<input type="submit">
</form>
</body>
</html>
18. Write a Java Program to Implement Helloworld program using servlets.
SERVLET FILE
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/Hello")
public class Hello extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("Hello world");
out.close();
19. Write a JSP Program using Expression, Scriplet and Directive.
JSP FILE
<%@page import="java.time.Month" %>
<!DOCTYPE html>
<html>
<head>
<title>Scripting Tags</title>
</head>
<body>
convert a string to upper case:
<% new String("HelloWorld").toUpperCase(); %>
<%
for(Month month:Month.values()){
out.println("<br>"+month);
%>
<%!
int cube(int n){
return n*n*n;
%>
<% out.println("<br> Cube of 3 is: " + cube(3));
%>
</body>
</html>
20. Write a JSP program to demonstrate database connectivity.
JSP FILE
<%@page import = "java.sql.*" %>
<%@page import = "java.io.*" %>
<html>
<head>
<title>Connection with mysql database</title>
</head>
<body>
<h1>Connection Status</h1>
<%
try{
String connectionUrl = "jdbc:mysql://localhost:3306/test";
Connection connect = null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
connect = DriverManager.getConnection(connectionUrl, "root", "");
if(!connect.isClosed()){
out.println("Successfully connected to " + "MySQL server
using TCP/IP...");
connect.close();
catch(Exception ex){
out.println("Unable to connect to Database");
%>
</body>
</html>