hey..i have a login page where different users first registered and then after they can login.
my question is how a user can see only his data after login where different users data are stored in database, plz...provide me answer its very urgent.
The given code accepts username and password to check whether the user is valid or not. If the user is valid, whole record of that particular user will get retrieved from the database and displayed it on the welcome page. Apart from this if user is not registered, then he or she can register himself first and then login.
1)login.jsp:
<html> <script> function validate(){ var username=document.form.user.value; var password=document.form.pass.value; if(username==""){ alert("Enter Username!"); return false; } if(password==""){ alert("Enter Password!"); return false; } return true; } </script> <form name="form" method="post" action="check.jsp" onsubmit="javascript:return validate();"> <table> <tr><td>Username:</td><td><input type="text" name="user"></td></tr> <tr><td>Password:</td><td><input type="password" name="pass"></td></tr> <tr><td></td><td><input type="submit" value="Login"></td></tr> <tr><td></td><td><a href="register.jsp">New User: Register Here</a></td></tr> </table> </form> </html>
2)check.jsp:
<%@page import="java.sql.*"%> <% try{ String user=request.getParameter("user"); String pass=request.getParameter("pass"); Class.forName("com.mysql.jdbc.Driver").newInstance(); Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root"); Statement st=con.createStatement(); ResultSet rs=st.executeQuery("select * from register where username='"+user+"' and password='"+pass+"'"); int count=0; while(rs.next()){ count++; } if(count>0){ out.println("welcome "+user); %> <table> <tr><td>First Name:</td><td><input type="text" value="<%=rs.getString("firstname")%>"></td></tr> <tr><td>Last Name:</td><td><input type="text" value="<%=rs.getString("lastname")%>"></td></tr> <tr><td>User Name:</td><td><input type="text" value="<%=rs.getString("username")%>"></td></tr> <tr><td>Date Of Birth</td><td><input type="text" value="<%=rs.getString("dob")%>"></td></tr> <tr><td>Age:</td><td><input type="text" value="<%=rs.getInt("age")%>"></td></tr> <tr><td>Email</td><td><input type="text" value="<%=rs.getString("email")%>"></td></tr> <tr><td>Address:</td><td><input type="text" value="<%=rs.getString("address")%>"></td></tr> <tr><td>Country</td><td><input type="text" value="<%=rs.getString("country")%>"></td></tr> <tr><td>State:</td><td><input type="text" value="<%=rs.getString("state")%>"></td></tr> <tr><td>City</td><td><input type="text" value="<%=rs.getString("city")%>"></td></tr> <tr><td>Telephone No:</td><td><input type="text" value="<%=rs.getInt("telephone")%>"></td></tr> <tr><td>Mobile:</td><td><input type="text" value="<%=rs.getInt("mobile")%>"></td></tr> </table> <% } else{ response.sendRedirect("login.jsp"); } } catch(Exception e){ System.out.println(e); } %>
continue..
3)register.jsp:
<html> <form method="post" action="insert.jsp"> <table> <tr><td>First Name:</td><td><input type="text" name="fname"></td></tr> <tr><td>Last Name:</td><td><input type="text" name="lname"></td></tr> <tr><td>User Name:</td><td><input type="text" name="uname"></td></tr> <tr><td>Password:</td><td><input type="password" name="pass"></td></tr> <tr><td>Confirm Password:</td><td><input type="password" name="cpass"></td></tr> <tr><td>Date Of Birth</td><td><input type="text" name="dob"></td></tr> <tr><td>Age:</td><td><input type="text" name="age"></td></tr> <tr><td>Email</td><td><input type="text" name="email"></td></tr> <tr><td>Address:</td><td><input type="text" name="address"></td></tr> <tr><td>Country</td><td><input type="text" name="country"></td></tr> <tr><td>State:</td><td><input type="text" name="state"></td></tr> <tr><td>City</td><td><input type="text" name="city"></td></tr> <tr><td>Telephone No:</td><td><input type="text" name="tno"></td></tr> <tr><td>Mobile:</td><td><input type="text" name="mobile"></td></tr> <tr><td></td><td><input type="submit" value="Submit"></td></tr> </table> </form> </html>
4)insert.jsp:
<%@page import="java.sql.*,java.util.*"%> <% String fname=request.getParameter("fname"); String lname=request.getParameter("lname"); String uname=request.getParameter("uname"); String pass=request.getParameter("pass"); String cpass=request.getParameter("cpass"); String dob=request.getParameter("dob"); int age=Integer.parseInt(request.getParameter("age")); String email=request.getParameter("email"); String address=request.getParameter("address"); String country=request.getParameter("country"); String state=request.getParameter("state"); String city=request.getParameter("city"); int telephone=Integer.parseInt(request.getParameter("tno")); int mobile=Integer.parseInt(request.getParameter("mobile")); try{ Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/roseindia", "root", "root"); Statement st=con.createStatement(); int i=st.executeUpdate("insert into register(firstname,lastname,username,password,confirm_pass,dob,age,email,address,country,state,city,telephone,mobile) values('"+fname+"','"+lname+"','"+uname+"','"+pass+"','"+cpass+"','"+dob+"',"+age+",'"+email+"','"+address+"','"+country+"','"+state+"','"+city+"',"+telephone+","+mobile+")"); out.println("Data is successfully inserted!"); response.sendRedirect("login.jsp"); } catch(Exception e){ System.out.print(e); e.printStackTrace(); } %>
Ads