JSP页面写法:
<%@ page contentType="text/html; charset=utf-8" language="java" %>
<%@ page import="java.io.*" %>
<%!
int getServerVersion(HttpServletRequest request) {
ServletContext application = request.getSession().getServletContext();
String info = application.getServerInfo();
int version = 8;
int a = info.indexOf("/");
int b = info.indexOf(".");
if (a != -1 && b != -1) {
version = Integer.valueOf(info.substring(a + 1, b));
}
return version;
}
String getParameter(HttpServletRequest request, String name) {
int version = getServerVersion(request);
String value = request.getParameter(name);
if (version <= 7 && value != null) {
// Tomcat 7及以下需要转换字符编码
try {
value = new String(value.getBytes("ISO-8859-1"), "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
return value;
}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>JSP页面</title>
</head>
<body>
<p>汤姆猫服务器版本: <%= getServerVersion(request) %></p>
<p>
<%
if (getServerVersion(request) <= 7) {
// 防止Tomcat 6-7获取post数据时乱码
request.setCharacterEncoding("ISO-8859-1");
} else {
// 防止Tomcat 8获取post数据时乱码
request.setCharacterEncoding("UTF-8");
}
String value = getParameter(request, "get_text");
out.println("get_text=" + value + "<br />");
value = getParameter(request, "post_text");
out.println("post_text=" + value);
%>
</p>
<form id="form1" name="form1" method="get" action="">
<label>
<input name="get_text" type="text" id="textfield" value="浏览器" />
</label>
<label>
<input type="submit" id="button" value="GET提交" />
</label>
</form>
<form id="form2" name="form1" method="post" action="">
<label>
<input name="post_text" type="text" id="textfield2" value="操作系统" />
</label>
<label>
<input type="submit" id="button2" value="POST提交" />
</label>
</form>
</body>
</html>
Servlet页面写法:
package mytest;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class ParamsTest
*/
public class ParamsTest extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ParamsTest() {
super();
// TODO Auto-generated constructor stub
}
private int getServerVersion(HttpServletRequest request) {
ServletContext application = request.getSession().getServletContext();
String info = application.getServerInfo();
int version = 8;
int a = info.indexOf("/");
int b = info.indexOf(".");
if (a != -1 && b != -1) {
version = Integer.valueOf(info.substring(a + 1, b));
}
return version;
}
private String getParameter(HttpServletRequest request, String name) {
int version = getServerVersion(request);
String value = request.getParameter(name);
if (version <= 7 && value != null) {
// Tomcat 7及以下需要转换字符编码
try {
value = new String(value.getBytes("ISO-8859-1"), "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
return value;
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if (getServerVersion(request) <= 7) {
// 防止Tomcat 6-7获取post数据时乱码
request.setCharacterEncoding("ISO-8859-1");
} else {
// 防止Tomcat 8获取post数据时乱码
request.setCharacterEncoding("UTF-8");
}
// 防止所有Tomcat版本输出网页内容乱码
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
out.println("<html xmlns=\"http://www.w3.org/1999/xhtml\">");
out.println("<head>");
out.println("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />");
out.println("<title>Servlet页面</title>");
out.println("</head>");
out.println("<body>");
out.println("<p>汤姆猫服务器版本: " + getServerVersion(request) + "</p>");
out.println("<p>");
String value = getParameter(request, "get_text");
out.println("get_text=" + value + "<br />");
value = getParameter(request, "post_text");
out.println("post_text=" + value);
out.println("</p>");
out.println("<form id=\"form1\" name=\"form1\" method=\"get\" action=\"\">");
out.println(" <label>");
out.println(" <input name=\"get_text\" type=\"text\" id=\"textfield\" value=\"浏览器\" />");
out.println(" </label>");
out.println(" <label>");
out.println(" <input type=\"submit\" id=\"button\" value=\"GET提交\" />");
out.println(" </label>");
out.println("</form>");
out.println("<form id=\"form2\" name=\"form1\" method=\"post\" action=\"\">");
out.println(" <label>");
out.println(" <input name=\"post_text\" type=\"text\" id=\"textfield2\" value=\"操作系统\" />");
out.println(" </label>");
out.println(" <label>");
out.println(" <input type=\"submit\" id=\"button2\" value=\"POST提交\" />");
out.println(" </label>");
out.println("</form>");
out.println("</body>");
out.println("</html>");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}