Lab Manual WT
Lab Manual WT
(KCS-652)
LABORATORY MANUAL
FOR
Bachelor of Technology
In
Computer Science and Engineering
Session: 2023-2024
3. Write programs using Java script for Web Page to display browsers information.
4. Write a Java applet to display the Application Program screen i.e. calculator and other.
5. Writing program in XML for creation of DTD, which specifies set of rules.
7. Install APACHE TOMCAT web server. Access the static web pages for login id, using this server.
8. Create a Cookie and add four user id’s and passwords to this Cookie.
9. Write a java program/servlet/JSP to connect database and extract data from the tables and display them
10. Write a JSP which insert the details of the 3 or 4 users who register with the web site by using registration
form.
11. Design and implement a simple shopping cart example with session tracking API.
EXPERIMENT NO.-01
<html>
<head>
<title>registartion form</title>
<style>
th, td {
</style>
</head>
<body>
<form>
<strong>First name:</strong><br>
<br>
<strong>Last name:</strong><br>
<br>
<strong>Sex:</strong><br>
<hr>
<strong>Username:</strong><br>
<br>
<strong>Password:</strong><br>
<input type="Password" name="password">
<br>
<hr>
<table style="width:100%">
<tr>
<th>Course</th>
<th>Marks Obtained(%)</th>
<th>Institute Name</th>
</tr>
<tr>
<td>10th</td>
<td>95%</td>
</tr>
<tr>
<td>12th</td>
<td>91%</td>
</tr>
<tr>
<td>Graduation</td>
<td>Persuing</td>
<td>AKTU</td>
</tr>
</table>
<hr>
<strong>Address:</strong><br>
<br>
<hr>
<strong>Elective Subject:</strong><br>
<hr>
<strong>Comment:</strong>
<hr>
<br>
<hr>
</form>
</body>
</html>
OUTPUT:
EXPERIMENT NO.-03
Write programs using Java script for Web Page to display browsers information.
<!DOCTYPE html>
<html>
<body>
<p>Do not rely on it! Most browsers returns "Gecko" as product name!</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = "navigator.product is " + navigator.product;
document.getElementById("demo").innerHTML = navigator.appVersion;
document.getElementById("demo").innerHTML = navigator.userAgent;
document.getElementById("demo").innerHTML = navigator.platform;
document.getElementById("demo").innerHTML = navigator.language;
document.getElementById("demo").innerHTML = navigator.onLine;
document.getElementById("demo").innerHTML = navigator.javaEnabled();
</script>
</body>
</html>
OUTPUT:
The Navigator Object
Write a Java applet to display the Application Program screen i.e. calculator and other.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="Cal" width=300 height=300>
</applet>
*/
b[i]=new Button(""+i);
}
add=new Button("add");
sub=new Button("sub");
mul=new Button("mul");
div=new Button("div");
mod=new Button("mod");
clear=new Button("clear");
EQ=new Button("EQ");
t1.addActionListener(this);
add(t1);
for(int i=0;i<10;i++)
{
add(b[i]);
}
add(add);
add(sub);
add(mul);
add(div);
add(mod);
add(clear);
add(EQ);
for(int i=0;i<10;i++)
{
b[i].addActionListener(this);
}
add.addActionListener(this);
sub.addActionListener(this);
mul.addActionListener(this);
div.addActionListener(this);
mod.addActionListener(this);
clear.addActionListener(this);
EQ.addActionListener(this);
}
v1=Integer.parseInt(t1.getText());
OP='*';
t1.setText("");
}
else if(str.equals("div"))
{
v1=Integer.parseInt(t1.getText());
OP='/';
t1.setText("");
}
else if(str.equals("mod"))
{
v1=Integer.parseInt(t1.getText());
OP='%';
t1.setText("");
}
if(str.equals("EQ"))
{
v2=Integer.parseInt(t1.getText());
if(OP=='+')
result=v1+v2;
else if(OP=='-')
result=v1-v2;
else if(OP=='*')
result=v1*v2;
else if(OP=='/')
result=v1/v2;
else if(OP=='%')
result=v1%v2;
t1.setText(""+result);
}
if(str.equals("clear"))
{
t1.setText("");
}
}
OUTPUT:
EXPERIMENT NO.-05
Writing program in XML for creation of DTD, which specifies set of rules
<?xml version="1.0"?>
<!DOCTYPE student[
<!ELEMENT student (name,address,std,marks)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT address (#PCDATA)>
<!ELEMENT std (#PCDATA)>
<!ELEMENT marks (#PCDATA)>
]>
<student>
<name>Harshit Jaiswal</name>
<address>Bareilly</address>
<marks>94</marks>
</student>
OUTPUT:
EXPERIMENT NO.-06
Statement st = con.createStatement();
int m = st.executeUpdate(sql);
if (m == 1)
System.out.println("inserted successfully : "+sql);
else
System.out.println("insertion failed");
con.close();
}
catch(Exception ex)
{
System.err.println(ex);
}
}}
OUTPUT:
EXPERIMENT NO.-07
Install APACHE TOMCAT web server. Access the static web pages for login id, using this
server.
<body>
<h2 align="center">Sign in</h2>
<form method="get" action="http://localhost:8888/india/Login.jsp">
<h3>
Enter ID <input type="text" name="t1"> <br>
Enter Password <input type="password" name="t2"> <br>
<input type="submit" value="Sign in">
</h3>
</body>
In the action attribute Login.jsp file is given to invoke. Assume localhost is the server. The
port number on which Tomcat is running is 8888. india is the folder created in Tomcat. The
attribute method can be given two values of either get or post; here, get is given.
Following is the server-side JSP file that includes a Scriptlet to understand what and how to
code Scriptlet.
File Name: Login.jsp
<body>
<h2 align="center">Log in Validation </h2>
<%
String str1=request.getParameter("t1");
String str2=request.getParameter("t2");
%>
</body>
OUTPUT:
FirstServlet.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
try{
response.setContentType("text/html");
String n=request.getParameter("userName");
out.print("Welcome "+n);
out.print("<form action='servlet2'>");
out.print("</form>");
out.close();
}catch(Exception e){System.out.println(e);}
}
SecondServlet.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
response.setContentType("text/html");
PrintWriter out = response.getWriter();
Cookie ck[]=request.getCookies();
out.print("Hello "+ck[0].getValue());
out.close();
}catch(Exception e){System.out.println(e);}
}
web.xml
<web-app>
<servlet>
<servlet-name>s1</servlet-name>
<servlet-class>FirstServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>s1</servlet-name>
<url-pattern>/servlet1</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>s2</servlet-name>
<servlet-class>SecondServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>s2</servlet-name>
<url-pattern>/servlet2</url-pattern>
</servlet-mapping>
</web-app>
OUTPUT:
EXPERIMENT NO.-09:
Write a java program/servlet/JSP to connect database and extract data from the tables
and display them.
Retrieve.jsp
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%
String id = request.getParameter("userid");
String driver = "com.mysql.jdbc.Driver";
String connectionUrl = "jdbc:mysql://localhost:3306/";
String database = "test";
String userid = "root";
String password = "";
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
%>
<!DOCTYPE html>
<html>
<body>
</tr>
<%
try{
connection = DriverManager.getConnection(connectionUrl+database, userid, password);
statement=connection.createStatement();
String sql ="select * from users";
resultSet =statement.executeQuery(sql);
while(resultSet.next()){
%>
<tr>
<td><%=resultSet.getString("first_name") %></td>
<td><%=resultSet.getString("last_name") %></td>
<td><%=resultSet.getString("city_name") %></td>
<td><%=resultSet.getString("email") %></td>
</tr>
<%
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
%>
</table>
</body>
</html>
After retrieve the data from the data base the table look like this.
:
EXPERIMENT NO.-10
Write a JSP which insert the details of the 3 or 4 users who register with the web site by
using registration form.
In this example, we are going to take "Guru Registration form" which has the following
fields:
1. First Name
2. Last Name
3. Username
4. Password
5. Address
6. Contact Number
7. After filling all these details we have submit button, on click of that button all
the details will be stored.
Register_1.jsp
<td>Last Name</td>
<tr>
<td>UserName</td>
<td><input type="text" name="username" /></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="password"
/></td>
</tr>
<tr>
<td>Address</td>
</tr> <td><input type="text" name="address" /></td>
<tr>
<td>Contact No</td>
<td><input type="text" name="contact" /></td>
</tr></table>
<input type="submit" value="Submit" /></form>
</body>
</html>
Explanation of the code:
Code Line 11: Here we are taking a form name which has action i.e. the servlet to which
the request will be processed and servlet name is guru_register.java. The request will be
processed through POST method.
Code Line 14-16: Here we are taking input type as text and name is first name
Code Line 18-20: Here we are taking input type as text and name is last name
Code Line 22-24: Here we are taking input type as text and name is username
Code Line 26-28: Here we are taking input type as password(this will hide the password
when typed) and name as password
Code Line 30-32: Here we are taking input type as text and name as address
Code Line 34-36: Here we are taking input type as text and name as contact
Code Line 37: Here we are taking a button of type submit and value is also submit. On
click of this button the action will go to corresponding guru_register servlet where all the
parameter values will be passed in the request.
Guru_register.java
package demotest;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
RequestDispatcher req =
request.getRequestDispatcher("register_1.jsp");
req.include(request, response);
}
else
{
Explanation of code:
Code Line 14: Here we defining guru_servlet which is extending HttpServlet.
Code Line 18: This action doPost() method which will be called when we mention POST
in action attribute in the above JSP form.
Code Line 20-25:Here we are fetching the values from request i.efirst_name, last_name ,
username, password, address and contact using request.getParameter.
Code Line 27-32: Here we are taking if condition where we check any of the parameters
which are fetched from request as whether they are empty or not. If any of the parameter is
empty then it will enter this condition ( first_name.isEmpty() || last_name.isEmpty ||
username.isEmpty || password.isEmpty || address.isEmpty || contact.isEmpty()) and we
have to fetch RequestDispatcher object using request object which will forward request to
register_1.jsp. Here we are also including request and response objects.
Code Line 33-37: This case will execute when any of the parameter is not empty .We will
have to fetch requestDispatcher object using request object which will forward request to
register_2.jsp.Here we are forwarding request and response objects.
Register_2.jsp
Output:
When we click on register_1.jsp, we will get a form which will have details like first
name, last name, username, password, address, contact. All the details have been filled.
When we click on submit button then we get the message as "Welcome User"
EXPERIMENT NO.-11
Design and implement a simple shopping cart example with session tracking API.
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("<HTML><HEAD><TITLE>SessionTracker</TITLE></HEAD>");
out.println("<BODY><H1>Session Tracking Demo</H1>");
out.println("</BODY></HTML>");
}
OUTPUT:
SessionTracker
Session Tracking Demo
currently have the following items in your cart:
APPLE
BANANA
MANGO
Would you like to Add More Items
Check Out