December 31, 2013

Struts application in java


Java struts login page application source code


This application is for beginner. copy paste the code to respective folder.  The application shows you how login  with struts work.

Login page

Login.jsp

Java struts login page application source code


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>

<title> Java struts web application source code </title>
</head>
<body>

<h2>Sample web application</h2>

<s:actionerror />

<s:form action="login.action" method="post">
    <s:textfield name="username" key="label.username" />
    <s:password name="password" key="label.password" />
    <s:submit method="execute" key="label.login" align="center" />
</s:form>

</body>
</html>

This jsp teach you how struts tag works

<%@ taglib prefix="s" uri="/struts-tags"%> adding a struts tag to jsp page

<s:form action="login.action" method="post">  as like form in html. once you submit the form the corresponding login action in struts.xml is called.

key="label.login" will be defined in application.properties

Welcome page.jsp   ( Second page)


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title> struts JSP jquery application</title>
</head>
<body>
<h2> Welcome to java lazy blog, Java web application </h2>
</body>
</html>


Web.xml


<?xml version="1.0" encoding="UTF-8"?>

<web-app id="WebApp_9" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

   
    <display-name>Struts2 application</display-name>

    <filter>
        <filter-name>StrutsNew</filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>StrutsNew</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <welcome-file-list>
        <welcome-file>Login.jsp</welcome-file>
    </welcome-file-list>

</web-app>

---

    <filter>
        <filter-name>StrutsNew</filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>StrutsNew</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

Through this filter class in struts.xml we are re directing each every url [ <url-pattern>/*</url-pattern>]  through filterdispatcher <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>



Jar file needed. you have to put this in lib folder






LoginAction.java inside src folder


package com.blazy.strutsSpringapplication;
/*
* Action class in struts
*/
public class LoginAction {
   
    private String username = null;
    private String password = null;
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
   
    public String execute()
    {
        if(username.equals("u") & (password.equals("p")))
           // code for validation
        {
            System.out.println(" Struts frame work example download "+username + password);
        return "success";
        }
        else
            return "error";
       
    }

}

--

public class LoginAction  implements ......

ApplicationResources.properties file inside resources folder


label.username = username
label.password = password
label.login = Login
error.login = Invalid user try again later



Struts.xml file  inside resource folder


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="false" />
    <constant name="struts.custom.i18n.resources" value="ApplicationResources" />

    <package name="default" extends="struts-default" namespace="/">
     <action name="login" class="com.org.strutsNew.LoginAction" converter="" method="save">
            <result name="success">Welcome.jsp</result>
            <result name="error">Login.jsp</result>
        </action>
     </package>
</struts>

we have created the login class above
<action name="login" class="com.org.strutsNew.LoginAction" converter="" method="save">

This is only a small program. please try this.

Wish you a Happy New year

+Deepu A.B.


Similar posts

  1. Java spring model view and controller - hello world example



https://javabelazy.blogspot.in

December 25, 2013

Cryptography in java

Java Cryptography



 import javax.crypto.Cipher;
   import javax.crypto.BadPaddingException;
   import javax.crypto.IllegalBlockSizeException;
   import javax.crypto.KeyGenerator;
   import java.security.Key;
   import java.security.InvalidKeyException;

   public class LocalEncrypter {

        private static String algorithm = "DESede";
        private static Key key = null;
        private static Cipher cipher = null;

        private static void setUp() throws Exception {
            key = KeyGenerator.getInstance(algorithm).generateKey();
            cipher = Cipher.getInstance(algorithm);
        }

        public static void main(String[] args)
           throws Exception {
            setUp();
            if (args.length !=1) {
                System.out.println(
                  "USAGE: java LocalEncrypter " +
                                         "[String]");
                System.exit(1);
            }
            byte[] encryptionBytes = null;
            String input = args[0];
            System.out.println("Entered: " + input);
            encryptionBytes = encrypt(input);
            System.out.println(
              "Recovered: " + decrypt(encryptionBytes));
        }

        private static byte[] encrypt(String input)
            throws InvalidKeyException,
                   BadPaddingException,
                   IllegalBlockSizeException {
            cipher.init(Cipher.ENCRYPT_MODE, key);
            byte[] inputBytes = input.getBytes();
            return cipher.doFinal(inputBytes);
        }

        private static String decrypt(byte[] encryptionBytes)
            throws InvalidKeyException,
                   BadPaddingException,
                   IllegalBlockSizeException {
            cipher.init(Cipher.DECRYPT_MODE, key);
            byte[] recoveredBytes =
              cipher.doFinal(encryptionBytes);
            String recovered =
              new String(recoveredBytes);
            return recovered;
          }
   }

Merry Christmas and happy new year wishes to all visitors +belazy


Up helly aa fire festival

http://belazy.blog.com/

December 18, 2013

To check whether a number is prime or not in java

How to find Prime Number in java

How to determine a prime number in java :This program will check whether the given input is either prime number or not. it will return true or false.
copy and paste the code to notepad. compile it with javac and run it...

Instead of bufferedStream class you can use scanner class.  The logic i used is divide the given number from 2 to half of the number. if any one is divisible i stopped the loop and concluded it as not prime. if it successfully passed through all loop with out a division, then its a prime


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Ramanujan s number 1729
 */

/**
 * @author Proximotech Java lazy
 *
 */


public class PrimeOrNot {

    /**
     * @param lazy java
     */
      static int i,num,flag=0;
   
    public static void main(String[] vadassery) {
        // TODO Auto-generated method stub
   
        System.out.println("enter the integer no:");
       BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
       try {
         num=Integer.parseInt(br.readLine());
    } catch (NumberFormatException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
       for(i=2;i<num/2;i++){
           if(num%2==0)
           {
               flag=1;break;
           }
       }
       if(flag==1)
           System.out.println("The given number is a prime number");
       else
           System.out.println("The above number is not a prime number"); 
    }

}

write a program in java to check the given number is prime or not


Checking the program with ramanujans number 1729 (smallest prime number which can be expressed as the sum of the cubes of two numbers in two ways )

out put : The given number is prime

Remember the smallest prime number is 2

To print all prime number in java


Alternative method to check a number is prime or not


public class JavaPrimeNumberCheck {
    public boolean isPrimeNumber(int number){
         
        for(int i=2; i<=number/2; i++){
            if(number % i == 0){
                return false;
            }
        }
        return true;
    }
     
    public static void main(String a[]){
        JavaPrimeNumberCheck pc = new JavaPrimeNumberCheck();
        System.out.println("Is 17 prime number? "+pc.isPrimeNumber(17));
        System.out.println("Is 19 prime number? "+pc.isPrimeNumber(19));
        System.out.println("Is 15 prime number? "+pc.isPrimeNumber(15));
    }
}

 Number to word convertor in java



public
class Test {
public static void main(String[] args) {
String []a = {"","one","two","three","four","five"};
String []b = {"","ten","twenty","thirty","fourty"};
int number = 432;
int i = number;
int d = i/100;
int r = i%100;
int t = r/10;
int tr = r% 10;
System.out.println(a[d]+" Hundered and "+b[t]+" "+a[tr]);
}
}



Are you looking for prime factor in java click here

http://belazy.blog.com/

December 16, 2013

ActionListeners example in java

ActionListeners example in java


 import java.awt.*;
import java.awt.event.*;

class click extends Frame implements ActionListener
{
Button b,p;

click()
{
this.setLayout(null);
b= new Button("pink");
p= new Button("blue");
b.setBounds(100,299,70,20);
p.setBounds(200,299,70,20);
this.add(p);
this.add(b);
b.addActionListener(this);
p.addActionListener(this);
}

public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==p)
setBackground(Color.pink);
if(ae.getSource()==b)
setBackground(Color.blue);
}

public static void main(String a[])
{
click c=new click();
c.setTitle("adding button");
c.setSize(500,500);
c.setVisible(true);
}

}


