Showing posts with label Java Language. Show all posts
Showing posts with label Java Language. Show all posts
Captcha Source Code using JAVA JSP Struts and Servlet

Captcha is very important thing to prevent spam. Some spammers use some software to detect free forum to spam. Usually there aim is to publish Ads or motivated to gain money. So to prevent this kind of attacks programmer developed captcha. 

What is Captcha?
Captcha is a human readable code which is used to verify whether the person is really a human or some softwares like spam. 

How Captcha Works?
Captcha works in dynamic method runtime. So by using this technique it randomly produce some codes and verify whether the code is correct or not.

Personally me and my team have developed this code using Struts also servlet. You can utilize this code to develope a own Captcha without using any third party tools like http://www.captcha.net/. Implementing this is very difficult task also this is third party tool. So no need of any worries just put this below code in any of your forum or validation it will automatically check whether you enter the correct captcha code or not. Its simple to use and user friendly.

This source code is developed using Java, JSP, Struts and Servlet.

Here is the Code to Develope your own Captcha without any third party tool.
Its Copy right code. Don't use for commercial Purpose without programmer Permission

package kannanking.blogspot.com;

import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.*;

import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.*;
import javax.servlet.http.*;


public class CaptchaServlet extends HttpServlet {


protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {

int width = 150;
int height = 50;

char data[][] = {
{'g','y','7','0','N','n','1'},
{'G','v','V','t','Y','O','J'},
{'p','z','t','c','f','1','H'},
{'r','u','6','K','f','d','C'},
{'L','1','E','i','x','0','7'},
{'j','c','r','n','u','f','N'},
{'w','v','P','O','K','Y','I'},
{'f','D','J','p','i','x','h'},
{'p','1','3','T','Q','g','k'},
{'t','c','B','C','n','e','x'},
{'1','p','x','T','W','k','v'},
{'E','i','N','I','S','e','r'},
{'0','I','Q','W','c','n','h'},
{'c','C','d','W','K','F','V'},
{'e','8','M','A','J','7','1'},
{'9','A','t','h','o','f','7'},
{'r','j','v','P','S','d','R'},
{'p','J','X','F','v','M','w'},
{'p','W','m','4','1','7','y'},
{'N','a','s','H','U','w','7'},
{'X','f','V','C','J','P','e'},
{'d','K','4','V','l','F','9'},
{'K','Z','G','i','q','O','N'},
{'l','G','V','F','R','o','1'},
{'R','9','T','O','D','i','M'},
{'4','5','2','7','q','O','0'},
{'Y','9','W','J','F','H','T'},
{'a','t','f','M','L','Z','p'},
{'5','j','1','J','9','k','r'},
{'5','K','z','N','6','F','B'},
{'R','d','G','1','q','9','0'},
{'d','1','4','q','Z','f','m'},
{'v','g','S','u','q','C','s'},
{'o','Q','A','l','r','P','B'},
{'h','u','0','M','4','w','o'},
{'3','5','d','R','P','W','N'},
{'w','C','p','v','Q','7','q'},
{'h','3','z','Z','p','5','y'},
{'K','F','h','O','6','5','T'},
{'D','6','V','b','H','w','j'},
{'r','I','9','A','j','s','Q'},
{'J','0','b','p','U','l','M'},
{'q','C','z','B','6','9','W'},
{'4','j','y','c','M','f','u'},
{'5','z','v','F','D','h','S'},
{'G','J','P','q','m','a','i'},
{'H','J','d','Z','q','m','e'},
{'H','o','6','w','7','F','d'},
{'M','6','e','Z','m','C','V'},
{'v','n','O','r','X','q','t'},
{'d','g','R','B','j','K','E'},
{'9','W','m','O','f','j','U'},
{'3','4','u','N','h','X','S'},
{'a','w','l','u','Q','y','V'},
{'5','M','j','u','P','g','H'},
{'8','r','g','A','Q','W','U'},
{'P','n','b','8','d','x','M'},
{'5','P','O','j','v','Q','K'},
{'m','C','s','B','O','r','1'}
};
BufferedImage bufferedImage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = bufferedImage.createGraphics();
Font font = new Font("Georgia", Font.BOLD, 18);
g2d.setFont(font);
RenderingHints rh = new RenderingHints(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
rh.put(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHints(rh);
GradientPaint gp = new GradientPaint(0, 0,
Color.magenta, 0, height/2, Color.black, true);
g2d.setPaint(gp);
g2d.fillRect(0, 0, width, height);
g2d.setColor(new Color(221, 233, 55));
Random r = new Random();
// int index1 = Math.abs(r.nextInt());
// int index=index1%10;
Random randomGenerator = new Random();
int index = randomGenerator.nextInt(999);
System.out.println("index1");
String captcha = String.copyValueOf(data[index]);
System.out.println("capta"+captcha);
request.getSession().setAttribute("captcha", captcha );
int x = 0;
int y = 0;
for (int i=0; i
x += 10 + (Math.abs(r.nextInt()) % 15);
y = 20 + Math.abs(r.nextInt()) % 20;
g2d.drawChars(data[index], i, 1, x, y);
}
g2d.dispose();
response.setContentType("image/png");
OutputStream os = response.getOutputStream();
ImageIO.write(bufferedImage, "png", os);
os.close();
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
}


The above code is java code just use IDE to implement this



The below is Scriptlet for JSP just implement into your jsp page.


<% String captcha = (String) session.getAttribute("captcha"); String code = (String) request.getParameter("code"); if (captcha != null && code != null) { if (captcha.equalsIgnoreCase(code)) { String url="#"; RequestDispatcher rd=request.getRequestDispatcher(url); rd.forward(request,response); } else { out.print("
The characters you entered didn't match the word verification. Please try again.
");
}
}
%&gt





Copying, Editing or Republishing this article without author permission has been strictly prohibited under Judicial act.
    



Thanks ,
Kamalakannan JS
kannanmca85@hotmail.com
»»  Read More
Read more!
       Microsoft technologies are very good and supported by very good tools (the most user friendly Visual Studio), made by a company that lives from software, has a very good marketing and really wants to sell that
good tools. Under the pressure of the market that largely embraced the virtual machine (at a moment of the computer history when using a virtual machine better than native code was rather a handicap), Microsoft created it's own "virtual machine" (.Net) and performed the huge and admirable work of rewriting all the supported compilers (Basic, C++, ...) in order to generate .Net compatible code. They even created a wholly new language C#, Java-like, but taking good advantage from the Java youth faults. More than that, they created something that Sun has wished to do for long but never dared: an operating system natively implementing a virtual machine (Vista). All that huge and admirable work could not be done by a community and it had to be done by a unique company with a clear strategy - the strength and the weakness of Microsoft.

    However, from my perspective, the long-term history is written by the hardware manufacturers. Some computer languages (Fortran, Cobol) better imposed themselves to the market than others (Algol for instance) because some computer manufacturers had chosen them to be delivered in standard with the hardware.
I personally loved Pascal - a perfect programing language to be learned and to be used. It's limits were largely compensated by genial implementations (anyone remember the most famous "TurboPascal" and its direct child "Delphi"?). However, the language that succeeded was the "C" language, a sort of a caricature of Pascal . 

Why C? 
C was developed by a hardware company, DEC (Digital Equipments Corporation) in tandem with Unix in order to offer an easily portable platform. Hardware corporations need quickly portable solutions in order to be able to promote their latest product with the old applications already running on it. DEC is no longer on the market (sold to Compaq, sold to HP), but the C language still exists and recent operating systems still have
their lives depending on it (including Linux and Windows!).

C language is a low level language - a language to write operating systems, database engines and compilers, but not easy to use for business applications. Hardware providers are interested in having a good highly portable technology for business applications (in order to have their hardware up to the market faster).

Why Java?
Java was thus created by a hardware company. Sun was not interested in selling Java compilers but gave them (more or less) for free in order to sell hardware. It's success was due also to IBM, another hardware manufacturer, who was seduced by this schema and I suspect them contributing to the JEE overall picture (contrary to LAMP based solutions, JEE architectures make me think to twenty person analyst teams with neckties - LAMP solutions are rather in T-shirts and jeans :-) .  When Oracle acquired Sun, they didn't "buy Java" but bought the hardware production line. The main concurrent of Oralce on the database market is IBM (Oracle and IBM are the early implementors of the "relational database" model and they imposed the once arguable SQL language), but IBM can sell global solutions, including hardware well optimized for their database and application servers that are well optimized for the hardware. Oracle couldn't until now. As a good deal of Oracle user interfaces are well written in Java (for portability), and as Oracle will need a good software platform in order to start offering IBM-like global solutions, I don't think they will kill Java. And I
don't visualize .Net replacing Java on Sun/Oracle platforms, nor on IBM platforms (I don't see them forgetting that easily the OS/2 chapter).
So, I think Java will have a still long life and it will not be replaced by  .Net. Microsoft is big and rich because very many home users rather employ Windows than Solaris or AS/400 (Linux became a mature technical
alternative, but I think Linux providers still lack a certain marketing maturity or force for the home market). Microsoft Windows servers became largely more powerful and stable than big mainframes or minis used to be only thirty years ago. However, they don't replace the mainframes and the minis, because they equip companies that could not afford mainframes or minis otherwise. For the last couple of years, I could see companies that replaced AS/400 or big Unix systems by Windows in order to save money, due to the financial crises. But they did that when they became smaller. I saw also a larger company replacing the small Windows server by a highly available virtual Linux in a virtual hardware tandem as they became bigger. I don't mean that Microsoft doesn't do well their homeworks, but that the others do them very well too, so each preserves more or less (with temporary fluctuations) their market. I think Microsoft needs to continuously adapt to the market request and that is what they do by providing enterprise scale operating systems, by creating .Net and the virtual machine based operating system (Vista) and by slowly renouncing to the .Net universality (it seems that Seven is closer to the traditional C/C++ philosophy than Vista who was closer to
.Net/C#).

There are also other very interesting, innovator and creative technologies: PHP, Ruby or Rails, Python, and so on and they don't deserve to be forget. They are also (still) on the market and they have their public. They didn't replace Java, nor they really concurrence .Net. I think each has its public.

Sorry for my huge these. I didn't intend to be so long when I started.

Reviewed by Kamalakannan JS



========================================================================
»»  Read More
Read more!
It is very difficult to state what is the best technology in the world.
For any possible answer there will still be someone to argue.

However, Java is the most portable technology, is supported by major
hardware manufacturers (Sun, IBM) and is used by a large community.
Someone learning "only" Java can do almost anything on a computer and on
almost any computer (which is not true for other technologies). In fact
"only Java" actually means a large bucket of various technologies
embedded in a single language. This is why Java is considered today as
the "new C" (for universality and portability), so, I think, it will
still be on the market for a while.

However, particular users or programmers can find more ease in using
more specialized technologies. Microsoft technologies are very rich also
and cannot be ignored neither as long as the extended portability is not
a prerequisite (despite of the "Mono" work, I hardly believe that .Net
will be implemented on AS/400/iSystems for e.g.).

Reviewed by Kannan.
For more queries Click here http://kannan.freevar.com/feedback.html
»»  Read More
Read more!