解å³JSPæ件ä¸çä¹±ç é®é¢
a.jsp页é¢å 容å¦ä¸ï¼
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="/Ajax_First/b.jsp" method="POST">
name:<input type="text" name="name" />
<input type="submit" />
</form>
</body>
</html>
b.jsp页é¢å 容å¦ä¸ï¼
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
String name=request.getParameter("name");
%>
<%=name %>
</body>
</html>
å½å¨a.jsp页é¢ä¸è¾å ¥ä¸æ并æ交åï¼å¨b.jsp页é¢åºç°ä¹±ç ï¼æ¤æ¶å¯ä»¥ç¨å¦ä¸æ¹æ³è¿è¡è®¾ç½®ã
解å³æ¹æ³ä¸ï¼
a.jsp页é¢çå
容ä¸å,b.jsp页é¢ä¸çå
容ååå¦ä¸ï¼
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
//String name=request.getParameter("name");
String name = new String(request.getParameter("name").getBytes("ISO-8859-1"),"UTF-8") ;
%>
<%=name %>
</body>
</html>
解å³æ¹æ³äºï¼
注æï¼æ¤æ¹æ³åªè½è§£å³postæ交æ¹å¼ã
åä¸ä¸ªè¿æ»¤å¨å¦ä¸ï¼
web.xmlæ件æå
¥ä»£ç å¦ä¸ï¼
<filter> <filter-name>filter</filter-name> <filter-class>com.EncodingFilter</filter-class> </filter> <filter-mapping> <filter-name>filter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
EncodingFilter.java代ç å¦ä¸ï¼
package com;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class EncodingFilter implements Filter {
public void destroy() {
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding("UTF-8");
chain.doFilter(request, response);
}
public void init(FilterConfig arg0) throws ServletException {
}
}
解å³æ¹æ³ä¸ï¼
å½a.jsp页é¢ä¸formæ交çmethod="GET"æ¶ï¼éè¦ä¿®æ¹tomcat/confä¸çserver.xmlæ件ï¼å¨<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />è¿ä¸ªæ ç¾çåé¢å ä¸è¿ä¸ªå±æ§URIEncodingï¼"UTF-8"
è½ç¶å¯ä»¥ä½æ¯æåæçï¼è¥æä½ è¿æ»¤å¨æ²¡æé
ç½®ï¼ååªæ¯é
ç½®äºserver.xmlæ件ï¼è¿æ ·åªæå¨è¡¨åæ¯getä¼ å¼æ¶åæå¯ä»¥ï¼å½æ¯postæ¹å¼æ¶ï¼ç
§æ ·æ¯ä¹±ç ï¼
è¿æ¶æ们就å¯ä»¥çåºå¨tomcat5ä¸çpostä¸getä¼ å¼æ¹å¼æ¯ä¸ä¸æ ·çã
解å³æ¹æ³åï¼
è¦æ³å³è§£å³get请æ±å解å³post请æ±ï¼é£å°±ä¹è¦åæ¶å®ç°æ¹æ³äºåæ¹æ³ä¸ã
上一篇: jsp 解决乱码问题
下一篇: jsp 解决ajax乱码的问题