Sample JSP Project- Registration and Login functionality
1) Download and install Eclipse.
2) Create a new Dynamic Project in Eclipse.
Steps:
1) File -> New -> Dynamic Web Project
2) Enter Project name
3) Click on Finish
3) Add the following jsp and html files in "WebContent" directory.
i) index.jsp
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSP Sample</title>
</head>
<body>
<a href="Login.html">Login</a>
</br>
</br>
<a href="Reg.html">Register</a>
</body>
</html>
ii) Login.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSP Login Page</title>
</head>
<body>
<form action="login.jsp" method="post">
User name :<input type="text" name="user" /></br></br>
password :<input type="password" name="password" /></br></br>
<input type="submit" />
</form>
</body>
</html>
iii) login.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSP login</title>
</head>
<body>
<%@ page import="java.sql.*"%>
<%@ page import="javax.sql.*"%>
<%
String username=request.getParameter("user");
session.putValue("username",username);
String pwd=request.getParameter("password");
Class.forName("com.mysql.jdbc.Driver");
java.sql.Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jsptest","root","12345");
Statement st= con.createStatement();
ResultSet rs=st.executeQuery("select * from jspusers where user='"+username+"'");
if(rs.next())
{
if(rs.getString(2).equals(pwd))
{
out.println("welcome "+username);
}
else
{
out.println("Invalid password try again");
}
}
else
%>
<a href="index.html">Home</a>
</body>
</html>
iv) Reg.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSP Registration</title>
</head>
<body>
<form action="reg.jsp" method="post">
User Name :<input type="text" name="username" /><br/><br/>
Password :<input type="password" name="password" /><br/><br/>
First Name :<input type="text" name="fname" /><br/><br/>
Last Name :<input type="text" name="lname" /><br/><br/>
Email :<input type="text" name="email" /><br/><br/>
<br/><br/>
<input type="submit" />
</form>
</body>
</html>
v) reg.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSP Registration</title>
</head>
<body>
<%@ page import ="java.sql.*" %>
<%@ page import ="javax.sql.*" %>
<%
String user=request.getParameter("username");
session.putValue("userid",user);
String pwd=request.getParameter("password");
String fname=request.getParameter("fname");
String lname=request.getParameter("lname");
String email=request.getParameter("email");
Class.forName("com.mysql.jdbc.Driver");
java.sql.Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jsptest","root","12345");
Statement st= con.createStatement();
ResultSet rs;
int i=st.executeUpdate("insert into jspusers values ('"+user+"','"+pwd+"','"+fname+"', '"+lname+"','"+email+"')");
out.println("Registered");
%>
<a href ="Login.html">Login</a><br/><br/>
<a href="index.html">Home</a>
</body>
</html>
4) Add the "mysql-connector jar" file in the directory WebContent -> WEB-INF -> lib
5) Create a table named "jspusers" in the database "jsptest".
create table jspusers (user varchar(20),
password varchar(15),
fname varchar(15),
lname varchar(15),
email varchar(20)
);
6) Add Tomcat server in the eclipse to run the Project.
How to add Tomcat?
No comments:
Post a Comment