Gadgets

Monday, 21 March 2016

Java web project using Servlet and JSP

Java Project using Servlet and JSP

In this article I explained a java project, it contains registration, login, update profile, change password, delete profile, logout, and forgot password operations.
I posted code here clearly and I drawn a flow chart for clear understanding.
Create the project in Netbeans
Create the Hibernate project in Netbeans
Registrations JSP file -  index.jsp
In this registration form I added form validation i.e. HTML5 
<form action="./Register"method="post">
   <table>
            <tr><td>Name :</td><td><input type="text" name="na" required/></td></tr>
            <tr><td>Email id :</td><td>
<input type="email" name="email"pattern="*@*.+" required/></td></tr>
            <tr><td>Password :</td><td>
<input type="password" name="psw"pattern=
"[A-Za-z0-9!@#$%^&*()_]{8,30}"onchange="this.setCustomValidity
(this.validity.patternMismatch ? 'Password contain at least 8 charecters':'');
if(this.checkValidity())form.cpsw.pattern=this.value"required/>
</td></tr>
            <tr><td>Reenter Password :</td><td>
<input type="password" name="cpsw"
onchange="this.setCustomValidity(this.validity.patternMismatch ?
 'Please enter same password as above':'');"required/>
</td></tr>
            <tr><td>Gender :</td><td>
<input type="radio" name="gender" value="male" required/>Male
 <input type="radio" name="gender" value="female" required/>
Female</td></tr>
            <tr><td>Birth Date :</td><td>
<input type="date" name="date"required/>
</td></tr>
            <tr><td>Phone Number :</td><td>
<input type="tel" name="phno"pattern="[0-9]{10}"required /></td></tr>
         
            <tr><td>Security Question :</td><td>
                    <select name="sq"required>
                        <option value="">Select question</option>
                        <option value="who is your best friend">Who is your best friend</option>
                        <option value="what is your favorite color">What is your favorite color</option>
                        <option value="what you like most in this world">What you like most in
this world</option>
                    </select>
                </td></tr>
            <tr><td>Security Answer :</td><td><input type="text" name="sa"required/>
</td></tr>
            <tr><td></td><td>
<input type="checkbox" name="tc" value="ON"required />
Accepted Terms and Conditions</td></tr>
             <tr><td></td><td><input type="submit" value="Signup" name="Signup" />
</td></tr>
        </table>
        </form>

Registration Servlet file - Register.java

            String name,uid,psw,dob,phno,sq,sa,gen;
            name=request.getParameter("na");
            uid=request.getParameter("email");
            psw=request.getParameter("psw");
            gen=request.getParameter("gender");
            dob=request.getParameter("date");
            phno=request.getParameter("phno").toString();
            sq=request.getParameter("sq");
            sa=request.getParameter("sa");
            Class.forName("com.mysql.jdbc.Driver");
            Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/
narendar","user","12345");
            String q="insert into user values('"+name+"','"+uid+"','"+psw+"','"+gen+"','"+dob+
"','"+phno+"','"+sq+"','"+sa+"')";
            Statement stmt=con.createStatement();
            int i=stmt.executeUpdate(q);
            if(i>0){
                out.println("registation successful <a href='login.jsp'>Click here to login</a>");
                  }
            else{ 
               out.print("error");            }

Login jsp file - login.jsp

<form action="./Login" method="post">
            <table>
                <tr><td>Email id :</td><td>
<input type="email" name="email"pattern="*@*.+" required/></td></tr>
                <tr><td>Password :</td><td>
<input type="password" name="psw"pattern="[A-Za-z0-9!@#$%^&*()_]{8,30}"required/>
</td></tr>
                <tr><td></td><td><input type="submit" value="Singin" name="Signin" />
</td></tr>
                <tr><td></td><td><a href="forgot.jsp">Forgot Password</a></td></tr>
            </table>
        </form>

Login Servlet file -  Login.java

                     String uid,psw;
                  uid=request.getParameter("email");
                  psw=request.getParameter("psw");
                  Class.forName("com.mysql.jdbc.Driver");
                  Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/
              narendar","user","12345");
                  String q="select * from user where uid='"+uid+"' and password='"+psw+"'";
                  Statement stmt=con.createStatement();
                  ResultSet res=stmt.executeQuery(q);
                  String dbuid=null,dbpsw=null;
                  while(res.next()){
                      dbuid=res.getString("uid");
                      dbpsw=res.getString("password");
                  }
                  if(uid.equals(dbuid)&&psw.equals(dbpsw)){
                      HttpSession se=request.getSession(true);
                      se.setAttribute("dbuid", dbuid);
                      RequestDispatcher rd=request.getRequestDispatcher("home.jsp");
                      rd.forward(request, response);
                  }
              else{
                      out.print("incorrect userid and password");
                      request.getRequestDispatcher("login.jsp").include(request, response);
                     }

Session created - lock.jsp