Decimal to Binary calculation in java


//import java.io.*;

class dectobin
{

public static void main(String arg[])
{

int b;

//int a=Integer.parseInt(10);
int a=10;


while(a<=0)
{

b=a%2;
a=a/2;
System.out.print(b);
}

System.out.println(" Binary to decimal convertor in java source code");

}
}


Creating A frame in java

import java.awt.*;
class demoframe extends Frame
{
public static void main(String arg[])
{
demoframe f=new demoframe();
f.setSize(300,300);
f.setVisible(true);
f.setTitle("Dallas cowboy xkeycode");

}
}

Ceaser Encryption in java


import java.awt.*;
import java.awt.event.*;
//import java.awt.text.*;
import java.lang.*;



public class encryp extends Frame implements ActionListener

{

Label l1,l2,l3;
TextField t;
TextField t2,k;
String s1;


Button b,e;

encryp()
{
this.setLayout(null);

l1=new Label("Input :");
l1.setBounds(350,50,50,20);
this.add(l1);
//l1.addActionListener(this);
l2=new Label("key :");
l2.setBounds(350,100,50,20);
this.add(l2);
l3=new Label("Output :");
l3.setBounds(350,350,50,20);
this.add(l3);




t=new TextField(4);
t.setBounds(400,50,100,20);
this.add(t);
t.addActionListener(this);

k=new TextField(4);
k.setBounds(400,100,100,20);
this.add(k);
k.addActionListener(this);



t2=new TextField(4);
t2.setBounds(400,350,100,20);
this.add(t2);
t2.addActionListener(this);

b=new Button("Ceaser");
b.setBounds(300,140,50,30);
this.add(b);
b.addActionListener(this);

e=new Button("exit");
e.setBounds(410,300,50,30);
this.add(e);
e.addActionListener(this);



}

public void work()
{
System.out.println(" work ");
}

public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==b)
{
s1=t.getText();
StringBuffer s=new StringBuffer(s1);
System.out.println(t.getText());

//s.reverse();
System.out.println(s);
t2.setText(s.reverse());
}

if(ae.getSource()==e)
{
System.exit(0);
}

}

public void textChanged(TextEvent te)
{
System.out.println(" java source code  ");
}

public static void main(String arg[])
{

encryp e=new encryp();
e.setTitle(" encryption ");
e.setSize(900,600);
e.setVisible(true);
}


Simple applications, copy paste and run it...



http://belazy.blog.com/

December 13, 2013

Method Overriding example in java


This is a small example for overriding in java.


Method Overriding : Implementation in sub class with same parameters and type signature Overrides the implementation in super class. Call of overriding is resolved through dynamic binding by JVM. JVM only deals with overriding.


Base class


public class Base {
   
    public void area()
    {
        System.out.println(" area inside base class ");
    }

}

Facebook comments