1protected void doGet(HttpServletRequest request,
2 HttpServletResponse response) throws ServletException, IOException {
3 String bookBarCode = new String(request.getParameter("bookbarcode")
4 .getBytes("ISO-8859-1"), "GBK");
5 String bookName = new String(request.getParameter("bookname").getBytes(
6 "ISO-8859-1"), "GBK");
7
8 String bookAuthor = new String(request.getParameter("bookauthor")
9 .getBytes("ISO-8859-1"), "GBK");
10 String bookPrice = new String(request.getParameter("bookprice")
11 .getBytes("ISO-8859-1"), "GBK");
12 String bookIntroduce = new String(request.getParameter("bookintroduce")
13 .getBytes("ISO-8859-1"), "GBK");
14
15 response.setContentType("text/html;charset=gb2312");
16 PrintWriter out = response.getWriter();
17 out.println(bookBarCode);
18 out.println(bookName);
19 out.println(bookAuthor);
20 out.println(bookPrice);
21 out.println(bookIntroduce);
22 }
new String(request.getParameter("bookintroduce").getBytes("ISO-8859-1"), "GBK");//先是将bookintroduce使用“ISO-8859-1”字符集解码,然后再使用“GBK”字符集构造新的String。
函数说明如下:
java.lang..String(byte[] bytes, charsetName) throws
String
public String(byte[] bytes,
String charsetName)
throws UnsupportedEncodingException
- Constructs a new
String
by decoding the specified array of bytes using the specified charset. The length of the new String
is a function of the charset, and hence may not be equal to the length of the byte array.
- 构造一个由解码指定的字节数组使用指定的字符集新的String。新的String的长度是一个字符集函数,因此可能不等于字节数组的长度。
The behavior of this constructor when the given bytes are not valid in the given charset is unspecified. The CharsetDecoder
class should be used when more control over the decoding process is required.
- Parameters:
bytes
- The bytes to be decoded into characters
charsetName
- The name of a supported charset
- Throws:
UnsupportedEncodingException
- If the named charset is not supported
- Since:
- JDK1.1
-------------------
tomcat容器默认采用了iso-8859-1的编码方式。