Monday 26 September 2016

captcha generation in java based on struts2

 Servlet that generates Captcha image. 
Let's see how our Captcha application works. 

Working of the application

  1. captcha.jsp:  This page contains the captcha image and one text field. 
  2. User reads the image text and then enters in the "Code" text field. After entering the value in the text field, user clicks on the "Verify"  command button.
  3. Then our Struts 2 action validates the user input and the session Captcha value.  If user enters the correct image code in the text filed, then action  forward to the "success.jsp" page. Otherwise, it gives an error and go to again the "captcha.jsp".



Index page
In the  index.html we have added a link to captcha.jsp. User can click on this link to go to the captcha form.


<html>
<head>
<title>Capcha Validation</title>
<style>
a:link{ color:red; text-decoration:underline; }
a:visited{ color:red; text-decoration:underline; }
a:hover{ color:green; text-decoration:none; }
a:active{ color:red; text-decoration:underline; }
</style>
</head>
<body>
<table>
<tr><td><a href="captcha.jsp">Captcha Application</a></td></tr>
</table>
</body>
</html>



2. Captcha Page 
The GUI of the application consists of  Captcha page  (captcha.jsp). 
The captcha.jsp is used to display the valid code and a text box. 
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Capcha Validation</title>
<style>
.errorMessage {
color: red;
font-size: 0.8em;


.label {
color:#000000;
}
</style>
</head>
<body>
<table>
<tr>
<td>
<h1>Captcha Validation</h1>
<s:form action="doCaptcha" method="POST">
<s:actionerror /> 
<img src="Captcha.jpg" border="0">
<s:textfield label="Code" name="j_captcha_response" size="20" maxlength="10"/>
<s:submit value="Verify" align="center" />
</s:form>
</td>
</tr>
</table>
</body>
</html>
The code :
<s:actionerror />
displays action errors and field validation errors.
The code <s:form action="doLogin" method="POST"> generates the html form for the application.

The code :
<img src="Captcha.jpg" border="0">
<s:textfield label="Code" name="j_captcha_response" size="20" maxlength="10"/>
The submit button is generated through <s:submit value="Verify" align="center" /> code.

3. Servlet Class

Develop a Servlet class "RoseIndiaCaptcha.java" to create a Captcha image. Here is the code ofRoseIndiaCaptcha.java Servlet class:

package com.roseindiaCaptcha.servlet;

import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
import java.awt.*;
import java.util.*;
import java.awt.font.TextAttribute;
 
public class RoseIndiaCaptcha extends HttpServlet {

  private int height=0;
  private int width=0;
    
  public static final String CAPTCHA_KEY = "captcha_key_name";

  public void init(ServletConfig config) throws ServletException {
    super.init(config);
   height=Integer.parseInt(getServletConfig().getInitParameter("height"));
     width=Integer.parseInt(getServletConfig().getInitParameter("width"));
  }

 
 protected void doGet(HttpServletRequest req, HttpServletResponse response) 
   throws IOException, ServletException {
    //Expire response
    response.setHeader("Cache-Control", "no-cache");
    response.setDateHeader("Expires", 0);
    response.setHeader("Pragma", "no-cache");
    response.setDateHeader("Max-Age", 0);
    
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 
    Graphics2D graphics2D = image.createGraphics();
    Hashtable map = new Hashtable();
    Random r = new Random();
    String token = Long.toString(Math.abs(r.nextLong()), 36);
    String ch = token.substring(0,6);
    Color c = new Color(0.6662f, 0.4569f, 0.3232f);
    GradientPaint gp = new GradientPaint(30, 30, c, 15, 25, Color.white, true);
    graphics2D.setPaint(gp);
    Font font=new Font("Verdana", Font.CENTER_BASELINE , 26);
    graphics2D.setFont(font);
    graphics2D.drawString(ch,2,20);
    graphics2D.dispose();
    
    HttpSession session = req.getSession(true);
    session.setAttribute(CAPTCHA_KEY,ch);

    OutputStream outputStream = response.getOutputStream();
    ImageIO.write(image, "jpeg", outputStream);
    outputStream.close();



 }

}

4.Mapping  for Servlet class in web.xml
<servlet>
<servlet-name>Captcha</servlet-name>
   <display-name>Captcha</display-name>
  <servlet-class>com.roseindiaCaptcha.servlet.RoseIndiaCaptcha</servlet-class>
<init-param> 
<description>passing height</description> 
<param-name>height</param-name> 
<param-value>30</param-value> 
</init-param> 
<init-param> 
<description>passing height</description> 
<param-name>width</param-name> 
<param-value>120</param-value> 
</init-param> 
</servlet>

<servlet-mapping>
  <servlet-name>Captcha</servlet-name>
   <url-pattern>/Captcha.jpg</url-pattern>
</servlet-mapping>
5.Success page

After successful validation page  transfer to the "success.page"

<html>
<head>
<title>Capcha Validation</title>
</head>
<body>
<table><tr><td><h1>You are Succesfully Validate</h1></td></tr></table>
</body>
</html>
6.Developing Action Class

Now let's develop the action class to handle the Captcha validation request. 
The code  String c= (String)session.getAttribute(RoseIndiaCaptcha.CAPTCHA_KEY) is used to get the Captcha value stored in session and then validate it with the user input. Here is the code of the Action class.

package com.roseindiaCaptcha.action;
import com.opensymphony.xwork2.ActionSupport;
import java.util.Date;
import javax.servlet.http.*;
import javax.servlet.*;
import com.opensymphony.xwork2.ActionContext;
import com.roseindiaCaptcha.servlet.*;

public  class CaptchaAction  extends ActionSupport {
  public String execute() throws Exception {
      HttpServletRequest request  = (HttpServletRequest)
 ActionContext.getContext().get(org.apache.struts2.StrutsStatics.HTTP_REQUEST);
      Boolean isResponseCorrect = Boolean.FALSE;
      javax.servlet.http.HttpSession session = request.getSession();
      String parm = request.getParameter("j_captcha_response");
      String c= (String)session.getAttribute(RoseIndiaCaptcha.CAPTCHA_KEY) ;
      if(!parm.equals(c) ){
      addActionError("Invalid Code! Please try again!");
      return ERROR;
    }else{
    return SUCCESS;
    }
  }
}
7.Configuring action mapping (in struts.xml)
Now we will create action mapping in the struts.xml file. Here is the code to be added in the struts.xml:
<action name="doCaptcha" class="com.roseindiaCaptcha.action.CaptchaAction">
<result name="error">captcha.jsp</result>
<result>success.jsp</result>
</action>
In the above mapping the action "doCaptcha" is used to validates the user using action class (CaptchaAction.java).

Friday 29 July 2016

Problem : Consecutive Prime Sum Some prime numbers can be expressed as Sum of other consecutive prime numbers. For example 5 = 2 + 3 17 = 2 + 3 + 5 + 7 41 = 2 + 3 + 5 + 7 + 11 + 13 Your task is to find out how many prime numbers which satisfy this property are present in the range 3 to N subject to a constraint that summation should always start with number 2. Write code to find out number of prime numbers that satisfy the above mentioned property in a given range.

import java.util.*; class Prime{ public static void main(String []args) { int count=0,p=2,i; Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int a[]=new int[n+1]; for(i=0;i<=n;i++) { a[i]=i; } for(p=2;p*p<=n;p++) { if(a[p]!=0) { for(i=p*2;i<=n;i+=p) { a[i]=0; } int sum=5; for(i=5;i<=n+2;i=i+2){ if((a[i]!=0 && a[i]==sum)||a[i]==-1) count=count+1; if (a[i] != 0 || a[i] == -1) sum=sum+i; if(a[sum]!=0) a[sum]=-1; } } } System.out.print(count); } }

Friday 8 July 2016

That's the Solution to a CODECHEF'S problem (Andrash and Stipendium) .Find bugs in this code......




problem:
Andrash and Stipendium







Andrash study in Uzhland National University. Now is the time of exam results. Andrash similar to other students, hopes that this scores in the exam could fetch him a scholarship/stipendium for his studies.
There are following simple rules to receive stipendium:
  • University follows 5 point grading system. In an exam, a student can receive any score from 2 to 5. 2 is called an F grade, meaning that student has failed that exam.
  • Student should not have fail any of the exams.
  • Student must obtain a full score in some of his/her exams to show that he/she is excellent in some of the subjects.
  • He/She must have a grade point average not less than 4.0
You are given information regarding the exams and how Andrash performed in those. Can you help him figure out whether he will receive the stipendium or not!!
Input
The first line of input contains a single integer T denoting the number of test cases. This will be followed by T test cases.
The first line of each test case contains an integer N denoting the number of examinations.
The next line of each test case contains N space separated integers denoting Andrash's score for each exam.
Output
For each of the T test cases, output a single line - "Yes" (without quotes) if Andrash will receive stipendium, or "No" (without quotes) - otherwise.
Constraints and Subtasks
  • 1T40
  • Let A[i] denote Andrash's score for i-th exam
Subtask #1: 20 points
  • 1N100
  • 2A[i]5
Subtask #2: 20 points
  • 1N105
  • 3A[i]5
Subtask #3: 60 points
  • 1N105
  • 2A[i]5
Example
Input:
2
5
3 5 4 4 3
5
3 4 4 4 5

Output:
No
Yes
Explanation
Example case 1. Student's average grade point score is 3.8 which makes him/her unqualified for obtaining the stipend.
Example case 2. Student satisfies all the criteria for getting stipend.


Solution:

import java.util.Scanner;
import java.util.Arrays;
class Stipend{
public static void main(String []args){

int i,j,avg=0,T,t,N;
Scanner sc=new Scanner(System.in);
T=sc.nextInt();

for(t=1;t<=T;t++){
N=sc.nextInt();

int[]a=new int[N+1];
for(j=0;j<N;j++ ){
  a[j]=sc.nextInt();
  avg=avg+a[j];
}

  avg=avg/N;
  Arrays.sort(a);
if(avg>=4)
{
    if(a[N-1]==5)
    {
if(a[1]==2){
        System.out.println("No");
    }
else{
System.out.println("Yes");
}}

else{
System.out.println("no");}
   

}
else{
    System.out.println("No");
    }

}
}
}