Wednesday 13 August 2014

Introduction to PL/SQL.

Let us know something about PL/SQL.

Procedural Language/Structured Query Language or PL/SQL is that part of RDBMS at which we do not pay much attention while studying. However, when it comes to develop project, it is the most important part and cannot be ignored. Using PL/SQL while designing database reduces the programmer's effort exponentially. Question arises, how? And the simple answer is, 'A major portion of programming dealing with DB is finished in the database designing phase.'
Let us consider a scenario, through which we can understand the importance of using PL/SQL.

Consider an example related to accounting, in which tax on salary is to be calculated. There exists a table named employee (in which the account details are stored, say, emp_id,name and salary). Now, on using conventional programming approach, the programmer will need to make a cursor (for which, JDBC-ODBC bridge,DSN etc), through which records of employee table would be extracted, necessary calculation would be performed on extracted data and the generated result i.e. tax would be inserted back in some table say Tax. And all this will be done by programmer.
Now let us have a look at approach in which PL/SQL is involved.While designing DB, few procedures would be made(say, calculateTax and populateTax), in which necessary calculations would be done and later, simply procedures would be called. Here the point to be observed is, making procedures and implementing necessary calculations in them, calling them, all these activities are being performed at DBMS and the programmer is far away from this.

Here's the code for above example.(according to MySQL)

Now let us look at few program units of PL/SQL
  • Procedures
  • Functions
  • Triggers
First program unit is Procedure. It is similar to functions (one which we use in programming) and are used for computations, however the difference between procedure and function is
  1. Function returns a value while procedure do not.
  2. Function can be used in SQL statements while procedures cannot.
Triggers. Basically they are stored procedures which get automatically invoked on occurrence of some specific event. For example, when the values of attribute Current Balance is to be altered on the occurrence of credit/debit events, Trigger is something which would be used. A procedure (more specifically, trigger) would be wired up with insert or delete event and as soon as the events would occur, the wired up stored procedure would be triggered. 

This was a brief introduction about PL/SQL. 

Please correct me if I got something wrong.

Friday 8 August 2014

Lets have a look at Session Tracking.


Here we would be discussing about how to track session in java.
But before moving on to Session Tracking, we must know something about Servlets. Here are some key points. With the request to servlet,

·         4 Objects are made, HttpServletRequest, HttpServletResponse, Document and  PrintWriter (one of             each, at the time of request to servlet)
·         Name/Value pair, which is extracted from request is kept in object of HttpServletRequest  .
·         Specified Servlet is searched. Here story may end (if class is not found), Error message will be kept in           response.
·         Pointer of HttpServlet is taken, Object of specified servlet is made, and its address is assigned to pointer.       Again, story may end (if class do not extend HttpServlet or it is not public)
·         Now doGet(HttpServletRequest,HttpServletResponse) method is called using that pointer. For execution of doPost method, explicit call need to be placed, by default, doGet is called.

After execution of doGet, whatever kept in object of document will be sent to client( browser).

Here's an example for download (lets say one.com)
https://docs.google.com/uc?export=download&id=0B0p0u1WTbGiRV0l1ZWxmMXpSbFE

Need of Session Tracking..???

ü        Problem: Data received in first request will not be available in second response.
Solution: Session Tracking i.e. If the data in the previous request is also available in current request, then it is said that session is being tracked.

How to Track Session...???

 Here we will be discussing on the following ways of Session Tracking.

  • Hidden Form Feed
  • URL Rewriting
  • Cookies
  • Session Object

1. Hidden Form Feed: 
        Data received in first request will be sent along with the response, but in a hidden manner. For doing so, we will keep the value of type attribute as hidden in input tag.
<input type="hidden" name='XYZ' value='Previous Data'>

Here's an example of Hidden Form Feed for download. (lets say two.com)
https://docs.google.com/uc?export=download&id=0B0p0u1WTbGiRQmk4WW5TYTBnb1E

2.URL Rewriting
      To make the data available in second request, we will keep it in query string. For this, we will have to programatically generate query string. But values in query string are encoded according to Http Protocols. Thus, we will be using encode method of class URLEncoder in the java.net package.
     Lets say, 'name' is the variable which is to be encoded.
name=URLEncoder.encode(name);

Here's an example of URL Rewriting for download. (lets say three.com)
https://docs.google.com/uc?export=download&id=0B0p0u1WTbGiROXBoQXZqdkQ0WDg

3. Cookies

  • Cookie is a particular type of object, in which name/value pair is stored.
  • Cookie is made at server side, and then kept in response so as to be transferred to browser
  • Browser will store the cookie at disk, and will also keep a track that from which site, the cookie came.
  • Next time when the request will be sent to that particular site, its cookies will also be sent along with the request.
    Cookie c1=new Cookie(“name”,java.net.URLEncoder.encode(name,”utf-8”));
    c1.setMaxAge(60*10); 
    c1.setPath("/");            

     When cookie is to be given to a particular resource, setPath() will be used 
     SetMaxAge() is used to specify life span of cookie.
     
    response.addCookie(c1);

    And at receiving end,
    c=request.getCookies();
    name=c[0].getValues();

Here's an example of Session Tracking through cookies for download. (lets say four.com)

4. Session Object
      Above listed all ways of tracking session are less reliable than session object. In this way,

   ü  Data in first request will be kept in a Session Object (which will be in RAM), and the second servlet             will get it from there.
   ü  Session Object will be requested from server, which will look that whether the object exists for this               connection or not.
   ü  If not, then address of a newly created session object will be given.
   ü  Session Object will not be shared by other connections and will not be destroyed after response.
   ü  By default, Max Time Interval: 30 min
   
             HttpSession hs=request.getSession();       à requested for session object.
      HttpSession s=request.getSession(false); àReference of existing object will be given, new                                                                                         will not be created( in case it do not exists).
     
       hs.setAttribute(“name”,name);
                                  name=(String)hs.getAttribute(“name”);   à At receiving end

              Here's an example of Session Tracking through Session Object for download (lets say five.com)

       Please leave comments.!!!