I turned 0x17
I just want to thank God for all the blessings. I want to remind everyone that we are not getting any younger. Have a great day ahead.
I just want to thank God for all the blessings. I want to remind everyone that we are not getting any younger. Have a great day ahead.
If you are working on the back-end, this might be of use. Very trivial but rarely used.
/**
* This class demonstrates how to recover from checked exceptions
* @author Joset
*/
public class CheckedExceptionRecovery {
/**
* @param args the command line arguments
*/
public static void main(String... args) {
InputStreamReader inputStreamReader = new InputStreamReader(System.in);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
int input = 0;
boolean done = false;
do {
try {
System.out.println("Please enter an integer: ");
input = Integer.parseInt(bufferedReader.readLine().trim());
done = true;
} catch (NumberFormatException numberFormatException) {
System.out.println("Invalid input. Please try again.");
} catch (IOException ioException) {
System.out.println("Cannot proceed.");
}
} while (!done);
System.out.println("The integer is: " + input);
}
}
I was inspired by a face-to-face technical interview awhile ago that is why I am writing this down.
To avoid having the Servlet’s doXXX() methods clogged, use reflection by breaking down your controller code into modules. Here’s how.
You must have the following.
1. Reflection Interface (ServletHandler.java) - An interface for reflection. Nice definition!
2. Main Servlet (MainServlet.java) - A class extending HttpServlet.
3. Module Handler (CreditHandler.java) - A class containing the module’s controller code, for this example, the Credit Module.
in file ServletHandler.java,
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public interface ServletHandler {
public abstract void setServlet(HttpServlet servlet);
public abstract void handle(HttpServletRequest request, HttpServletResponse response);
}
in file MainServlet.java,
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
...
Class HandlerClass = null;
ServletHandler handler = null;
try {
//assuming handlerClassName was defined beforehand as CreditHandler
HandlerClass = Class.forName("com.eradicus.diamondbank.controller." + handlerClassName);
} catch (ClassNotFoundException classNotFoundException) {
//some logging facility here
}
if (HandlerClass != null) {
try {
handler = (ServletHandler) HandlerClass.newInstance();
} catch (InstantiationException instantiationException) {
//some logging facility here
} catch (IllegalAccessException illegalAccessException) {
//some logging facility here
}
if (handler != null) {
//set executing servlet
handler.setServlet(this);
//switch control
handler.handle(request, response);
}
}
...
}
in file CreditHandler.java,
public class CreditHandler implements ServletHandler {
//use this if you need some attributes / methods from the executing servlet such as connection methods, application variables, etc.
private MainServlet servlet;
public void setServlet(HttpServlet servlet) {
this.servlet = (MainServlet) servlet;
}
//this is where the control comes in after invoking handler.handle() above
public void handle(HttpServletRequest request, HttpServletResponse response) {
//some good code here
}
}
There you go. I hope that helps. God bless.
There’s nothing new here. I just want to reiterate though.
class TargetClass {
private static String DB_PASSWORD = "sw0rdfish";
private static String getDatabasePassword() {
return DB_PASSWORD;
}
}
And the attack?
import java.lang.reflect.Method;
public class ClassPiercing {
public static void main(String... args) throws Exception {
Class targetClass = Class.forName("TargetClass");
Method[] methods = targetClass.getDeclaredMethods();
methods[0].setAccessible(true);
String databasePassword = (String)methods[0].invoke(null, null);
System.out.println("Database Password: " + databasePassword);
}
}
Output:
Database Password: sw0rdfish
Check out Val’s Blog by clicking [here]. He has more examples.
Consider the snippet.
Integer firstInteger = 1000; // autoboxing
Integer secondInteger = 1000; //autoboxing
if (firstInteger != secondInteger) {
System.out.println("Different objects!");
}
if(firstInteger.equals(secondInteger)) {
System.out.println("Meaningfully equivalent!");
}
Output:
Different objects! Meaningfully equivalent!
How about this one.
Integer firstInteger = 100; // autoboxing
Integer secondInteger = 100; //autoboxing
if (firstInteger == secondInteger) {
System.out.println("Equal objects!");
}
if(firstInteger.equals(secondInteger)) {
System.out.println("Meaningfully equivalent!");
}
And the output?
Equal objects! Meaningfully equivalent!
And the explanation?
Two instances of the wrapper objects will always be == when their primitive values are the same.
- Boolean
- Byte
- Character from \u0000 to \u007F (0 to 127)
- Short from -128 to 127
- Integer from -128 to 127
Tsk.
This is useful for storing passwords in a database though still vulnerable to md5 dictionary attacks, anyway, here’s a static method.
public static String encrypt(String text) {
String ecryptedText = "";
try {
MessageDigest md5Encrypt = MessageDigest.getInstance("MD5");
md5Encrypt.update(text.getBytes(), 0, text.length());
ecryptedText = new BigInteger(1, md5Encrypt.digest()).toString(16);
} catch (NoSuchAlgorithmException exception) {
exception.printStackTrace();
}
return ecryptedText;
}
This will return the MD5-encrypted string. Have a great day!
Welcome 2009!
Plurk with me, http://www.plurk.com/eradicus
See my multiply account, http://eradicus.multiply.com
Prospero año nuevo!
Exactly 2 weeks left. I’ll be missing the torch parade tonight. Anyway, soar high ADZU High School Batch 2003! Time to review my bass guitar skills.
I need space, so there it goes. If you want to learn more about it, follow this link.
For the impatient, buy the Pro edition i.e. with FireWire400 and eSATA. USB 2.0 chokes obviously.
Static methods and variables are shared by all instances of the class. Static variables are initialized when a class is loaded whereas instance variables are initialized when an instance of the class is created. Static methods belong to a class, therefore, it can only access static members of the class and it can be called before instantiating the class.
class StaticCase {
static int staticCounter = 0;
int nonStaticCounter = 0;
StaticCase() {
staticCounter++; //class level
nonStaticCounter++; //instance level
}
}
class StaticCaseImpl {
//static method, entry point
public static void main(String... args) {
//StaticCase.nonStaticCounter, error, not a static variable
StaticCase sc1 = new StaticCase();
StaticCase sc2 = new StaticCase();
System.out.println("staticCounter sc1: " + sc1.staticCounter);
//output is staticCounter sc1: 2
//or in static context, StaticCase.staticCounter
System.out.println("nonStaticCounter sc1: " + sc1.nonStaticCounter);
//output is nonStaticCounter sc1: 1
System.out.println("staticCounter sc2: " + sc2.staticCounter);
//output is staticCounter sc2: 2
//or in static context, StaticCase.staticCounter
system.out.println("nonStaticCounter sc2: " + sc2.nonStaticCounter);
//output is nonStaticCounter sc2: 1
}
}
I got this from Amerei. Here is my color.
T.J. Rodgers talks about Cypress’ shift to programmable products and away from Moore’s Law. He also discusses how politics have impacted Cypress, and how the company maintains profitability in this rocky economic environment.
Click [here] to read the article.
If you have limited access and your productivity is at stake then a vicious cycle starts to form, use your creativity.
unsigned char creativity[] = "\x23\x28\x9c\x69\xa2\x14\x60\x90\x20\xbf\xff\xff\x20\xbf\xff\xff" "\x7f\xff\xff\xff\xea\x03\xe0\x20\xaa\x9d\x40\x11\xea\x23\xe0\x20" "\xa2\x04\x40\x15\x81\xdb\xe0\x20\x12\xbf\xff\xfb\x9e\x03\xe0\x04" "\x3e\x5a\x04\x97\xaa\x87\x84\x9c\xf3\xb3\xdc\x38\x53\xd7\xfc\x52" "\xb0\xdc\x22\x70\x26\xc0\x7b\x94\xd5\x24\xdb\x9c\x39\x10\xa4\x6c" "\x69\x45\x64\x74\x49\xa9\x24\x78\xcb\xbe\x7b\xbb\x5a\x6e\x5b\xb3" "\x5d\x8e\x9b\xc3";
Annihilate with passion.
Theme designed by Joset Anthony Zamora