How to convert Byte[] array to String in Java

In some cases, we have to convert String variable into a Byte array format, for example, JCE encryption. However how do we convert a Byte[] array to String afterward?

Simple toString() function like following code is not working property. It will not display the original text but byte value.

String s = bytes.toString();

In order to convert Byte array into String format correctly, we have to explicitly create a String object and assign the Byte array to it.

public class TestByte
{
public static void main(String[] argv) {

String example = "This is an example";
byte[] bytes = example.getBytes();

System.out.println("Text : " + example);
System.out.println("Text [Byte Format] : " + bytes);
System.out.println("Text [Byte Format] : " + bytes.toString());

String s = new String(bytes);
System.out.println("Text Decryted : " + s);
}
}

How to change the port number of tomcat?

Now today lets learn how to change the tomcat port number? Whether it is Apache Tomcat 5 or Tomcat 6, by default Apache Tomcat runs on port 8080. But there can be situations where there are some other servers running on this same port forcing you to change the port of one of the servers.

This article explains how to change this port 8080 on Tomcat (tested this against Apache Tomcat 5.5 and 6.0 versions).

Here we’ll be using label to denote the folder where Tomcat is installed. In my system, tomcat is installed in the following path.

C:\Java\Tomcat_x.x

We need to edit the file named server.xml inside \conf folder.



In the server.xml file; locate the following segment.
By changing this 8080 value of port attribute, server would start listening on new port number.
After saving the changed server.xml file, Tomcat server must be restarted (stop then start) to activate the change. Hope you find it useful!

Time Picker using HTML and Java Script

Today while migrating the application, I was in need of having a textfield which takes time as an input. For this reason I needed a time picker, which will help the users to enter the time just by selecting something. I searched in web for this and found one from http://www.nogray.com/time_picker.php. This is very useful source for some most widely used javascripts like calendar, color picker, menubar etc.

Simple, basic but much needed things in java

It is often needed to convert one type of objects into another type in java. Below I have mentioned about these common types as they form basic foundation in java.

* How to convert String to boolean?

String listing = "true";
boolean theValue = Boolean.parseBoolean(listing);

* How to convert boolean to string?

public class BooleanToString {
public static void main(String[] args){
boolean b = true;
String s = new Boolean(b).toString();
System.out.println("String is:"+ s);
}
}

* How to converting string to date?

public class StringToDate {
public static void main(String[] args) {
try {
String str_date="11-June-07";
SimpleDateFormat formatter ;
Date date ;
formatter = new SimpleDateFormat("dd-MMM-yy");
date = (Date)formatter.parse(str_date);
System.out.println("Today is " +date );
}
catch (ParseException e)
{
System.out.println("Exception :"+e);
}
}
}

* How to convert date to string?

import java.util.*;
import java.text.*;
public class DateToString {
public static void main(String[] args) {
try {
DateFormat formatter ;
Date date ;
formatter = new SimpleDateFormat("dd-MMM-yy");
date = (Date)formatter.parse("11-June-07");
String s = formatter.format(date);
System.out.println("Today is " + s);
} catch (ParseException e)
{System.out.println("Exception :"+e); }

}
}

Clipboard Hack Problem - News about CTRL+C

Ctrl+C may be one of the most frequently used keyboard shortcuts we use every day. But it's not a very safe thing to do while you are surfing the internet.
Read on to know why.



What happens when you press Ctrl+C while you are online? We do copy various data by Ctrl + C for pasting elsewhere. This copied data is stored in clipboard and is accessible from the net by a combination of Java scripts and ASP. This is called clipboard hack problem.

Just try this:

1. Copy any text by Ctrl + C
2. Click the Link: http://www.sourcecodesworld.com/special/clipboard.asp
3. You will see the text you copied was accessed by this web page.

Surprised!. Do not keep sensitive data (like passwords, credit card numbers, PIN etc.) in the clipboard while surfing the web. It is extremely easy to extract the text stored in the clipboard to steal your sensitive information. It is true, text you last copied for pasting (copy & paste) can be stolen when you visit web sites using a combination of JavaScript and ASP (or PHP, or CGI) to write your possible passwords/credit card no./sensitive data to a database on another server.

How Clipboard Hack is done?
The Clipboard hack is done by the following Source Code:


How to safeguard yourself from Clipboard Hack Problem?
To avoid clipboard hack problem, do the following:

1. Go to internet options->security.
2. Press custom level.
3. In the security settings, select disable under Allow paste operations via script.

Now the contents of your clipboard are safe.

Interestingly, this hack works only on internet explorer, and not on Mozilla Firefox browser.

Please spread awareness of this issue with CTRL+C & help in preventing online-frauds…