Posts

Showing posts from March, 2012

Java Server Pages (JSP) - Submit

<%--------FormOrnegi.jsp----------%>

<html>
<body>
<form method="post" action="FormData.jsp">
<table>
<tr>
<td>Ad:</td>
<td><input type="text" name="txtAdi"/></td>
</tr>
<tr>
<td>Soyad:</td>
<td><input type="text" name="txtSoyad"/></td>
</tr>
<tr>
<td><input type="submit" name="btnGonder" value="Gonder"/></td>
</tr>
</table>
</form>
</body>
</html>

<%--------FormData.jsp----------%>

<%@ page import="java.util.*"%>
<%
out.println("Ad: "+request.getParameter("txtAdi"));
out.println("<br>Soyad: "+request.getParameter("txtSoyad"));
%>

Java Server Pages (JSP) - Session

Apache Tomcat ROOT Directory: This is the directory that we must copy our Jsp projects.
C:\Program Files\Apache Software Foundation\Apache Tomcat 7.0.11\webapps\ROOT\JspOrnekleri


<%--------KullaniciGirisi.jsp----------%>
<html>
<body>
<%
out.println("Session ID: "+session.getId());
%>
<form method="post" action="KullaniciGirisi.jsp">
<table>
<tr>
<td>Ad:</td>
<td><input type="text" name="txtAd"/></td>
</tr>
<tr>
<td>Sifre:</td>
<td><input type="text" name="txtSifre"/></td>
</tr>
<tr>
<td><input type="submit" name="btnGonder" value="Gonder"/></td>
</tr>
</table>
</form>
<%

String ad=request.getParameter("txtAd");
String sifre=request.getParameter("txtSifre");

