1. 解決中文亂編的問題
要求1:自行編寫程序測試通過表單參數傳遞引起中文亂碼的情況,並解決中文亂編的問題.
(1) 當提交的方式是POST時,應該如何處理?
(2) 當提交的方式是GET時,應該如何處理?
要求2:編寫Servlet程序,在Servlet頁面中通過PrintWriter輸出中文,解決此種亂碼情況.
[html] <%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<center>
<h1>
解決中文亂編的問題
</h1>
<form action="check" method="get">
<table border="1">
<tr>
<td colspan="2">
<h2>
Get提交方式
</h2>
</td>
</tr>
<tr>
<td>
文本:
</td>
<td>
<input type="text" name="username">
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="提交">
</td>
</tr>
</table>
</form>
<form action="check" method="post">
<table border="1">
<tr>
<td colspan="2">
<h2>
Post提交方式
</h2>
</td>
</tr>
<tr>
<td>
文本:
</td>
<td>
<input type="text" name="username">
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="提交">
</td>
</tr>
</table>
</form>
</center>
<pre>
當把中文做為參數進行傳遞的時候,容易產生中文亂碼問題,可采用如下方式解決:
POST傳遞:request.setCharacterEncoding("GBK");
GET傳遞:
將接收過來的參數進行重新轉碼
String name=request.getParameter("username");
name=new String(name.getBytes("ISO8859-1"),"GBK");
修改server.xml
URIEncoding="GBK"
</pre>
</body>
</html>
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<center>
<h1>
解決中文亂編的問題
</h1>
<form action="check" method="get">
<table border="1">
<tr>
<td colspan="2">
<h2>
Get提交方式
</h2>
</td>
</tr>
<tr>
<td>
文本:
</td>
<td>
<input type="text" name="username">
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="提交">
</td>
</tr>
</table>
</form>
<form action="check" method="post">
<table border="1">
<tr>
<td colspan="2">
<h2>
Post提交方式
</h2>
</td>
</tr>
<tr>
<td>
文本:
</td>
<td>
<input type="text" name="username">
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="提交">
</td>
</tr>
</table>
</form>
</center>
<pre>
當把中文做為參數進行傳遞的時候,容易產生中文亂碼問題,可采用如下方式解決:
POST傳遞:request.setCharacterEncoding("GBK");
GET傳遞:
將接收過來的參數進行重新轉碼
String name=request.getParameter("username");
name=new String(name.getBytes("ISO8859-1"),"GBK");
修改server.xml
URIEncoding="GBK"
</pre>
</body>
</html>
[html]
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>encoding</servlet-name>
<servlet-class>com.mars.check</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>encoding</servlet-name>
<url-pattern>/check</url-pattern>
</servlet-mapping>
</web-app>
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>encoding</servlet-name>
<servlet-class>com.mars.check</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>encoding</servlet-name>
<url-pattern>/check</url-pattern>
</servlet-mapping>
</web-app>
[java]
package com.mars;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class check extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=GBK");
String newname = new String(request.getParameter("username").getBytes(
"ISO-8859-1"), "GBK");
PrintWriter out = response.getWriter();
out.print("<html><head></head><body>GET方式提交<br/><h2>" + newname);
out.print("</h2></body></html>");
out.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=GBK");
//request.setCharacterEncoding("GBK");
System.out.println("調用doPost");
String newname = request.getParameter("username");
PrintWriter out = response.getWriter();
out.print("<html><head></head><body>POST方式提交<br/><h2>" + newname);
out.print("</h2></body></html>");
out.close();
}
}
package com.mars;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class check extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=GBK");
String newname = new String(request.getParameter("username").getBytes(
"ISO-8859-1"), "GBK");
PrintWriter out = response.getWriter();
out.print("<html><head></head><body>GET方式提交<br/><h2>" + newname);
out.print("</h2></body></html>");
out.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=GBK");
//request.setCharacterEncoding("GBK");
System.out.println("調用doPost");
String newname = request.getParameter("username");
PrintWriter out = response.getWriter();
out.print("<html><head></head><body>POST方式提交<br/><h2>" + newname);
out.print("</h2></body></html>");
out.close();
}
}
摘自 Mars學IT