The problem discussed in this article is a technical puncture to the problems encountered in the project.
People who have done the project know that after the completion of the function of a project, the next thing is to consider how the functions of these ideas to achieve, for their unfamiliar areas, to carry out technical puncture. My puncture method is to first find a very good open source components available, if not, find the relevant documents, write and test their own code.
In this article, I mainly solve three problems.
1, solve the problem of string encryption, in the previous article, we design user module, we are ready to the user's password field to MD5 encrypted way to save, so here need to write a string encryption to generate a MD5 string method;
2, to solve the problem of generating image thumbnails and generating verification code;
3, solve the problem of URL rewriting, the reason to use URL rewriting, mainly in order to allow users to visit their home page, you can use Http://www.xkland.com/username or http://username.xkland.com such a form , rather than an ugly form like http://www.xkland.com/index.jsp?username=xxx.
It should be explained that to solve the above three problems, it is not that there is no open source, but I think it is too troublesome to integrate different components every time, and the function we need is not very complex, we do not need things that are too common, as long as we can solve the specific problems here, so we should do it ourselves. At the same time, technical improvements can also be achieved.
First look at the problem of MD5 encryption, the JDK will provide data encryption support, where the Java.security.MessageDigest class can achieve MD5 encryption, but the data generated after encryption is byte[] type, Here you just need to write a method to convert it to a string on the line, the code is as follows:
package com.xkland.util;
import java.security.MessageDigest;
import java.lang.NullPointerException;
import java.security.NoSuchAlgorithmException;
public class StringUtil {
public static char [] num_chars = new char [] { ' 0 ' ,
' 1 ' , ' 2 ' , ' 3 ' , ' 4 ' , ' 5 ' , ' 6 ' , ' 7 ' , ' 8 ' ,
' 9 ' , ' A ' , ' B ' , ' C ' , ' D ' , ' E ' , ' F ' } ;
public static String toMD5String(String input)
throws NullPointerException,NoSuchAlgorithmException {
if (input == null ) throw new NullPointerException();
char [] output = new char [ 32 ];
MessageDigest md = MessageDigest.getInstance( " MD5 " );
byte [] by = md.digest(input.getBytes());
for ( int i = 0 ;i < by.length;i ++ ) {
output[ 2 * i] = num_chars[ (by[i] & 0xf0 ) >> 4 ];
output[ 2 * i + 1 ] = num_chars[ by[i] & 0xf ];
}
return new String(output);
}
}