0% found this document useful (0 votes)
29 views15 pages

Assign 5

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views15 pages

Assign 5

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Web Technology Lab Manual

Assignment No. - 05
TITLE

JSP, Servlet and MySQL(Backend)

OBJECTIVES

1. Understand about basic concepts of html, CSS


2. Understand the basic functionalities of JSP
3. Having the knowledge of SQL query to create the database

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

Students will be able to,


1. Develop a dynamic webpage using JSP, HTML and Servlet.
2. Write a server side java application called Servlet to catch the data sent from client,
process it and store it on database (MySQL).
3. Write a server side java application called JSP to catch form data sent from client and
store it on database (MySQL).

SOFTWARE NEEDED

1. Any Operating System


2. JDK 7or later
3. Editors; Netbeans/Eclipse
4. Web browser
5. Tomcat 7 or later

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.

Why we need JSP?


JSP is used for the design of dynamic web page and servlet is used to code the logic that is
present i.e. in the MVC (Model-View-Controller) architecture, the servlet is the controller and
the JSP is the view.
Architecture of JSP
1. The request / response part of a JSP is defined in below architecture
2. The client initiated request for a JSP file using browser
3. Webs server (i.e, JSP Engine) invokes the JSP file and interpret the JSP file produce a
java code. The created java code will be a Servlet.
4. Once Servlet is created, JSP engine compiles the servlet. Compilation errors will be
detected in this phase.
5. Now servlet class is loaded by the container and executes it.
6. Engine sends the response back to the client.

Page 26
Web Technology Lab Manual

Figure.2: Architecture of JSP


Syntax of JSP:
JSP declarations is used to declare variables and methods as shown below,
<% text %>
Following is the simple and first example for JSP:
//Hello.jsp
<html>
<head>
<title> JSP File</title>
</head>

<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

Figure.3: Functions of Servlet

TECHNOLOGY/TOOL IN BRIEF

1. JSP and Servlets


2. IDE: NetBeans 7.0 or Later
3. Databases: MySQL

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

DESIGN / EXECUTION STEPS

Following steps are used to Create and Execute web applications,


1. Design html and jsp files with an extension of.html and .jsp
2. Write database connection page using servlet
3. Set MySQL username, password and database name in database connection page
4. Start the Tomcat Server with port number
5. Open the browser and type localhost:8084

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.

PROGRAM CODE & OUTPUT

Following pages required to run this application:


1. index.jsp
2. SignUp.jsp
3. User.jsp
4. LoginSuccess.jsp
5. LoginFailure.jsp
6. LoginServlet.jsp
7. Login.jsp

// index.jsp
<%--
Document : Login
Created on : Dec 31, 2017, 2:07:34 PM
Author : Admin --
%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>


<!DOCTYPE html>
Page 29
Web Technology Lab Manual

<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>

<center><input type="submit" name="user" value="Login"></center>


</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
--%>

<%@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 bgcolor="Skyblue">

<%
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>

<td> <strong><em><font color="#990000" size="+1" face="Times New Roman"


style="text-decoration:underline">User Registration</font></em></strong>
</td>
</tr>
</table>

<table border="black" align ="center">


<tr>

<td><form name="s" action="LoginServelt.jsp" method="get" onSubmit="return


valid()">
<table align="center" cellpadding="6" cellspacing="6" width="400" height="300">
<td>
<tr>
<td><font face="Times New Roman" size="+1"><strong>Name</strong></font></td>
<td>&nbsp;&nbsp;&nbsp;&nbsp; <input type="text" name="unn" class="b"></td>

Page 31
Web Technology Lab Manual

</tr>
<tr>
<td><font face="Times New Roman" size="+1"><strong>User ID</strong></font></td>
<td>&nbsp;&nbsp;&nbsp;&nbsp; <input type="text" name="uidd" id="name"></td>
</tr>
<tr>
<td><font face="Times New Roman"
size="+1"><strong>Password</strong></font></td>
<td>&nbsp;&nbsp;&nbsp;&nbsp; <input type="password" name="passs" class="b"></td>
</tr>
<tr>
<td><font face="Times New Roman" size="+1"><strong>Mobile</strong></font></td>
<td>&nbsp;&nbsp;&nbsp;&nbsp; <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>&nbsp;&nbsp;&nbsp;&nbsp; <input type="text" name="eidd" class="b"></td>
</tr>
<tr>
<td><font face="Times New Roman" size="+1"><strong>Date</strong></font></td>
<td>&nbsp;&nbsp;&nbsp;&nbsp; <input type="text" name="date" class="b"
value="<%=strDateNew1%>"></td>
</tr>
<tr>
<td></td>
<td>&nbsp;&nbsp;&nbsp;&nbsp; <input type="submit" name="s" value="submit"
class="b1" >
&nbsp;&nbsp;&nbsp; <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
--%>

<%@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>
<h1> User Logged In Successfully!...</h1>
</body>
</html>

//LoginFailure.jsp
<%--
Document : LoginFailure
Created on : Dec 31, 2017, 2:42:21 PM
Author : Admin
--%>

<%@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>
<h1> <font color="Red"> Please Provide Correct Username or Password!..Retry
Again!.. </font> </h1>
<table>
<tr>
<td>
<form id="user1" method="post" action="User.jsp" name="s" onSubmit="return
valid()">
<table>
<tr>
<td>
User Id</td>
<td><input type="text" name="user1"></td>
</tr>
<tr>
<td>
Password</td>

Page 33
Web Technology Lab Manual

<td><input type="password" name="pass"></td>


</tr>
<tr>
<td>
</td>
<td>
<input type="submit" name="user"
value="Login"></td> </tr></table>
</form>
</td>
</tr></table>
</body>
</html>

//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");

PreparedStatement ps=con.prepareStatement("insert into


student(sname,suserid,spass,mobile,email,date) values(?,?,?,?,?,'"+strDateNew+"')");
//ResultSet rs=ps.executeQuery();
ps.setString(1,x);
ps.setString(2,a);
ps.setString(3,b);
ps.setString(4,c);
ps.setString(5,d);

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());
}
%>

//Database Name: loginpage


//Table Name: student

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 3: Once registered click Login directly

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

You might also like