一、JSP TO JSP
<form method="get" id="editform" action="ss.jsp">
<input type="text" name="a" id="a" value="aaaa风格化"/>
<input type="text" name="b" id="b" value="b&*的风格"/>
<input type="text" name="c" id="c" value="ccc风格化"/>
<input type="text" name="d" id="d" value="ddd风格化"/>
<input type="submit" value="sbumit" />
</form>
?
<body>
<%String a=new String(request.getParameter("a").getBytes("ISO8859-1"),"GBK");
String b=new String(request.getParameter("b").getBytes("ISO8859-1"),"GBK");
String c=request.getParameter("a");
String d=request.getParameter("b"); %>
a:<%=a %>//结果显示乱码
b:<%=b %>//结果显示乱码
c:<%=c %>//正常
d:<%=d %>//正常
</body>
?如果改用get则刚好相反
?
二、JSP TO JAVA
1、使用post通过struts传递参数到action
<form method="post" action="<%=path %>/go.do" >
<input type="text" name="a" id="a" value="12a*(的风格" /><br>
<input type="text" name="b" id="b" value="12a*(的风格" />
<br><input type="text" name="c" id="c" value="12a*(的风格" /><br>
<input type="text" name="d" id="d" value="12a*(的风格" /><br>
<input type="submit" vluae="submit"/>
</form>
action
String a=new String(request.getParameter("a").getBytes("ISO8859-1"),"GBK");//正常
String b=new String(request.getParameter("b").getBytes("ISO8859-1"),"GBK");//正常
String c=request.getParameter("c");//乱码
String d=request.getParameter("d");//乱码
?如果改用get则刚好相反
?
2、使用编码函数
<script type="text/javascript">
function show(){
var f="#wer中文测试";
f = encodeURIComponent(encodeURIComponent(f));
test.value = f;
var url = "servlet/Test?f="+f;
var rtv = showModalDialog(url,"","dialogWidth:820px; dialogHeight:620px; status:0;help:0;scrolling:auto");
}
</script>
<body><input id="test" value=""> <input type="button" value="TEST" onclick="show()" />
</body>
?
package com.yourcompany.struts.action;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.yourcompany.struts.form.TestForm;
public class TestAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
TestForm testForm = (TestForm) form;
response.setCharacterEncoding("UTF-8");
String name=testForm.getName();
String pwd=testForm.getPwd();
response.setCharacterEncoding("UTF-8");
String myname=request.getParameter("myname");
myname=URLDecoder.decode(myname, "UTF-8");
String mypwd=request.getParameter("mypwd");
mypwd=URLDecoder.decode(mypwd, "UTF-8");
System.out.println(name);
System.out.println(pwd);
System.out.println(myname);
System.out.println(mypwd);
request.setAttribute("name", name);
request.setAttribute("pwd", pwd);
request.setAttribute("myname", myname);
request.setAttribute("mypwd", mypwd);
return mapping.findForward("go");
}
}
?
?
?
?
?