Posts Tagged "Java"

Struts2 Iterating through a List of List

This is a simple example demonstrating how to use the iterator tag in struts2 to iterate through a list of objects which contains another list of objects.
Assume that we have a class called City which contains a list of phone numbers:

public class City {
	private String name;
	private List<PhoneNumber> phoneNumbers;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public List<PhoneNumber> getPhoneNumbers() {
		return phoneNumbers;
	}

	public void setPhoneNumbers(List<PhoneNumber> phoneNumbers) {
		this.phoneNumbers = phoneNumbers;
	}
}
Read More

Plain-text encryption/decryption in Java

There could be cases in which you are planning to keep some sensitive data in your database but you do not wish to have this information available to anyone (not even the DBA should be able to see the content) except the specific user. To do this you can encrypt the sensitive plan-text provided by the user. Save the encrypted message in the database and decrypt it anytime the user would like to view/edit the data. Doing this is quite easy with java. You would just need to add a few jar files to your project and a few lines of code to encrypt and decrypt the data.

The libraries you need to download are available at: http://www.jasypt.org/
The following sample code encrypts and decrypts a String. Please not that I have created an object of BasicTextEncryptor twice the reason for that is to show that you would need to set the correct password used in the encryption process to decrypt the plain-text message.

import org.jasypt.util.text.BasicTextEncryptor;

public class Secret {

	public static void main(String[] args) {

		String userPassword="PassWord";
		String message = "Some text which is to be encrypted by the user password. Later it is decrypted to the original plain-text using the same password.";

		//Encrypt
		BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
		textEncryptor.setPassword(userPassword);
		String myEncryptedText = textEncryptor.encrypt(message);
		System.out.println("Encrypted text:\n"+myEncryptedText);

		//Decrypt
		BasicTextEncryptor textDecryptor = new BasicTextEncryptor();
		textDecryptor.setPassword(userPassword);
		String plainText = textDecryptor.decrypt(myEncryptedText);

		System.out.println("Decrypted text:\n"+plainText);
	}

}
Read More

Google Android


I just installed Google’s Android SDK on my machine. I have to admit that I have been really impressed! I’m not personally an android software developer, I just installed the JDK to see how it is, but if you are planning on developing software for the Android it shouldn’t get long to start implementing your own software. There is an Eclipse plugin developed by google which nicely integrates the SDk into Eclipse. The best thing about android is that developers can develop their software using Java and since there are many java developers out there I’m sure there going to be many applications developed for Android even more than the IPhone, its just a matter of time now. :D The interface components are all added using xml files the following is the xml file that I used to play with the UI.

Read More

Eclipse MYSQL and Java Servlet

Just wasted a day connecting Servlet to the MYSQL :D . It’s funny cause in regular java applications when using Eclipse IDE you just need to import the MYSQL driver as the external .jar file, but when using Servlet you need to add the the external .jar file also you need to copy the mysql-connector-java-*-bin.jar (the driver again)  into WEB-INF/lib. The second part I didn’t know and it cost half a day cause I didn’t use to get any exception errors in the console,…. just a blank page in the browser!!

ECLIPSE

 

Read More

XQJ and XQuery

XQ

Everyone knows that java is one of the best programming languages out there. What I personally love about java is that it has always been evolving to become a better tool for the developers. One of the new features that Java community has been working on is the XQuery API for Java (XQJ), It is a new feature to be added to java API , which gives the developers the functionality of running XQueries on XML files. This functionality is similar to that of JDBC Java API for SQL, but the query language for XQJ is XQuery. There is a great introduction to XQJ on the web if you are interested in knowing more about XQuery click here.

Read More