Sunday, 23 February 2014

Getting validation for entering only numeric values using javaScript

<html>
<head>
<script>
function validate(evt) {
  var theEvent = evt || window.event;
  var key = theEvent.keyCode || theEvent.which;
  key = String.fromCharCode( key );
  var regex = /[0-9]|\./;
  if( !regex.test(key) ) {
 alert("Enter numeric only...");
    theEvent.returnValue = false;
    if(theEvent.preventDefault) theEvent.preventDefault();
  }
  }
</script>
</head>
<body>
Enter  value : <input type='text' onkeypress='validate(event)' />
</body>
</html>

Monday, 10 February 2014

Eclipse ShortCuts

Hi Folks,
These are the most common Eclipse shortcuts used in the industries
Ctrl+shift+OManaging Packages.Used to import/unimport necessary/unneccessary classes
Ctrl+/Use to comment/uncomment the current line.Single line comment
Ctrl+shift+/Used to comment the selected text.Multiline comments
Ctrl+shift+\Used to uncomment the Multiline comments.Select the commented text first
Ctrl+TPlace the cursor on Interface/Class name first. It gives the list of child classes
Ctrl+Shift+JPlace the cursor inside Method/class.Prepares the javadoc for the classes/methods
Ctrl+WClose the current Editor
Ctrl+Shift+WClose All Opened Editor
Ctrl+space Autocomplete/assist
Sysout+ctrl+space System.out.println
Ctrl + DDelete Current Line
Ctrl + LGoto Line Number
Ctrl+Shift+PGoto Matching Braces
Ctrl+OQuick Out Line
Ctrl+Shift+CChange Method Signature
F3Select the method that is called.Press F3 will take you to the method defination
Ctrl+Shift+GPut the cursor on className/Method Name first.It will show all the references of that class/Method.Find who has called that method
Ctrl+Shift+RUsed to Search for any resources like .java,.jsp within the workspace
Double click  after{Will highlight the whole area covered by the {
Alt+Shift+S,RGenerate getters/Setters
Alt+Shift+XRun As
Alt+Shift+DDebug As
Ctrl+QGoto Last Edited Place
Ctrl+IIndent the current/selected Line
Alt+Shift+RRename a class/variable and refactor it.Changes are affected to all the occurance
Ctrl+Shift+down ArrowMove to next methods/variables
Ctrl+Shift+up ArrowMove to previous  methods/variables
Ctrl+EOpen Editor with all resources of the package
Alt+Shift+WOpen the Code in Navigator or other Explorer
Alt+Shift+MSelect the block of code and press this shortcut.Will form another method containing that selected code.Helps to break the big method into smaller chunks
Alt+Shift+CChange method signature and refactor it.All the references will be updated
Ctrl+Shift+LList all the shortcuts
Alt+up /Down arrowMoves the selected code up or down
Ctrl + Alt + HPlace the cursor at the method name,shows the whole hierarchy of method call

Creating Captcha Application

We can generate Captcha application in 2 modes
1.Online mode
2.Offline mode
Online Mode:
Needs internet,we are going to use re-captcha api,which internally
uses db to store images and voices,it is widely used capcha in the
internet,like facebook etc.
re-captcha api is given by Google.
Offline mode:
We are going to use some other offline apis like
It generates captcha but it is one per session,
so if you refresh the page,it wont give you new captcha
In this article we will see both procedures i.e Online Mode & Offline Mode.
1. Online Captcha
MyOnlineCapcha
     |
     |—->WEB-INF
     |         |——>web.xml
     |         |——>lib
     |                    |—–>recaptcha4j-0.0.7.jar
     |——>index.jsp
     |——>process.jsp
Creating Capcha in your Application using google Re-Captcha
1.Open the below link and Click get re-CAPTCHA Link
2. Click on “Sign up Now!” button and signup  to Your gmail Account inorder to use Re-Captcha.
3. Create a reCAPTCHA Key.Enter a domain(can be anything,non-existing as well),Select
Enable this key on all domains.Otherwise the capcha will work only on your domain,if
you want to use them in all other domains you must select them as well.
    Click on create Key.
4. The above step will generate private and public key for captcha.
5. We are now going to use those private and pubic key in our java application inorder to
generate captcha.Download the re-Captcha library from the below link
6. Create a New web Project.Include the jars into the project
    To show the image Include the following code in index.jsp
index.jsp
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<%@ page import="net.tanesha.recaptcha.ReCaptcha"%>
 
<%@ page import="net.tanesha.recaptcha.ReCaptchaFactory"%>
 
<html>
<body>
    <form action="process.jsp" method="post">
        <%
          //ReCaptcha c = ReCaptchaFactory.newReCaptcha("your_public_key", "your_private_key", false);
          ReCaptcha captcha = ReCaptchaFactory.newReCaptcha("6LfwU-kSAAAAANon6jhYZbfeeH-vgCKncnzs9WOr", "6LfwU-kSAAAAAGiis8gP7kS42ty9Wt_1euOGzj3J", false);
          out.print(captcha.createRecaptchaHtml(null, null));
        %>
        <input type="submit" value="submit" />
    </form>
</body>
</html>
7. To Process the answer create process.jsp
process.jsp
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<%@ page import="net.tanesha.recaptcha.ReCaptchaImpl"%>
<%@ page import="net.tanesha.recaptcha.ReCaptchaResponse"%>
 
<html>
<body>
    <%
        String remoteAddr = request.getRemoteAddr();
        ReCaptchaImpl reCaptcha = new ReCaptchaImpl();
        reCaptcha.setPrivateKey("6LfwU-kSAAAAAGiis8gP7kS42ty9Wt_1euOGzj3J");
 
        String challenge = request.getParameter("recaptcha_challenge_field");
        String uresponse = request.getParameter("recaptcha_response_field");
        ReCaptchaResponse reCaptchaResponse = reCaptcha.checkAnswer(remoteAddr, challenge, uresponse);
 
        if (reCaptchaResponse.isValid())
        {
            out.print("Answer was entered correctly!");
        } else
        {
            out.print("Answer is wrong");
        }
    %>
</body>
</html>
2. Offline Captcha
Directory Structure
SimpleCaptchaDemo
       |
       |——–>WEB-INF
       |           |——>web.xml
       |           |——>lib
       |                    |—–>simplecaptcha-1.2-jdk1.5.jar
       |–>index.jsp
       |–>process.jsp
1. Create web.xml
web.xml
?
1
2
3
4
5
6
7
8
9
10
11
12
<web-app>
    <servlet>
        <servlet-name>demo</servlet-name>
        <servlet-class> nl.captcha.servlet.StickyCaptchaServlet
        </servlet-class>
    </servlet>
 
    <servlet-mapping>
        <servlet-name>demo</servlet-name>
        <url-pattern>/stickyImage</url-pattern>
    </servlet-mapping>
</web-app>
2. Create index.jsp to show image.
index.jsp
?
1
2
3
4
5
<img src="stickyImage" />
<form action="process.jsp">
Your Answer<input type="text" name="answer">
<input type="submit" name="submit"/>
</form>
3. To process the answer create process.jsp
process.jsp
?
1
2
3
4
5
6
7
8
9
10
11
12
13
<%@page import="nl.captcha.Captcha"%>
<%
    Captcha captcha=(Captcha)session.getAttribute(Captcha.NAME);
    request.setCharacterEncoding("UTF-8");
 
    String answer=request.getParameter("answer");
 
    if(captcha.isCorrect(answer))
       out.println("Correct");
 
    else
        out.println("Incorrect");
%>