In this file, we get the values from database and forward that values to home page
<%
String uid=session.getAttribute("dbuid").toString();
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/
narendar","user","12345");
String q="select * from user where uid='"+uid+"'";
Statement stmt=con.createStatement();
ResultSet re=stmt.executeQuery(q);
String tuid=null,tname=null,tdob=null,tphno=null,tgen=null;
while(re.next()){
    tuid=re.getString("uid");
    tname=re.getString("name");
    tgen=re.getString("gender");
tphno=re.getString("ph_number").toString();
tdob=re.getString("dob").toString();   
  }
%>

 Welcome Home page - home.jsp

<%@include file="lock.jsp" %>
Welcome <%out.print(tname);%>
        <nav>
            <a href="home.jsp">Home</a>&nbsp;|&nbsp;
            <a href="update_p.jsp">Update Profile</a>&nbsp;|&nbsp;
            <a href="change_psw.jsp">Change Password</a>&nbsp;|&nbsp;
            <a href="delete_profile.jsp">Delete Profile</a>&nbsp;|&nbsp;
            <a href="logout.jsp">Logout</a>&nbsp;|&nbsp;
        </nav>

 Update profile file update_p.jsp

<%@include file="lock.jsp" %>
        <h1>Update Your Profile</h1>
        <form action="./Update"method="post">
        <table>          
             <tr><td></td><td>
<input type="hidden" name="email"value="<%out.print(tuid);%>"/></td></tr>          
            <tr><td>Name :</td><td>
<input type="text" name="na" value="<%out.print(tname);%>"required/></td></tr>         
            <tr><td>Birth Date :</td><td><input type="date" name="date"
value="<%out.print(tdob);%>"required/></td></tr>
            <tr><td>Phone Number :</td><td>
<input type="tel" name="phno"pattern="[0-9]{10}"value="<%out.print(tphno);%>"
required /></td></tr>
             <tr><td></td><td><input type="submit" value="Update" name="Update" />
</td></tr>
        </table>
        </form>

Update servlet file - Update.java

                     String name,phno,uid,dob;
                     name=request.getParameter("na");
                     phno=request.getParameter("phno");
                     dob=request.getParameter("date");
                     uid=request.getParameter("email");
                     Class.forName("com.mysql.jdbc.Driver");
                     Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/
                     narendar","user","12345");
                     String q="update user set name='"+name+"',dob='"+dob+"',ph_number='"+phno+
                     "' where uid='"+uid+"'";
                     Statement stmt=con.createStatement();
                     int i=stmt.executeUpdate(q);
                     if(i>0){
                     out.print("successfully updated");
                             request.getRequestDispatcher("home.jsp").include(request, response);
                     }else{
                         out.print("try again");
                          }

Change password jsp file -  change_psw.jsp

<%@include file="lock.jsp" %>
        <h1>Change Password</h1>
         <form action="./Changepsw"method="post">
        <table>
            <tr><td></td><td>
<input type="hidden" name="email" value="<%out.print(uid);%>" /></td></tr>
            <tr><td>Old Password :</td><td>
<input type="password" name="oldpsw"pattern="[A-Za-z0-9!@#$%^&*()_]{8,30}"
required/></td></tr>
            <tr><td>New Password :</td><td>
<input type="password" name="psw"pattern="[A-Za-z0-9!@#$%^&*()_]{8,30}"
onchange="this.setCustomValidity
(this.validity.patternMismatch ? 'Password contain at least 8 charecters':'');
if(this.checkValidity())form.cpsw.pattern=this.value"required/>
</td></tr>
            <tr><td>Reenter Password :</td><td>
<input type="password" name="cpsw"
onchange="this.setCustomValidity(this.validity.patternMismatch ?
 'Please enter same password as above':'');"required/>
</td></tr>
            <tr><td></td><td><input type="submit" value="Update" name="Update" />
</td></tr>
        </table>
        </form>

Change password servlet file - Changepsw.java

              String oldpsw,newpsw,uid;
            oldpsw=request.getParameter("oldpsw");
            newpsw=request.getParameter("psw");
            uid=request.getParameter("email");
            Class.forName("com.mysql.jdbc.Driver");
            Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/
narendar","user","12345");
            String q="select * from user where uid='"+uid+"'";
            Statement stmt=con.createStatement();
            ResultSet rs=stmt.executeQuery(q);
            String tpsw=null;
            while(rs.next()){
              tpsw=rs.getString("password");
                    }
            if(oldpsw.equals(tpsw)){
                String q1="update user set password='"+newpsw+"' where uid='"+uid+"'";
                Statement st=con.createStatement();
                int i=st.executeUpdate(q1);
                if(i>0){
                    out.print("your password successfully updated");
        request.getRequestDispatcher("home.jsp").include(request, response);
                }
                else{
                    out.print("try again");
                }
            }else{
                out.print("you given wrong password");
            }
        }

Delete profile jsp file - delete_profile.jsp

<h1>Are you sure, do you want delete your profile?</h1>
        <a href="home.jsp">No</a>
        <a href="delete.jsp">Yes</a>
Profile Delete jsp file(file name delete.jsp):
<%@include file="lock.jsp" %>
      <%    
