1
protected 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的編碼方式。