Skip to content

Instantly share code, notes, and snippets.

@codinko
codinko / ServletResponseSetCookieSnippet.java
Created December 26, 2015 00:08
ServletResponseSetCookieSnippet
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Cookie cookie = new Cookie("username", "harley");
cookie.setMaxAge(60*60*24); // 24 hours for expiry
response.addCookie(cookie);
}
@codinko
codinko / OddEvenThreadType1.java
Last active May 3, 2020 21:16
Java Threads - Print Odd numbers and even numbers using two threads
package com.codinko.oddeven;
/* OddEvenThreadType1: One thread prints odd, other thread prints even so o/p will be 0 2 4 6 8 10 1 3 5 7 9 11
*/
public class OddEvenThreadType1 {
public static void main(String[] args) {
Runnable r1 = new Runnable1();
Runnable r2 = new Runnable2();
Thread t1 = new Thread(r1);
Thread t2 = new Thread(r2);
@codinko
codinko / FactoryMethodPatternEg2.java
Last active December 29, 2015 18:49
FactoryMethodPattern
class CustomerFactory
{
public static ICustomer GetCustomer(int i)
{
switch (i)
{
case 1:
GoldCustomer goldCustomer = new GoldCustomer();
goldCustomer.GoldOperation();
goldCustomer.AddPoints();
@codinko
codinko / FactoryMethodPatternEg1.java
Last active December 29, 2015 18:48
FactoryMethodPattern
public enum CarType {
SMALL, LUXURY
}
public Interface class Car { // abstract class or Interface.
}
public class CarFactory {
public static Car buildCar(CarType model) {
Car car = null;
@codinko
codinko / FactoryMethodPatternEg3.java
Created December 29, 2015 18:51
FactoryMethodPattern
public abstract class MazeGame {
public MazeGame() {
Room room1 = makeRoom();
Room room2 = makeRoom();
room1.connect(room2);
this.addRoom(room1);
this.addRoom(room2);
}
abstract protected Room makeRoom();
@codinko
codinko / AbstractFactoryPatternEg1.java
Created December 29, 2015 18:54
AbstractFactoryPattern
//GuiFactory example
//Abstract Product
interface Button {
void paint();
}
//Abstract Product
interface Label {
void paint();
}
@codinko
codinko / IterateOverMapEntrySet.java
Created January 5, 2016 18:59
HashMap - Iterate over EntrySet
public static void printMap(Map map) {
Iterator iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry entry = (Map.Entry)iterator.next();
System.out.println(entry.getKey() + " = " + entry.getValue());
}
}
//or
@codinko
codinko / IterateOverListAndSet.java
Created January 6, 2016 02:18
List and Set - Different ways of Iteration
//The first two ways are using iterator() method of the Collection Interface
Iterator<String> iterator = list.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
//or
for(Iterator<String> iterator=list.iterator();iterator.hasNext(); )
{
System.out.println(iterator.next());
}
@codinko
codinko / SpringConfigRaw.xml
Created January 17, 2016 03:32
Raw Spring Configuration file
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi=<a href="http://www.w3.org/2001/XMLSchema-instance">http://www.w3.org/2001/XMLSchema-instance</a>
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- <bean/> definitions here -->
</beans>
</xml>
@codinko
codinko / SpringEhcachePom.xml
Last active January 17, 2016 05:42
pom.xml for Ehcache with Spring
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.codinko.cache</groupId>
<artifactId>caching</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<ehcache.version>2.4.2</ehcache.version>