String q1="delete from user where uid='"+tuid+"'";
Statement stmt1=con.createStatement();
int i=stmt1.executeUpdate(q1);
if(i==0){
    out.println("your account deleted successfully..<a href='index.jsp'>Click here to Register
</a>");
}
%>

Logout jsp file logout.jsp

<%@include file="lock.jsp" %>
        <%
        HttpSession se=request.getSession(false);
        se.invalidate();
        out.print("successfully loged out");
        request.getRequestDispatcher("login.jsp").include(request, response);
                %>

FORGOT PASSWORD CODE

Forgot password jsp file - forgot.jsp

<form action="./Emailcheck" method="post">
            <table>
                <tr><td>Email id :</td><td><input type="email" name="email"pattern="*@*.+"
required/></td></tr>
                <tr><td></td><td><input type="submit" value="Continue" name="" /></td></tr>
            </table>
        </form>

Forgot password servlet file - Emailcheck.java

                  String uid=request.getParameter("email");
                  Class.forName("com.mysql.jdbc.Driver");
                  Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/
              narendar","user","12345");
                  String q="select * from user where uid='"+uid+"'";
                  Statement stmt=con.createStatement();
                  ResultSet rs=stmt.executeQuery(q);
                  String tuid="",tsq="";
                  while(rs.next()){
                      tuid=rs.getString("uid");
                       tsq=rs.getString("security_q");
                  }
                  if(uid.equals(tuid)){
                  HttpSession se=request.getSession(true);
                  se.setAttribute("tuid", tuid);
                  se.setAttribute("tsq",tsq);
                  RequestDispatcher rd=request.getRequestDispatcher("forgot_psw.jsp");
                  rd.forward(request, response);
                  }
                     else{
              out.print("Your Entered wrong email id");
                     request.getRequestDispatcher("forgot.jsp").include(request, response);
                     }
                 }

Security question and answer checking - forgot_psw.jsp

<%
String se_uid=session.getAttribute("tuid").toString();
String se_sq=session.getAttribute("tsq").toString();   
%>
<form action="forgot_psw.jsp" method="post">
            <table>
                <tr><td>your security question :</td><td><%out.print(se_sq);%></td></tr>
                <tr><td>your security answer :</td><td><input type="password" name="sa"
required/></td></tr>
                <tr><td></td><td><input type="submit" value="Continue" name="Signin" />
</td></tr>             
            </table>
        </form>
<%
try{
    String sa;
    sa=request.getParameter("sa");  
    Class.forName("com.mysql.jdbc.Driver");
    Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/
narendar","user","12345");
    String q="select * from user where uid='"+se_uid+"' and security_q='"+se_sq+"'";
    Statement stmt=con.createStatement();
    ResultSet rs=stmt.executeQuery(q);
    String tsa=null,tuid=null,tpsw=null;
    while(rs.next()){
        tsa=rs.getString("security_a");
        tuid=rs.getString("uid");      
    }
    if(sa.equals(tsa)){
    HttpSession se=request.getSession(true);
    se.setAttribute("tsa", tsa);
    se.setAttribute("tuid",tuid); 
  RequestDispatcher rd=request.getRequestDispatcher("update_psw.jsp");
  rd.forward(request, response);
    }
       else{       out.println("Your Entered wrong answer, try again");
       }
}

Update forgot password file - update_psw.jsp

<%
        String se_sa=session.getAttribute("tsa").toString();
        String se_uid=session.getAttribute("tuid").toString();
        %>
        <h1>Update your password</h1>
        <form action="./Forgotpsw"method="post">
        <table>
            <tr><td></td><td>
<input type="hidden" name="email" value="<%out.print(se_uid);%>" /></td></tr>
             <tr><td></td><td>
<input type="hidden" name="email" value="<%out.print(se_sa);%>" /></td></tr>
           <tr><td>Password :</td><td>
<input type="password" name="psw"pattern="[A-Za-z0-9!@#$%^&*()_]{8,30}"
onchange="this.setCustomValidity
(this.validity.patternMismatch ? 'Password contain at least 8 charecters':'');
if(this.checkValidity())form.cpsw.pattern=this.value"required/></td></tr>
           <tr><td>Conform Password :</td><td>
<input type="password" name="cpsw"onchange="this.setCustomValidity
(this.validity.patternMismatch ? 'Please enter
same password as above':'');"required/></td></tr>
           <tr><td></td><td>
<input type="submit" value="Update" name="Update" />
</td></tr>
        </table>        </form>

Update forgot password servlet file - Forgotpsw.java

String psw,uid;         
                     psw=request.getParameter("psw");
                     uid=request.getParameter("email");
                     Class.forName("com.mysql.jdbc.Driver");
                     Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/
                     narendar","user","12345");
                     String q="update user set password='"+psw+"' where uid='"+uid+"'";
                     Statement stmt=con.createStatement();
                     int i=stmt.executeUpdate(q);
                     if(i>0){
                     out.print("your password updated successfully..<a href='login.jsp'>Click here
                     to login</a>");
                     }else{
                         out.print("try again");
                     }
                

OUTPUT SCREEN SHORTS









No comments:

Post a Comment