WT LAB Work
WT LAB Work
Year: 3rd
Experiment No: 1
OBJECTIVE: Write HTML/Java scripts to display your CV in navigator, your Institute website,
Department Website and Tutorial website for specific subject.
PROGRAM:
1. Navigator (Browser) Display: To display your CV in a web browser (navigator), you can
create an HTML file (cv.html) with the following content:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Your CV</title>
</head>
<body>
<h1>Your CV</h1>
<p>Education, experience, skills, etc.</p>
<!-- Add more sections as needed -->
</body>
</html>
2. Institute Website: If you want to display your CV on your institute’s website, you’ll need to
upload the cv.html file to the appropriate directory on the server where your institute’s website
is hosted. The URL to access your CV would be something like
https://www.example.com/cv.html.
3. Department Website: Similarly, for the department website, upload the cv.html file to the
relevant directory on the department’s server. The URL would be specific to your department,
such as https://www.example.com/department/cv.html.
4. Tutorial Website for a Specific Subject: If you want to create a tutorial website related to a
specific subject (e.g., Java programming), you can create a separate HTML file for that tutorial.
For example, let’s say you want to create a tutorial on “Introduction to Java.” You can create
an intro_to_java.html file with relevant content and upload it to the tutorial website.
Experiment No: 2
OBJECTIVE: Write HTML/Java scripts to display your CV in navigator, your Institute website,
Department Website and Tutorial website for specific subject.
PROGRAM:
Create a student details entry form using JavaServer Pages (JSP). JSP allows you to embed Java code
directly into your HTML pages.
Create the JSP Form: Below is an example of a JSP form for collecting student details. Save this
as student_form.jsp:
<label for="email">Email:</label>
<input type="email" id="email" name="email" required><br>
Create the JSP Script (insert.jsp): Create a JSP script (insert.jsp) that will handle the form
submission and insert the data into the MySQL database. Here’s a basic example:
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn =
DriverManager.getConnection("jdbc:mysql://localhost:3306/your_database", "your_username",
"your_password");
String sql = "INSERT INTO students (first_name, last_name, email) VALUES (?, ?, ?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, fname);
pstmt.setString(2, lname);
pstmt.setString(3, email);
pstmt.executeUpdate();
out.println("Record inserted successfully!");
conn.close();
} catch (Exception e) {
out.println("Error: " + e.getMessage());
}
%>
Replace the placeholders (your_username, your_password, your_database, and students) with your
actual MySQL credentials and table name.
Database Setup: Ensure that you have a MySQL server running. Create a database and a table
(e.g., students) with appropriate columns (e.g., first_name, last_name, email).
Testing: Place both the JSP form (student_form.jsp) and the JSP script (insert.jsp) in the same folder
under your server’s webapps directory (e.g., Tomcat’s webapps folder). Access the form via your
browser (e.g., http://localhost:8080/your_app/student_form.jsp). Submit data, and it should be inserted
into the database.
Experiment No: 3
OBJECTIVE: Write programs using Java script for Web Page to display browsers information.
PROGRAM:
Browser Name and Version: The following code snippet retrieves and displays the browser name and
version and also the operating system (platform):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Browser Information</title>
</head>
<body>
<h1>Browser Information</h1>
<p id="browser-info"></p>
<p id="os-info"></p>
<script>
const browserInfo = `Browser: ${navigator.appName} (Version ${navigator.appVersion})`;
document.getElementById("browser-info").textContent = browserInfo;
const osInfo = `Operating System: ${navigator.platform}`;
document.getElementById("os-info").textContent = osInfo;
</script>
</body>
</html>
This code snippet uses the navigator.appName and navigator.appVersion properties to get the browser
name and version. The navigator.platform property provides information about the user’s operating
system.
Other Browser Properties: We can diaplay additional properties from the navigator object to get more
details. For example:
Experiment No: 4
OBJECTIVE: Write a Java applet to display the Application Program screen i.e. calculator and other.
PROGRAM:
The applet allows you to perform addition, subtraction, multiplication, and division. The following
steps should be followed for simple Java applet that creates a basic calculator:
1. Create a text field to accept the expression and display the output.
2. Create buttons for digits (0-9) and a decimal point.
3. Add a button to clear the complete expression.
4. Create buttons for operations (addition, subtraction, multiplication, and division) and an equals
button to compute the result.
5. Add ActionListeners to all the buttons.
6. Compute the result and display it in the text field.
Experiment No: 5
OBJECTIVE: Writing program in XML for creation of DTD, which specifies set of rules. Create a
style sheet in CSS/ XSL & display the document in web browser.
PROGRAM:
XML File: Save your XML document with a .xml extension. For example, you could name
it document.xml.
DTD File: Save your DTD (Document Type Definition) with a .dtd extension. This file should be in
the same directory as your XML file. For example, you could name it note.dtd.
CSS File: Save your CSS (Cascading Style Sheets) with a .css extension. For example, you could
name it styles.css.
note {
font-family: Arial, sans-serif;
margin: 4px;
padding: 5px;
background-color: #f8f8f8;
border: 1px solid #ddd;
}
Displaying in a Browser: To display the XML document with the CSS styles in a browser, you
need to transform the XML document into an XHTML document using XSLT (XSL
Transformations). Unfortunately, CSS cannot be applied directly to XML documents in most
modern web browsers.
To perform step 4, you need to use XSLT (XSL Transformations) to transform your XML
document into an XHTML document. Here’s how you can do it:
XSLT File: First, you need to create an XSLT file. Save it with a .xsl extension. For example, you
could name it transform.xsl. This file will define how to transform the XML document into
XHTML.
XML
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My Note</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th style="text-align:left">To</th>
<th style="text-align:left">From</th>
</tr>
<tr>
<td><xsl:value-of select="note/to"/></td>
<td><xsl:value-of select="note/from"/></td>
</tr>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Linking the XSLT File: Next, you need to link the XSLT file in your XML document. You can do
this by adding the following line to your XML file:
XML
XML
Opening in a Browser: Now, you can open your XML file in a web browser. The browser will
apply the XSLT transformation and display the resulting XHTML document.
Experiment No: 6
OBJECTIVE: Program to illustrate JDBC connectivity. Program for maintaining database by sending
queries. Design and implement a simple servlet book query with the help of JDBC & SQL. Create MS
Access Database, Create on ODBC link, Compile & execute JAVA JDVC Socket.
PROGRAM:
JDBC Connectivity:
JDBC (Java Database Connectivity) allows Java applications to interact with databases. Here’s a
basic example of how to establish a JDBC connection to a MySQL database:
import java.sql.*;
public class FirstExample {
static final String DB_URL = "jdbc:mysql://localhost/kipm";
static final String USER = "guest";
static final String PASS = "guest123";
static final String QUERY = "SELECT id, first, last, age FROM Employees";
public static void main(String[] args) {
try (Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(QUERY)) {
while (rs.next()) {
System.out.print("ID: " + rs.getInt("id"));
System.out.print(", Age: " + rs.getInt("age"));
System.out.print(", First: " + rs.getString("first"));
System.out.println(", Last: " + rs.getString("last"));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
This code connects to a MySQL database, executes a query, and displays the results.
Search for or browse to the “ODBC Data Sources” applet (64-bit or 32-bit) in
the Start Menu or Control Panel.
Write your Java code that establishes a JDBC connection (as shown in the
first example).
OBJECTIVE: Install TOMCAT web server and APACHE. Access the above developed static web
pages for books web site, using these servers by putting the web pages developed.
PROGRAM:
1. Download Tomcat:
Choose the appropriate version (usually the latest stable version) and
download the ZIP or TAR.GZ file.
4. Start Tomcat:
5. Access Tomcat:
o Apache HTTP Server is a powerful and widely used web server. Follow these steps to
install it:
1. Download Apache:
Choose the appropriate version (usually the latest stable version) and
download the ZIP or TAR.GZ file.
3. Start Apache:
4. Access Apache:
o Place your static web pages (HTML, CSS, JavaScript, images, etc.) in the appropriate
directories:
For Apache Tomcat: Put your web pages in the webapps directory within the
Tomcat installation. Create a subdirectory (e.g., mybooks) and place your files
there.
For Apache HTTP Server: Put your web pages in the htdocs or www directory
within the Apache installation.
OBJECTIVE: Assume four users user1, user2, user3 and user4 having the passwords pwd1, pwd2,
pwd3 and pwd4 respectively. Write a servlet for doing the following. Create a Cookie and add these
four user id's and passwords to this Cookie. 2. Read the user id and passwords entered in the Login
form and authenticate with the values available in the cookies.
PROGRAM:
We’ll create a cookie containing the user IDs and passwords, and then authenticate users based on the
values stored in the cookies. Below is a example of how you can achieve this using Java servlets:
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;
import java.io.IOException;
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
// Assume user IDs and passwords
String[] userIDs = {"user1", "user2", "user3", "user4"};
String[] passwords = {"pwd1", "pwd2", "pwd3", "pwd4"};
Adjust the servlet path (@WebServlet("/LoginServlet")) and the HTML form in your JSP or HTML
file accordingly. Also, handle exceptions and edge cases as needed in a production environment.
Experiment No: 9
OBJECTIVE: Install a database Mysql. Create a table which should contain at least the following
fields: name, password, email-id, phone number Write a java program/servlet/JSP to connect to that
database and extract data from the tables and display them. Insert the details of the users who register
with the web site, whenever a new user clicks the submit button in the registration page.
PROGRAM:
In this table:
id is an auto-incremented primary key.
name, password, email, and phone_number are columns to store user information.
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
@WebServlet("/UserServlet")
public class UserServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
try {
// Load MySQL JDBC driver
Class.forName("com.mysql.cj.jdbc.Driver");
// Close resources
resultSet.close();
preparedStatement.close();
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Experiment No: 10
OBJECTIVE: Write a JSP which insert the details of the 3 or 4 users who register with the web site by
using registration form. Authenticate the user when he submits the login form using the user name and
password from the database.
PROGRAM:
1. Create a Java servlet named RegistrationHandler (you can adjust the name as needed).
2. In the servlet, handle the form submission by extracting the user input (username and password)
and inserting it into the MySQL database.
3. You’ll need to set up a MySQL database with a table (e.g., users) to store user information.
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
@WebServlet("/RegistrationHandler")
public class RegistrationHandler extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection connection = DriverManager.getConnection(dbURL, dbUser, dbPassword);