程序代码:
----------------index.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. http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<script type="text/javascript">
function getCheck(text){
location.href="/test/Check?flag="+text;
}
</script>
<body>
<a href="javascript:getCheck('dispatcher转向')">通过 '/testJSP/Check?flag=dispatcher转向 ' 转至 Check.java</a>
<br/>
<a href="javascript:getCheck('redirect重定向')">通过 '/testJSP/Check?flag=dispatcher重定向' 重定向 Check.java</a>
<br/>
<a href="javascript:getCheck('redirect外网')">通过 '/testJSP/Check?flag=redirect外网' 连接 Check.java</a>
</body>
</html>
--------------check-----
public void doGet(HttpServletRequest come,HttpServletResponse go)
throws ServletException,IOException{
String flag=come.getParameter("flag");
come.setAttribute("names","野马abc");
flag=URLDecoder.decode(flag, "utf-8");
if("dispatcher转向".equals(flag)) { //转发
RequestDispatcher rd=come.getRequestDispatcher("/0628/forward1.jsp?redirect="+URLEncoder.encode(flag,"utf-8"));//此处用带中文参数的url地址
System.out.println(flag);
rd.forward(come,go);
}else if("redirect重定向".equals(flag)){
go.sendRedirect("/test/0628/forward2.jsp?redirect="+flag);//此处用带中文参数的url地址
}else if("redirect外网".equals(flag)){
go.sendRedirect("http://www.baidu.com");
}
else {
System.out.println("都不匹配");
}
}
----- forward1----
<%@page import=" page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%!
public String transferCharacter(String s){//这个方法用来转变,对于表单get提交的中文这个方法是管用的,但在这里就不行
try{
return new String(s.getBytes("ISO-8859-1"),"utf-8");
}catch(UnsupportedEncodingException e){
throw new RuntimeException("不支持的字符集");
}
}
%>
<%
out.println("这是一个转发的例子==");
%>
<br/>
<%
String names=(String)request.getAttribute("names");
String para=transferCharacter(request.getParameter("redirect"));
out.println("属性name的值:"+names);
%>
<br/>
<%
out.println("参数para的值:"+para);//此处para的值一直取不到,一直是乱码
%>