Thursday, October 9, 2008

reCaptcha Implementation

Description:
reCaptcha is one of the Captcha implementation. In this article I will mention step by step implementation in a java application and the issues I've faced while using reCaptcha.

Implementation Steps:
  1. Go to reCaptcha site 'http://recaptcha.net/
  2. Open an account and create site
  3. Get public key and private key
  4. In UI page (HTML page or JSP) where the form is available and data entry need to be verified, add following code block - 

    --- Code Start ---
    <-script type="text/javascript" src="http://api.recaptcha.net/challenge?k=(set your the public key)">
    <-/script>
    <-noscript>
    <-iframe src="http://api.recaptcha.net/noscript?k=(set your the public key)" height="300" width="500" frameborder="0"><-/iframe>

    <-textarea name="recaptcha_challenge_field" rows="3" cols="40"><-/textarea>  
    <-input type="hidden" name="recaptcha_response_field" value="manual_challenge">
    --- Code End ---

    Note: remove '-' at the begining of HTML tags which is there to make the HTML tag visible.


  5. In the server side code (servlet, struts Action etc.),  write code as below-

    ------------------
    --- Code Start ---
    ------------------

    -- Import section --
    import net.tanesha.recaptcha.ReCaptchaImpl;
    import net.tanesha.recaptcha.ReCaptchaResponse;

    -- Code to get form value from UI and reCaptcha API call --
    String challenge = request.getParameter("recaptcha_challenge_field");
    String responseValue = request.getParameter("recaptcha_response_field"); 
    String remoteAddr = request.getRemoteAddr(); 
    ReCaptchaImpl reCaptcha = new ReCaptchaImpl(); 
    reCaptcha.setPrivateKey("6LepaQMAAAAAANSLGCL-mZGEEnimfywUSztxLVRg");
    ReCaptchaResponse reCaptchaResponsereCaptcha.checkAnswer(remoteAddr, challenge, responseValue );
    boolean valid = reCaptchaResponse.isValid();

    if (valid) { /* Your code */}
     else { /* Your code */}
    ------------------
    --- Code End ---
    ------------------ 
Issue List

Issue 1:
reCaptcha doesn't support IPv6 yet. So, need to be careful when getting IP address of machine and passing to reCaptcha API.

Solution 1:
If IPv6 isn't explicitly needed, uninstall IPv6 protocol from the machine
Solution 2:
Configure JVM to work with IPv4 protocol stack. The parameter needs to be set is "-Djava.net. preferIPv4Stack=true". 

Issue 2:
reCaptcha shows two words to validate but in practical, second word doesn't have any effect. This is a known issue in reCaptcha.

Resources:
1.  reCaptcha web site: 'http://recaptcha.net/'

Wednesday, September 10, 2008

How to use cursor

Implicit cursors - For SQL queries returning single row PL/SQL declares implicit cursors.
Explicit Cursors - Explicit cursors are used in queries that return multiple rows.

Cursor declaration:
without parameters-
CURSOR cursor_name
IS
    SELECT_statement;
    
with parameters-
CURSOR cursor_name (parameter_list)
IS
   SELECT_statement;
   
Open, fetch and close cursor:
Syntax for open - OPEN ;        
Syntax for close - CLOSE ;
Syntax for fetch - FETCH INTO ;

Cursor (explicit) attributes:
%NOTFOUND: Boolean, which evaluates to true, if the last fetch failed.
%FOUND: Boolean, which evaluates to true if the last fetch, succeeded. 
%ROWCOUNT: numeric, which returns number of rows fetched by the cursor so far. 
%ISOPEN: Boolean, which evaluates to true if the cursor is opened otherwise to false.

Loop for cursor:
 - Cursor For Loop itself opens a cursor, read records then closes the cursor automatically. Hence OPEN, FETCH and CLOSE statements are not necessary in it. 

Cursor updattion/deletion:
Steps:
  1. delcare cursor with 'FOR UPDATE' clause
  2. Add 'WHERE CURRENT OF ' in update or delete statement

Example of cursor usage:
1. Open and loop through
OPEN cursor_name FOR
            SELECT * FROM table_name;
         LOOP
            FETCH INTO variables;
         END LOOP;
CLOSE cursor_name;



---
Resource:
1. http://www.exforsys.com/tutorials/oracle-9i/oracle-cursors.html
2. http://www.techonthenet.com/oracle/cursors/declare.php
---

Thursday, July 31, 2008

Oracle error List and solutions

This post includes the Oracle errors i got during my development (java code, Stored Procedure, executing SQL from TOAD/SQLPlus). I tried to give little history for each error to understand the problem I had.


Error:
ORA-01460: unimplemented or unreasonable conversion requested

History:
Environment- Red Hat LInux 4.5/JBoss 4.0.5 GA/Oracle 10g (OS/Application server/database)
I was using ojdbc14.jar for Oracle9i driver and when i try to save a file as BLOB in oracle database from my java code (using JNDI), this exception was thrown.

Solution:

Use ojdbc14.jar of version
10.2.0.4.

Error:
ORA-17009: Closed Statement : Next

History:
In DB Accessor class, first method calls second method which returns a ResultSet and the second method is actually creates DB connection, statement and executes the query. The second method closes the connection and statement in finally block. So, when first method try to traverse the ResultSet


Solution:

Use ojdbc14.jar of version
10.2.0.4.


Friday, July 18, 2008

How to avoid confirmation message when closing browser using window.close()

Description:
From javascript, when we use window.close() to close the browser window programmatically, Internet explorer shows a confirmation message.

Solution:
This solution does work for both IE 6 & 7.

Code snippet-start

Part 1:
function closeWindow(){
window.opener = ''; //Line 1
window.open('', '_self',''); // Line 2
window.close();
}

Part 2:
input value="Close Browser" onclick="closeWindow()" type="button"

Code snippet-end

Explanation:
Line 1 - is required for IE 6
Line 2 - is required for IE 7