You are on page 1of 13

PART1

1. Web application that takes name and age from an HTML page. If the age is less than 18,
it should send a page with “Hello <name>, you are not authorized to visit the site”
message, where <name> should be replaced with the entered name. Otherwise it should
send “Welcome <name> to this site” message using servlet.
Ans:
index.html
<html>
<head>
<title>User Information</title>
</head>
<body>
<h4>Mishank Jain</h4>
<h4>KCTYIT016 </h4>
<form method=get action="NewServlet">
<table border=1>
<tr><th>Enter User Name :</th><td><input type=text name=t1></td>
<tr><th>Enter User Age :</th><td><input type=text name=t2></td>
</table>
<input type=submit value=submit>
</form>
</body>
</html>
NewServlet.java
package mypack;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class NewServlet extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<html><head><title>Servlet LoginServlet</title></head>");
String uname = request.getParameter("t1");
String uage = request.getParameter("t2");
int age = Integer.parseInt(uage);
if(age < 18)
{
out.println("<h4> Mishank Jain KCTYIT016");
out.println("<body bgcolor=red >");
out.println("<h1> Hello "+uname+", you are not authorized to visit the site</h1>");
}
else
{
out.println("<h4> Mishank Jain KCTYIT016");
out.println("<body bgcolor=green >");
out.println("<h1> Welcome !!! "+uname+" to this site </h1>");
}
out.println("</body></html>");}}
Output:
2. A web application that lists all cookies stored in the browser on clicking “List Cookies”
button. Add cookies if necessary.
3. A web application that takes a name as input and on submit it shows a hello <name>
page where name is taken from the request. It shows the start time at the right
top corner of the page and provides a logout button. On clicking this button, it should
show a logout page with Thank You <name > message with the duration of usage
(hint: Use session to store name and time) using jsp.

Ans:
index.html

<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<h1>Mishank Jain</h1>
<h1>KCTYIT 016</h1>
<form action="elapsedTime" method="get">
Name : <input type="text" name="name">
<br> <input type="submit" value="Enter">
</form>
</body>
</html>

elapsedTime.java

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalTime;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author Mishank
*/
public class elapsedTime extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res) throws
IOException,ServletException{
res.setContentType("text/html");
PrintWriter out = res.getWriter();
LocalDate date = LocalDate.now();
LocalTime time = LocalTime.now();
Cookie ck = new Cookie("name",req.getParameter("name"));
res.addCookie(ck);
ck = new Cookie("startDate",date.toString());
res.addCookie(ck);
ck = new Cookie("startTime",time.toString());
res.addCookie(ck);
Cookie[] cookies = req.getCookies();
out.println("<br><a href='loggedOut' style='text-align:right'><h3>Log Out</h3></a>");
out.println("Hello, " + cookies[0].getValue());
out.println(cookies[1].getValue());
out.println(cookies[2].getValue());
}
}

loggedOut.java
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import java.time.*;
import java.time.temporal.ChronoUnit;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author Mishank
*/
public class loggedOut extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res) throws
IOException,ServletException{
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("<h1>Mishank Jain</h1>");
out.println("<h1>KCTYIT 016</h1>");
LocalTime timeNow = LocalTime.now();
LocalDate dateNow = LocalDate.now();
Cookie[] cookies = req.getCookies();
LocalTime timeIn = LocalTime.parse(cookies[2].getValue());
LocalDate dateIn = LocalDate.parse(cookies[1].getValue());
out.println(cookies[0].getValue()+" Logged In at : " + cookies[1].getValue()+" " +
cookies[2].getValue());
out.println("<br>");
out.println(cookies[0].getValue()+" Logged out at : " + dateNow +" " + timeNow);
out.println("<br>Total time spent on website is : " + timeIn.until(timeNow,
ChronoUnit.SECONDS) + "seconds");
}
}
Output:
4. Write a JSP application to print the current date and time.
Ans:
CurrentDate.jsp
<%@ page import = "java.io.*,java.util.*, javax.servlet.*" %>
<html>
<head>
<title>Display Current Date & Time</title>
</head>
<body>
<center>
<h4>Mishank Jain</h4>
<h4>KCTYIT016</h4>
<h1>Display Current Date & Time</h1>
</center>
<%
Date date = new Date();
out.print( "<h2 align = \"center\">" +date.toString()+"</h2>");
%>
</body>
</html>
Output:
5. Create a table which should contain at least the following fields: name, password,
emailid, phone number Write a 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.

