Assign 5
Assign 5
Assignment No. - 05
TITLE
OBJECTIVES
PROBLEM STATEMENTS
1. Design and Build Student Login Page using JSP, Servlet and MySQL.
2. Design and Build Employee Login Page using JSP, Servlet and MySQL.
.
OUTCOMES
SOFTWARE NEEDED
Page 25
Web Technology Lab Manual
THEORY - CONCEPT
Java Server Pages (JSP): It is a server side programming technology that is used to create
dynamic web-based applications. JSP have right to use the complete Java APIs, including the
JDBC API to access the databases.
It is a technology that helps software developers to create dynamic web pages based on HTML,
XML and other document types. It was released in 1999 by Sun Microsystems. It is just like a
PHP and ASP, but it uses the Java programming language.
A JSP element is a type of java servlet that is designed to accomplish the role of a user interface
for a java web application. Web developers write JSPs as text files that combine HTML or
XHTML code, XML elements, and rooted JSP actions and commands.
Using JSP, you can collect input from users through webpage forms, current records from a
database or another source and create web pages dynamically.
JSP tags can be used for different purposes, such as retrieving information from a database or
registering user preferences, accessing JavaBeans components, passing control between pages,
and sharing information between requests, pages etc.
Page 26
Web Technology Lab Manual
<body>
<%
out.println("Welcome to JSP Class");
%>
</body>
</html>
Output:
Welcome to JSP Class
Servlet:
A Servlet is a server side program and written in Java. Servlet is a web component that is
deployed on the server for creating the dynamic web pages. A Java servlet is a Java program that
extends the capabilities of a server. Although servlets can respond to any types of requests, they
most commonly execute applications hosted on Web servers.
Page 27
Web Technology Lab Manual
TECHNOLOGY/TOOL IN BRIEF
NetBeans: NetBeans is an IDE, used for quickly and easily developing java desktop, mobile,
and web applications, as well as HTML5 applications with HTML, JavaScript, and CSS. Also
provides a huge set of tools for PHP and C/C++ developers. It is free and open source tool and
has a great community of users and developers around the world.
MySQL: MySQL is a freely available open source Relational Database Management System
(RDBMS). It uses the Structured Query Language (SQL).
SQL is the most popular language for adding, accessing and managing data in a database. It is
most noted for its quick processing, proven reliability, ease and flexibility of use. MySQL is an
essential part of almost every open source PHP application. Good examples for PHP & MySQL-
based scripts are WordPress, Joomla, Magento and Drupal.
Page 28
Web Technology Lab Manual
TEST CASES
Manual testing is used to validate the fields like username, password, mobile number and email
id’s of the users entered by user with the database.
CONCLUSION / ANALYSIS
Hence, we have performed the dynamic web application using JSP, Servlet and MySQL.
// index.jsp
<%--
Document : Login
Created on : Dec 31, 2017, 2:07:34 PM
Author : Admin --
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-
8"> <title> Login </title>
</head>
<body>
<table>
<tr>
<td width="300"></td>
<td> <font color="#339900" size="3.5">User Login</font>
<table>
<tr>
<td>
<form id="user1" method="post" action="User.jsp" name="s" onSubmit="return
valid()">
<table border="2.0">
<tr>
<td>
User Id</td>
<td><input type="text" name="user1"></td>
</tr>
<tr>
<td>
Password</td>
<td><input type="password" name="pass"></td>
</tr>
<tr>
<td>
</tr></table>
<center><table>
<tr>
<td>
<a href="SignUp.jsp"><font color="#000000" size="2"><b>New
User?</b></font></a></td></tr></table>
</td>
</tr></table></center>
</td>
</tr>
</table>
</form>
</body>
Page 30
Web Technology Lab Manual
</html>
//SignUp.jsp
<%@ page import="java.sql.*"%>
<%@ page import =
"java.util.Date,java.text.SimpleDateFormat,java.text.ParseException"%> <%--
Document : SignUp
Created on : Dec 30, 2017, 11:52:03 PM
Author : Admin
--%>
<%
java.util.Date now = new java.util.Date();
String DATE_FORMAT1 = "dd/MM/yyyy";
SimpleDateFormat sdf1 = new SimpleDateFormat(DATE_FORMAT1);
String strDateNew1 = sdf1.format(now);
%>
<table border="black" bgcolor=" black" align ="center" >
<tr>
Page 31
Web Technology Lab Manual
</tr>
<tr>
<td><font face="Times New Roman" size="+1"><strong>User ID</strong></font></td>
<td> <input type="text" name="uidd" id="name"></td>
</tr>
<tr>
<td><font face="Times New Roman"
size="+1"><strong>Password</strong></font></td>
<td> <input type="password" name="passs" class="b"></td>
</tr>
<tr>
<td><font face="Times New Roman" size="+1"><strong>Mobile</strong></font></td>
<td> <input type="text" name="mobb" class="b"></td>
</tr>
<tr>
<td><font face="Times New Roman" size="+1"><strong>Email
ID</strong></font></td>
<td> <input type="text" name="eidd" class="b"></td>
</tr>
<tr>
<td><font face="Times New Roman" size="+1"><strong>Date</strong></font></td>
<td> <input type="text" name="date" class="b"
value="<%=strDateNew1%>"></td>
</tr>
<tr>
<td></td>
<td> <input type="submit" name="s" value="submit"
class="b1" >
<input type="reset" name="r" value="clear"
class="b1"></td> </tr></td>
<td></td>
</table>
</form></td>
</tr></table>
<center> <table><tr><td> <a href="index.jsp"><font color="#000000" size="2"><b>Login
Here</b></font></a>
</td>
</tr></table></center>
</body>
</html>
//LoginSuccess.jsp
<%--
Document : LoginSuccess
Created on : Dec 31, 2017, 2:40:04 PM
Page 32
Web Technology Lab Manual
Author : Admin
--%>
//LoginFailure.jsp
<%--
Document : LoginFailure
Created on : Dec 31, 2017, 2:42:21 PM
Author : Admin
--%>
Page 33
Web Technology Lab Manual
//LoginServlet.jsp
<%@page import="java.sql.*"%>
<%@ page import="java.io.*"%>
<%@ page import = "java.util.Date,java.text.SimpleDateFormat,java.text.ParseException"%>
<%
String a = request.getParameter("uidd");
String x = request.getParameter("unn");
String b = request.getParameter("passs");
String c = request.getParameter("mobb");
String d = request.getParameter("eidd");
session.setAttribute("d",d);
//String f=request.getParameter("date");
//out.print(strDateNew1);
//out.print(x);
//out.print(b);
//out.print(c);
//out.print(d);
//out.print(f);
java.util.Date now = new java.util.Date();
String DATE_FORMAT = "yyyy-MM-dd";
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
String strDateNew = sdf.format(now) ;
//response.sendRedirect("signup.jsp?message=success"); //String
userid=null;
//String m="avl";
try
Page 34
Web Technology Lab Manual
Class.forName("com.mysql.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/loginpage","root","root");
ps.executeUpdate();
response.sendRedirect("SignUp.jsp?success");
}
catch(Exception e1)
{
out.println(e1.getMessage());
response.sendRedirect("SignUp.jsp?Failure");%>
}
%>
//User.jsp
<%@ page import="java.sql.*;"%>
<%
String a=request.getParameter("user1");
String b=request.getParameter("pass");
String id=null,name=null,userid=null,email=null;
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/loginpage","root","root");
//Connection con = databasecon.getconnection();
PreparedStatement ps=con.prepareStatement("select sid,sname,suserid,email from student where
suserid='"+a+"' && spass='"+b+"'");
ResultSet rs=ps.executeQuery();
Page 35
Web Technology Lab Manual
if(rs.next())
{
id=rs.getString("sid");
name=rs.getString("sname");
userid=rs.getString("suserid");
email=rs.getString("email");
session.setAttribute("sid",id);
session.setAttribute("sname",name);
session.setAttribute("suserid",userid);
session.setAttribute("email",email);
//response.sendRedirect("user5.jsp");
response.sendRedirect("LoginSuccess.jsp?Success");
//out.print(name2);
}
else
{
response.sendRedirect("LoginFailure.jsp?Failure");
}
}
catch(Exception e2){
out.println(e2.getMessage());
}
%>
Page 36
Web Technology Lab Manual
Output:
Step 1: Run index.jsp page
Step 2: If user is already registered then use own credentials else click New User for registration
Page 37
Web Technology Lab Manual
Step 4: If user gives wrong username and password, it will show error message
Page 38
Web Technology Lab Manual
ORAL QUESTIONS
1. What is JSP?
2. What is Servlet?
3. What is the purpose of MySQL?
4. What is database?
5. What is the syntax of JSP?
6. How do we connect JSP file to database?
Page 39