Monday, October 8, 2012

Servlets with Extjs

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.sql.*;
import java.sql.*;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;


    public class register extends HttpServlet{

    Statement ps1 = null;
      ResultSet rs1 = null;           

   public void doPost(HttpServletRequest request, HttpServletResponse response)
                                   throws ServletException,IOException{
    response.setContentType("text/html");
    PrintWriter pw = response.getWriter();

     System.out.println("MySQL Connect Example.");
    Connection conn = null;
    String url        = "jdbc:mysql://localhost:3306/";
    String dbName   = "user_register";
    String driver   = "com.mysql.jdbc.Driver";
    String userName = "";
    String password = "";

    String username = request.getParameter("username");
    String user_pass = request.getParameter("password");
    String firstname = request.getParameter("firstname");
    String lastname = request.getParameter("lastname");
    String email = request.getParameter("email");
    String state = request.getParameter("state");
    String city = request.getParameter("city");
    String country = request.getParameter("country");
    Statement st;
    try {
      Class.forName(driver).newInstance();
      conn = DriverManager.getConnection(url+dbName,userName,password);
      System.out.println("Connected to the database");
      String query = "insert into register set

username='"+username+"',password='"+user_pass+"',firstname='"+firstname+"',lastname='"+lastname+"',email=

'"+email+"',state='"+state+"',city='"+city+"',country='"+country+"'";
       st = conn.createStatement();
       int i = st.executeUpdate(query);

      if(i>0)
        {
                pw.println("<html>");
                pw.println("<Title>");
                pw.println("Register");
                pw.println("</Title>");
                pw.println("<body>");
                pw.println("<table width='100%' align='center'>");
                pw.println("<tr>");
                pw.println("<td width='100%' align='center'

style='padding-top:100px;font-weight:bold;color:blue;'>");
                pw.println("You are Successfully entered the Record.");
                pw.println("</td>");
                pw.println("</tr>");
                pw.println("</table>");
                pw.println("</body>");
                pw.println("</html>");

        }
        else
        {
                  response.sendRedirect("register.html?error");
        }
    //  pw.println(query);

    String query1="select * from register";
        ps1 = conn.prepareStatement(query1);
        rs1 = ps1.executeQuery(query1);

/*Table for displaying the records */       

pw.println("<br>");
            pw.println("<b></b>");
            pw.println("<html><table border=''><tr>");
            pw.println("<td><b>Id</b></td>");
            pw.println("<td><b>Username</b></td>");
            pw.println("<td><b>First Name</b></td>");
            pw.println("<td><b>Last Name</b></td>");
            pw.println("<td><b>Email</b></td>");
            pw.println("<td><b>State</b></td>");
            pw.println("<td><b>City</b></td>");
            pw.println("<td><b>Country</b></td>");
            pw.println("</tr>");
            /*while(rs1.next())

            {
                Integer id        = rs1.getInt(1);
                String uname        = rs1.getString(2);
                String fname        = rs1.getString(4);
                String lname        = rs1.getString(5);
                String emailid     = rs1.getString(6);
                String statename   = rs1.getString(7);
                String cityname       = rs1.getString(8);
                String countryname = rs1.getString(9);
               
                pw.println("<tr>");
                pw.println("<td>"+id+"</td>");
                pw.println("<td>"+uname+"</td>");
                pw.println("<td>"+fname+"</td>");
                pw.println("<td>"+lname+"</td>");
                pw.println("<td>"+emailid+"</td>");
                pw.println("<td>"+statename+"</td>");
                pw.println("<td>"+cityname+"</td>");
                pw.println("<td>"+countryname+"</td>");
                pw.println("</tr>");
            }

            pw.println("</table><br>");*/
           

        JSONArray jArray = new JSONArray();
        while(rs1.next())

        {
                Integer id        = rs1.getInt(1);
                String uname        = rs1.getString(2);
                String fname        = rs1.getString(4);
                String lname        = rs1.getString(5);
                String emailid     = rs1.getString(6);
                String statename   = rs1.getString(7);
                String cityname       = rs1.getString(8);
                String countryname = rs1.getString(9);
   
                JSONObject jobj    = new JSONObject();

            //    jobj.put("id",id);
                jobj.put("uname",uname);
                jobj.put("fname",fname);
                jobj.put("lname",lname);
                jobj.put("emailid",emailid);
                jobj.put("statename",statename);
                jobj.put("cityname",cityname);
                jobj.put("countryname",countryname);

                jArray.add(jobj);
           
                       
        }
       
        //pw.println(jArray);       
    
     JSONObject jObjDevice = new JSONObject();
        jObjDevice.put("device", jArray);
        JSONObject jObjDeviceList = new JSONObject();
        jObjDeviceList.put("devicelist", jObjDevice );
   
        pw.println(jObjDevice);
       
      

 
      conn.close();
         pw.println("Disconnected from database");
    }     catch (Exception e)
    {
                e.printStackTrace();
        }
  }
}




var store_company = new Ext.data.Store({
          model: 'Company',
          proxy: {
              type: 'ajax',
              url: 'register',
              actionMethods: {create: "POST", read: "POST", update: "POST", destroy: "POST"},
              reader: {
                  type: 'json',
                  root: 'device',
                  success:'true'
              },
             
          }
         
       });



<script type="text/javascript">
   
    Ext.onReady(function() {              
       Ext.define('Company', {
           extend: 'Ext.data.Model',
           fields: [
                    {name:'uname',type: 'string'},
                    {name:'fname',type: 'string'},
                    {name:'lname',type: 'string'},
                    {name:'emailid',type: 'string'},
                    {name:'statename',type: 'string'},
                    {name:'cityname',type: 'string'},
                    {name:'country',type: 'string'}]
       });

       var store_company = new Ext.data.Store({
          model: 'Company',
          proxy: {
              type: 'ajax',
              url: 'register.class',    //Not sure if this is correct url:'register.class'"!!
             
              reader: {
                  type: 'json',
                  root:'device'
              },
             
          }
         
       });                            
       
       
        var grid_company = Ext.create('Ext.grid.Panel', {
                                     
            store: store_company,
               
                columns:[
                    
                {
                    text     : 'User Name',
                    width    : 120,
                    sortable : true,
                   
                    dataIndex: 'uname'
                },
                {
                    text     : 'Firstname',
                    width    : 120,
                    sortable : true,
                    dataIndex: 'fname'
                },
                {
                    text     : 'Lastname',
                    width    : 110,
                    sortable : true,
                    dataIndex: 'lname'
                },
                {
                    text     : 'Email',
                    width    : 150,
                    sortable : true,
                    dataIndex: 'emailid'
                },
                {
                    text     : 'State',
                    width    : 100,
                    sortable : true,
                    dataIndex: 'statename'
                },
                {
                    text     : 'City',
                    width    : 150,
                    sortable : true,
                    dataIndex: 'cityname'
                },
                {
                    text     : 'Country',
                    width    : 150,
                    sortable : true,
                    dataIndex: 'countryname'
                }
            ],
             bbar: Ext.create('Ext.PagingToolbar', {
        store: store_company,
        displayInfo: true,
        displayMsg: 'Displaying topics {0} - {1} of {2}',
        emptyMsg: "No topics to display"
    }),   
            height: 210,
            width: 902,
            title: 'Company List',
            renderTo: 'grid-company',
            viewConfig: {
                        stripeRows: true
                }       
        });
        store_company.load();
    });
    </script>

0 comments:

Post a Comment