if(ad!=null&&sifre!=null)
{
if(ad.equals("Nurhak")&…

Polymorphism

package polymorphismsample;

/**
 *
 * @author Nurhak
 */
public class PolymorphismSample {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        ComputerEngineer ce = new ComputerEngineer("Nurhak", "Kaya", "IT");
        ce.addProgrammingLang("Java");
        ce.addProgrammingLang("C#");
        Accountant ac = new Accountant("Jessie", "J");
        Manager mn = new Manager("Michael", "M");
        mn.addEmployee(ce);
        mn.addEmployee(ac);
        mn.getReport();
    }
}

package polymorphismsample;
/**  *  * @author Nurhak  */ public class Employee {
    protected String name;     protected String surname;
    public Employee(String name, String surname) {         this.name = name;         this.surname = surname;     }
    public void getReport() {         System.out.println("Name: " + name + " Surname: " + sur…

Thread Sample - III

package thread2;

/**
 *
 * @author Nurhak
 */
public class Thread2 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Worker w1 = new Worker("A");
        Worker w2 = new Worker("B");
        Worker w3 = new Worker("C");
        Worker w4 = new Worker("D");

        w1.setPriority(Thread.MIN_PRIORITY);
        w2.setPriority(Thread.NORM_PRIORITY);
        w3.setPriority(Thread.MAX_PRIORITY);
        w4.setPriority(Thread.MAX_PRIORITY - 2);
        w1.start();
        w2.start();
        w3.start();
        w4.start();
    }
}

package thread2;
/**  *  * @author Nurhak  */ public class Worker extends Thread {
    public Worker(String name) {         super(name);     }
    @Override     public void run() {         try {             for (int i = 0; i < 10; i++) {                 System.out.println("Priority --> " + this.getPriority());             }         } catch (Exc…

Thread Sample - II

package threads;

/**
*
* @author Nurhak
*/
public class Threads {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {

Worker w1=new Worker();
Worker w2=new Worker();

w1.start();
w2.start();
}
}


package threads;

/**
 *
 * @author Nurhak
 */
public class Worker extends Thread {

    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {

            System.out.println(this.getName() + "-->" + i);

        }
    }
}

Socket Server Sample

package socketserver;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Nurhak
 */
public class SocketServer extends Thread {

    public static final int PORT = 8080;
    public Socket socket = null;

    public SocketServer(Socket socket) {
        this.socket = socket;
    }

    public static String findValue(int param) {
        String text = null;
        switch (param) {
            case 0:
                text = "Zero";
                break;
            case 1:
                text = "One";
                break;
            case 2:
                text = "Two";
                break;
            case 3:
                text = "Three";
                break;
            …

IP Finder

package ıpfinder;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Nurhak
 */
public class IPFinder {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        InetAddress ia;
        try {
            ia = InetAddress.getLocalHost();
            String pcName = ia.getHostName();
            String pcIp = ia.getHostAddress();

            System.out.println("PcName: "+pcName + " & Pc IP: " + pcIp);
        } catch (UnknownHostException ex) {
            Logger.getLogger(IPFinder.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}

Hash Mapper - II

package hashmapper2;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

/**
 *
 * @author Nurhak
 */
public class HashMapper2 {

    private static HashMap hm = new HashMap();

    public static void main(String[] args) {

        hm.put("England", "London");
        hm.put("Italy", "Rome");
        hm.put("Turkey", "Ankara");
        hm.put("Germany", "Berlin");
        hm.put("Greece", "Athens");
        hm.put("Spain", "Madrid");
        hm.put("Czech Republic", "Prague");
        hm.put("Belgium", "Brussels");
        hm.put("Holland", "Amsterdam");

        Set s = hm.keySet();
        Iterator countries = s.iterator();
        while (countries.hasNext()) {
            Object country = countries.next();
            System.out.println("Country name: &…

Hash Mapper

package hashmapper;

import java.util.HashMap;

/**
 *
 * @author Nurhak
 */
public class HashMapper {

    private static HashMap hm = new HashMap();
    private static String numbers[][] = {
        {"1", "a"},
        {"2", "b"},
        {"3", "c"},
        {"4", "d"},
        {"5", "e"}
    };

    public static void findValue(String key) {
        System.out.println("-->" + hm.get(key));
    }

    public static void main(String[] args) {

        for (int i = 0; i < numbers.length; i++) {
            hm.put(numbers[i][0], numbers[i][1]);
        }

        findValue("1");
        findValue("3");
        findValue("5");

        System.out.println(hm);

    }
}

Bank Account Sample

package bankprocess;

/**
 *
 * @author Nurhak
 */
public class BankProcess {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        BankAccount account = new BankAccount();

        account.setAccountNr(123456789);
        account.putMoney(3000);
        account.takeMoney(1500);

        System.out.println("Total amount of money: " + account.getTotal());
    }
}

package bankprocess;
/**  *  * @author Nurhak  */ public class BankAccount {
    private int accountNr;     private double total;
    public boolean takeMoney(double money) {         if (money <= this.getTotal()) {             this.setTotal(this.getTotal() - money);             return true;         } else {             return false;         }     }
    public void putMoney(double money) {         this.setTotal(this.getTotal() + money);     }
    /**      * @return the accountNr      */     public int getAccountNr() {         return accountNr;     }
    /** …

Thread Sample - I

package banker;

/**
 *
 * @author Nurhak
 */
public class Banker extends Thread {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        Account a = new Account();
        Worker1 w1 = new Worker1("Worker1", a);
        Worker2 w2 = new Worker2("Worker2", a);
        w1.start();
        w2.start();
    }
}


package banker;

/**
 *
 * @author Nurhak
 */
public class Account {

    private double moneyAmount = 50;

    public double putMoneyToBank(double m) {
        this.moneyAmount += m;
        return moneyAmount;
    }

    public void getMoneyFromBank(double m) {
        if (this.moneyAmount < 0) {
            System.out.println("Not enough money at the Bank account! " + this.moneyAmount);
            return;
        }
        this.moneyAmount -= m;
    }

    public double learnMoneyAmount() {
        return moneyAmount;
    }
}

package banker;
/**  *  * @author Nurhak  */ public class Worker1 e…

E-Mail Sender

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package emailsender;

import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 *
 * @author nurhak.kaya
 */
public class EmailSender {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws NoSuchProviderException, MessagingException {

        try {

            String host = "smtp.gmail.com";
            String from = "abc@gmail.com";
            String pass = "***";
            boolean sessionDebug = true;
            String subject = "Test mail subject";
            String content = "This is the text part of the message";

            Properties props = System.…

Algorithm Sample - Sum of the numbers that can be divided 5 but can't be divided 7 from 1 to 100(1 and 100 included)

package java_final;


import java.util.ArrayList;
import java.util.List;


/**
 *
 * @author nurhak.kaya
 */
public class Java_Final {


    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        int sayi = 0;
        int toplam = 0;
        List list = new ArrayList();
        try {
            for (sayi = 1; sayi <= 100; sayi++) {
                if (sayi % 5 == 0) {
                    if (sayi % 7 != 0) {
                        list.add(sayi);
                        toplam = toplam + sayi;
                    }
                }
            }


            System.out.println("1'den 100'e kadar (1 ve 100 dahil) 5'e tam bölünebilen"
                    + " fakat 7'ye tam bölünemeyen tüm ardışık sayılar :");


            for (int i = 0; i < list.size(); i++) {


                System.out.println(" --> " + list.get(i));
            }
            System.out.println("…

WebService sample - Station Query (İstasyon Sorgu)

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.entity;

/**
 *
 * @author nurhak.kaya
 */
public enum Istasyon {

    Station1("48120"),
    Station2("48500");

    String value;

    public String getValue() {
        return value;
    }

    private Istasyon(String value) {
        this.value = value;
    }
}

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.entity;

/**
 *
 * @author nurhak.kaya
 */
public class KaynakEntity {
    String resourceCallSign;

    String stationName;
    String stationCallSign;

    public String getResourceCallSign() {
        return resourceCallSign;
    }

    public void setResourceCallSign(String resourceCallSign) {
        this.resourceCallSign = resourceCallSign;
    }

    public String getStationCallSign() {
        return stationCallSign;
    }

    public void setStationCallSign(String stationCallSign) {
   …

WebService sample - Identity Query (Kimlik Sorgu)

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.entity;

/**
 *
 * @author nurhak.kaya
 */
public enum Gender {

    Male("Erkek"),
    Female("Bayan");
    String value;

    public String getValue() {
        return value;
    }

    private Gender(String value) {
        this.value = value;
    }
}

/*  * To change this template, choose Tools | Templates  * and open the template in the editor.  */ package net.entity;
/**  *  * @author nurhak.kaya  */ public class KimlikEntity {
    String tcKimlikNo, name, surname, address, sucDurum;
    public String getTcKimlikNo() {         return tcKimlikNo;     }
    public void setTcKimlikNo(String tcKimlikNo) {         this.tcKimlikNo = tcKimlikNo;     }
    public KimlikEntity(String tcKimlikNo) {         this.tcKimlikNo = tcKimlikNo;     }
    public KimlikEntity() {     }     //Gender gender;     String gender;
    public KimlikEntity(String tcKimlikNo, String …