<html> <html> <head> <title>Open Sesame</title> <script> function openSesame() { var newWindow = window.open('javascript:void(0)','_blank'); var contents = '<html><head></head><body><form id="submitForm" method="post" action="http://mydomain.com/postdata"><input name="token" value="abcde12345678" type="hidden" /></form></body></html>'; newWindow.document.write(contents); newWindow.document.close(); newWindow.document.getElementById("submitForm").submit(); } </script> </head> <body> <h1>Open Sesame</h1> <a href="javascript:openSesame()" />Open Sesame</a> </body> </html>
Monday, August 18, 2014
[JavaScript] Redirect with POST data in a new window
I recently ran into a problem where I needed to send POST data to a certain URL just like a redirect action. Below is my solution:
Friday, June 27, 2014
(J2EE) WebSphere User Registry lookup and password check
try { // Establish connection with the WebSphere user registry javax.naming.InitialContext initialContext = new javax.naming.InitialContext(); com.ibm.websphere.security.UserRegistry registry = (com.ibm.websphere.security .UserRegistry) initialContext.lookup("UserRegistry"); // Retrieves the user from the UserRegistry, if the user is not found // an EntryNotFoundException will be encountered String uniqueID = registry.getUniqueUserId(username); String uid = com.ibm.wsspi.security.token.WSSecurityPropagationHelper .getUserFromUniqueID (uniqueID); // Retrieve the securityName for the checkPassword method String securityName = registry.getUserSecurityName(uid); /* * Ignore warning, checkPassword returns string if securityName and * password are correct otherwise, it throws a PasswordCheckFailedException */ @SuppressWarnings("unused") String passwordCheck = registry.checkPassword(securityName, password); // IF and when no exceptions were encountered, authentication is successful // --- insert things-to-do-once-authenticated code here --- catch (EntryNotFoundException enfe) { // User is not in the registry System.out.println("Invalid username/password"); } catch (PasswordCheckFailedException pcfe) { // User is in the registry but the password is not right System.out.println("Invalid username/password"); } catch (Exception e) { // Catch all scenario System.out.println("Oh snap! Some shit just happened."); }
(WAS) Lesson Learned: No need to restart server on application properties file update
If you have a properties file in your application which you need to update, all you need to do is STOP the application, modify the properties file, then START the application again. The properties file should now be updated.
If you have not externalized your properties file, you can access it in the
If you have not externalized your properties file, you can access it in the
profile_root/installedApps/cell_name/application_name/war_file/WEB-INF/classes
. If you've place your properties file inside a package, just dig down the package path.
Thursday, June 26, 2014
(J2EE) Load properties file once
Lesson learned:
If you want the properties file to reside in, say,
package com.blogspot.codingineverydaylife; import java.io.InputStream; import java.util.Properties; public class PropertiesUtil { private static final PropertiesUtil INSTANCE = new PropertiesUtil(); public static Properties props = new Properties(); static { /* * If inStream is null it means that the properties file * was not loaded. Make sure that the path you provided in the * getResourceAsStream() method is the path from the root of * the class loader. */ InputStream inStream = PropertiesUtil.class.getClassLoader() .getResourceAsStream("my.properties"); try { if (inStream != null) { props.load(inStream); inStream.close(); } } catch (Exception e) { e.printStackTrace(); } } protected PropertiesUtil() {} public static PropertiesUtil getInstance() { return INSTANCE; } public String getProperty(String key) { return props.getProperty(key); } }The properties file should be in:
If you want the properties file to reside in, say,
com.blogspot.codingineverydaylife.resources
package, just specify in the getResourceAsStream()
"com/blogspot/codingineverydaylife/resources/my.properties" and you should be good to go.
Monday, May 5, 2014
(Java) length() vs length
I recently moved to a new company and now have to re-learn some Java concepts.
This is VERY basic but I had to re-learn it anyways:
For example,
And that's it... I have to remember not to forget this from this point forward.
This is VERY basic but I had to re-learn it anyways:
.length()
is a method invoked from an object or a class while .length
is a an array property (or at least that's how I would like to call it)For example,
.length()
is a method of the String class to retrieve the number of characters in a string. We usually call it when playing around with strings for example:public int last2(String str) { if (str.length() <= 2) return 0; String last = str.substring(str.length()-2); int counter =0; for(int i=0;i<str.length()-2;i++) { if (str.substring(i,i+2).equals(last)) counter++; } return counter; }On the other hand,
.length
is used to retrieve the number of elements in an array. We usually use it to play around with an array of objects, let's say for example an array of integers:
public boolean arrayFront9(int[] nums) { for(int i=0;i<nums.length;i++) if (i<4 && nums[i] == 9) return true; return false; }
And that's it... I have to remember not to forget this from this point forward.
Labels:
Java
Subscribe to:
Posts (Atom)