6. Create a table which should contain the following fields: name, password, email-
id,phone number (these should hold the data from the registration form).Write JSP to
connect to that database and extract data from the tables and display them.

Ans:
index.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type"content="text/html; charset=UTF-8">
<title> JSP Page</title>
</head>
<body>
<h3> Mishank Jain </h3>
<h3> KCTYIT016 </h3>
<h1>Choose Option</h1>
<a href="insert.jsp">Insert Record</a><p></p>
<a href="display.jsp">Display Record</a>
</body>
</html>

insert.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type"content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h3> Mishank Jain </h3>
<h3> KCTYIT016 </h3>
<form action="insertdb.jsp"method="post">
<table border="0"cellspacing="2"cellpadding="5">
<thead>
<tr>
<th colspan="2">Registration Form</th>
</tr>
</thead>
<tbody>
<tr>
<td><label>Name</label></td>
<td><input type="text" name="pname"/></td>
</tr>

<tr>
<td><label>Password</label></td>
<td><input type="password" name="pass"/></td>

</tr>
<tr>

<td><label>Email id</label></td>
<td><input type="text" name="qty"/></td>

</tr>
<tr>

<td><label>Phone number</label></td>
<td><input type="text" name="pno"/></td>

</tr>
<tr>

<td><input type="submit"value="Save"/></td>
<td><input type="reset"value="reset"/></td>

</tr>
</tbody>
</table>
</form>
<font color="red"><c:if test="${not empty param.errMsg}">
<c:out value="${param.errMsg}"/>
<a href="index.jsp">Go Back</a>
</c:if></font>
<font color="green"><c:if test="${not empty param.susMsg}">
<c:out value="${param.susMsg}"/>
<a href="index.jsp">Go Back</a>
</c:if></font>
</body>
</html>

insertdb.jsp

<%@ page import="java.io.*,java.util.*,java.sql.*"%>


<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
<html>
<head>
<title>JINSERT Operation</title>
</head>
<body>
<h3> Mishank Jain </h3>
<h3> KCTYIT016 </h3>
<c:if test="${ empty param.pname or empty param.qty or empty param.pno or empty
param.pass}">
<c:redirect url="insert.jsp">
<c:param name="errMsg"value="Please Enter All The Details"/>
</c:redirect>
</c:if>
<sql:setDataSource var="dbsource"driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost/sampleDB"
user="root" password=""/>

<sql:update dataSource="${dbsource}"var="result">
INSERT INTO registrationform(name, password, emailid, phonenumber) VALUES (?,?,?,?);
<sql:param value="${param.pname}"/>
<sql:param value="${param.pass}"/>
<sql:param value="${param.qty}"/>
<sql:param value="${param.pno}"/>
</sql:update>

<c:if test="${result>=1}">
<font size="5"color='green'> Congratulations ! Data inserted
successfully.</font>
<c:redirect url="insert.jsp">
<c:param name="susMsg" value="Congratulations ! Data inserted
successfully." />
</c:redirect>
</c:if>

</body>
</html>

display.jsp
<%@ page import="java.io.*,java.util.*,java.sql.*"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
<html>
<head>
<title>SELECT Operation</title>
<script>
function confirmGo(m,u) {
if ( confirm(m) ) {
window.location = u;
}
}
</script>
</head>
<body>
<h3> Mishank Jain </h3>
<h3> KCTYIT016 </h3>
<sql:setDataSource var="dbsource"driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost/sampleDB"
user="root" password=""/>
<sql:query dataSource="${dbsource}"var="result">
SELECT * from registrationform;
</sql:query>
<center>
<form>
<table border="1"width="40%">
<caption>Personal Details</caption>
<tr>
<th>Name</th>
<th>password</th>
<th>emailid</th>
<th>phone number</th>

</tr>
<c:forEach var="row"items="${result.rows}">
<tr>
<td><c:out value="${row.name}"/></td>
<td><c:out value="${row.password}"/></td>
<td><c:out value="${row.emailid}"/></td>
<td><c:out value="${row.phonenumber}"/></td>
</tr>
</c:forEach>
</table>
</form>
<a href="index.jsp">Go Home</a>
</center>
</body>
</html>

Output:

You might also like