Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa JSP-Servlet how to create a login page and registration page?

Have Programming Question? Ask it here!
 
 
 


Bhagirath Singh
how to create a login page and registration page?
2 Answer(s)      2 years and 11 months ago
Posted in : JSP-Servlet


hellow, pls tell me the code for how we can create a login page and registration page and how we can store the info in database.only in advance java as jsp.


Advertisement
View Answers

May 30, 2011 at 10:53 AM


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<a></td></tr>

</table>
</form>
</html>

2)check.jsp:

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

<%
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 login where username='"+user+"' and password='"+pass+"'");
int count=0;
          while(rs.next())
          {

                   count++;
          }

                    if(count>0)
          {
            out.println("welcome "+user);
          }
          else
          {
                       response.sendRedirect("login.jsp");
          }
%>

3)register.jsp:

<html>

<form name="form" method="post" action="insertdata.jsp">
<table>
<tr><td>Enter First Name</td><td><input type="text" name="fname"></td></tr>
<tr><td>Enter Last Name</td><td><input type="text" name="lname"></td></tr>
<tr><td>Enter Username</td><td><input type="text" name="uname"></td></tr>
<tr><td>Enter Pasword</td><td><input type="password" name="pass"></td></tr>
<tr><td>Enter Address</td><td><input type="text" name="address"></td></tr>
<tr><td>Enter City</td><td><input type="text" name="city"></td></tr>
<tr><td></td><td><input type="submit" value="Add" name="button"></td></tr>
</table>
</form>
</html>


May 30, 2011 at 10:54 AM


continue....

4)insertdata.jsp:

<%@page import="java.sql.*,java.util.*"%>
<table>
<%
String fname=request.getParameter("fname");
String lname=request.getParameter("lname");
String uname=request.getParameter("uname");
String pass=request.getParameter("pass");
String address=request.getParameter("address");
String city=request.getParameter("city");
System.out.println(fname+lname+uname+pass+address+city);
        try{
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
        Statement st=con.createStatement();
        int i=st.executeUpdate("insert into login(firstname,lastname,username,password,address,city) values('"+fname+"','"+lname+"','"+uname+"','"+pass+"','"+address+"','"+city+"')");

        out.println("Data is successfully inserted into database.");

                    con.close();

        }
        catch(Exception e){
        System.out.println(e);
        }
        %>
        </table>


Related Tutorials/Questions & Answers:
Advertisements
Advertisements
 

 

 

DMCA.com