Hibernate

adplus-dvertising
Validation of the data using hibernate and servlet with jsp
Previous Home Next

In the Hibernate we can validate the data by using the input on the jsp page and from the database table. In this process a user insert an data into the given text field of the jsp page and that data check from the database if text field value exists from the database then our action will perform which we want to do either action will not be processed.

We can do this type of activity by procedure in database and using a method and another is hibernate which help to make this action direct. To understand this we need to see the below given example which we have given: For making the Validation Application we need to follow the some step which tells us how to work with the Hibernate.

Step 1. Follow the step of the last create application of multi table inserting database. That is shown in the below link. Click Here

Step 2. On that same application taken and create first of all database table One for store the direct value and second the login for the validation.

Table which we use in this application code is given below:


 create table Reg1
(
id int not null primary key auto_increment,
fname varchar(50),
lname varchar(50),
quali varchar(50),
mid varchar(50),
uid varchar(50),
pass varchar(50)
)

create table Login1
(
id int not null primary key auto_increment,
mid varchar(59),
uid varchar(50),
pass varchar(50)
)

Step 3. Now create a jsp page like insert page Page. Code of that page is given below:


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <form action="InsertData" method="post"><pre>
            First Name      <input type="text" name="fname"/>
            Last Name       <input type="text" name="lname"/>
            Email           <input type="text" name="mid"/>
            User Id         <input type="text" name="uid"/>
            Password        <input type="password" name="pass"/>
            
            <input type="submit" value="Submit"/>
                   
        </form>
    </body>
</html>

Here we use the action name is InserData which perform the action to store the data into the database

Step 4. Now we create according to our requirement a Login page code of that page is given below:


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <form action="Validate" method="post"><pre>
            User Id         <input type="text" name="uid"/>
            Password        <input type="password" name="pass"/>
            
            <input type="submit" value="Login"/>
        </form>
    </body>
</html>

Step 5. Now we create the reverse eng file and POJO class as we have discussed in the last example of ours.

Step 6. Now we Prepare the action page for inserting the database Name is InsertData code of that action page is given below. Here in the given code we show the selecte page (Taking servlet action is shown in the last application(click here for last application to show how to take the servlet)):


protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        String fname,lname,mid,uid,pass;
        fname = request.getParameter("fname");
        lname = request.getParameter("lname");
        mid = request.getParameter("mid");
        uid = request.getParameter("uid");
        pass = request.getParameter("pass");
        SessionFactory sf = new Configuration().configure().buildSessionFactory();
        Session s = sf.openSession();
        Transaction tx = s.beginTransaction();
        Regi1 r = new Regi1(fname, lname, mid, uid, pass);
        Login1 l = new Login1(mid, uid, pass);
        s.save(r);
        s.save(l);
        tx.commit();
        s.close();
        
        try (PrintWriter out = response.getWriter()) {
            /* TODO output your page here. You may use following sample code. */
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet InsertData</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet InsertData at " + request.getContextPath() + "</h1>");
           // out.println("<h1><a href="login.jsp">click here for login</a>"+fname+"</h1>");
            
            out.println("</body>");
            out.println("</html>");
        }
    }

Step 7. Now build and run the application.

Step 8. Now create the action of the Login page using validation. Code of the action page is givne page is given below. Here we will take also servlet. (It is main selected part of the action)


 protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
      
        
        
        int a = r4r.Admin.checkId(request.getParameter("uid"),request.getParameter("pass"));
 if(a!=0)
      try (PrintWriter out = response.getWriter()) {
         out.println("ok");
      }
  else 
   try (PrintWriter out = response.getWriter()) {
      out.println("not ok");
     
  }

Step 9. For the connectivity we need to create a method page which hold the all conectivity panel of the action name is we use here admin java class. Which code we use in this class that is also given below:


 public class Admin 
{

    public static int checkId(String u,String ps)
  {
    try
    {
      SessionFactory sf = new Configuration().configure().buildSessionFactory();
      Session s = sf.openSession();
      Transaction tr = s.beginTransaction();
      //Criteria cr = s.createCriteria(r4r.Login1.class).add(Restrictions.eq( "mid", u )); 
      
      Query query = s.createQuery("select count(*) from Login1 where uid = "+u+" && pass = "+ps+"");
       
        query.executeUpdate();

     return query.executeUpdate();
    }catch(Exception ex) { return 0; }
      //  return 0;
  }
}

Setp 10. Right click on the node of the application and run the application of reaching on the Login page.

Previous Home Next