I am writing a jsp page, in which I want to determine the character encoding of request parameters. This is my code:
String name= request.getParameter("hey");
UniversalDetector detector;
try{
detector = new UniversalDetector(null); //Exception is thrown here
detector.handleData(name.getBytes(), 0, name.length());
detector.dataEnd();
String encoding = "s";//detector.getDetectedCharset();
if (encoding != null) {
out.println("Detected encoding = " + encoding);
} else {
out.println("No encoding detected.");
}
detector.reset();
}catch(Throwable e3)
{开发者_运维知识库
out.print("Error: Jasper Exception >>"+e3.getMessage()+"<br>");
}
In the second line(which I mentioned) an exception is thrown. The error printed in catch is:
Error: Jasper Exception >>org/mozilla/universalchardet/prober/CharsetProber
What should I do?
I don't know why you get this exception. But what's sure is that what you're trying to do won't work. When you get a parameter for the request, you get it as a String. This means that the web container has already transformed the bytes it received from the HTTP request into Strings, and thus has already used an charset encoding.
If you call getBytes()
on a parameter, you won't find the bytes sent by the browser. You'll get the result of re-encoding the string into bytes using the default charset of the server.
Another problem of your code is the exception handling: you catch the exception thrown by the constructor but still continue using the detector, which has not been created due to the exception.
Maybe you should explain what you want to do and why. You want to do it.
I got the answer myself and I think it's good to put the answer here. The Jasper Exception was thrown because it could not find the class org/mozilla/universalchardet/prober/CharsetProber.
I noticed that I did not copy the prober folder into the path MyWebSite/WEB-INF/classes/org/mozilla/universalchardet
I copied the folder there and now it works.
P.S: unfortunately my major problem went on. it can't suggest any charset because the length of string is not much enough to be recognized as a special charset text.
精彩评论