0% found this document useful (0 votes)
34 views4 pages

TP JSP MySql Et Sessions

Uploaded by

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

TP JSP MySql Et Sessions

Uploaded by

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

Tp JSP MySql et Sessions

Also you can learn about session handling in jsp.

1
2 CREATE TABLE `members` (
`id` int(10) unsigned NOT NULL auto_increment,
3 `first_name` varchar(45) NOT NULL,
4 `last_name` varchar(45) NOT NULL,
5 `email` varchar(45) NOT NULL,
6 `uname` varchar(45) NOT NULL,
`pass` varchar(45) NOT NULL,
7 `regdate` date NOT NULL,
8 PRIMARY KEY (`id`)
9 ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
10
index.jsp

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


<!DOCTYPE html>
2
<html>
3 <head>
4 <meta http-equiv="Content-Type" content="text/html;
5 charset=UTF-8">
6 <title>JSP Example</title>
</head>
7 <body>
8 <form method="post" action="login.jsp">
9 <center>
10 <table border="1" width="30%" cellpadding="3">
11 <thead>
<tr>
12 <th colspan="2">Login Here</th>
13 </tr>
14 </thead>
15 <tbody>
<tr>
16 <td>User Name</td>
17 <td><input type="text" name="uname" value=""
18 /></td>
19 </tr>
20 <tr>
<td>Password</td>
21 <td><input type="password" name="pass" value=""
22 /></td>
23 </tr>
24 <tr>
25 <td><input type="submit" value="Login" /></td>
<td><input type="reset" value="Reset" /></td>
26 </tr>
27 <tr>
28 <td colspan="2">Yet Not Registered!! <a
29 href="reg.jsp">Register Here</a></td>
</tr>
30 </tbody>
31 </table>
32 </center>
33
34
</form>
35 </body>
36 </html>
37
38
reg.jsp

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


<!DOCTYPE html>
2
<html>
3 <head>
4 <meta http-equiv="Content-Type" content="text/html;
5 charset=UTF-8">
6 <title>Registration</title>
</head>
7 <body>
8 <form method="post" action="registration.jsp">
9 <center>
10 <table border="1" width="30%" cellpadding="5">
11 <thead>
<tr>
12 <th colspan="2">Enter Information Here</th>
13 </tr>
14 </thead>
15 <tbody>
<tr>
16 <td>First Name</td>
17 <td><input type="text" name="fname" value=""
18 /></td>
19 </tr>
20 <tr>
<td>Last Name</td>
21 <td><input type="text" name="lname" value=""
22 /></td>
23 </tr>
24 <tr>
25 <td>Email</td>
<td><input type="text" name="email" value=""
26 /></td>
27 </tr>
28 <tr>
29 <td>User Name</td>
<td><input type="text" name="uname" value=""
30 /></td>
31 </tr>
32 <tr>
33 <td>Password</td>
34 <td><input type="password" name="pass"
35 value="" /></td>
</tr>
36 <tr>
37 <td><input type="submit" value="Submit"
38 /></td>
39 <td><input type="reset" value="Reset" /></td>
40
41 </tr>
<tr>
42 <td colspan="2">Already registered!! <a
43 href="index.jsp">Login Here</a></td>
44 </tr>
45 </tbody>
46 </table>
</center>
47 </form>
48 </body>
49 </html>
50
registration.jsp

1
2 <%@ page import ="java.sql.*" %>
3 <%
4 String user = request.getParameter("uname");
5 String pwd = request.getParameter("pass");
String fname = request.getParameter("fname");
6 String lname = request.getParameter("lname");
7 String email = request.getParameter("email");
8 Class.forName("com.mysql.jdbc.Driver");
9 Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/dbname",
10 "root", "dbpass");
Statement st = con.createStatement();
11 //ResultSet rs;
12 int i = st.executeUpdate("insert into members(first_name, last_name, email, uname,
13 + "','" + email + "','" + user + "','" + pwd + "', CURDATE())");
14 if (i > 0) {
//session.setAttribute("userid", user);
15 response.sendRedirect("welcome.jsp");
16 // out.print("Registration Successfull!"+"<a href='index.jsp'>Go to Login</a>");
17 } else {
18 response.sendRedirect("index.jsp");
19 }
%>
20
21
welcome.jsp

1 Registration is Successful.
2 Please Login Here <a href='index.jsp'>Go to Login</a>
login.jsp

1 <%@ page import ="java.sql.*" %>


<%
2
String userid = request.getParameter("uname");
3 String pwd = request.getParameter("pass");
4 Class.forName("com.mysql.jdbc.Driver");
5 Connection con =
6 DriverManager.getConnection("jdbc:mysql://localhost:3306/dbname",
"root", "dbpass");
7 Statement st = con.createStatement();
8 ResultSet rs;
9 rs = st.executeQuery("select * from members where uname='" +
10 userid + "' and pass='" + pwd + "'");
11 if (rs.next()) {
12 session.setAttribute("userid", userid);
13 //out.println("welcome " + userid);
//out.println("<a href='logout.jsp'>Log out</a>");
14 response.sendRedirect("success.jsp");
15 } else {
16 out.println("Invalid password <a href='index.jsp'>try
17 again</a>");
18 %> }
19
success.jsp

1
<%
2 if ((session.getAttribute("userid") == null) ||
3 (session.getAttribute("userid") == "")) {
4 %>
5 You are not logged in<br/>
<a href="index.jsp">Please Login</a>
6 <%} else {
7 %>
8 Welcome <%=session.getAttribute("userid")%>
9 <a href='logout.jsp'>Log out</a>
10 <%
}
11 %>
12
logout.jsp

1 <%
2 session.setAttribute("userid", null);
3 session.invalidate();
4 response.sendRedirect("index.jsp");
%>
5
Screenshots:

You might also like