日期:2014-05-16 浏览次数:20320 次
第一种方法是通过get方式传递参数:
parameter.jsp代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%> <html> <head> <title>这是我的第一个JSP页面</title> </head> <body> <a href="ParaServlet?a=中国">paratest</a> </body> </html>
?ParaServlet.java的代码如下:
import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.ServletException; public class ParaServlet extends HttpServlet { public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,java.io.IOException{ String str = request.getParameter("a"); String str1 = new String(str.getBytes("ISO-8859-1"),"GBK"); System.out.println(str1); } public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,java.io.IOException{ } }
?
get方式通过
String str1 = new String(str.getBytes("ISO-8859-1"),"GBK");
来转码。
第二种是通过post方式传递参数:
parameter2.jsp代码如下:
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%> <html> <head> <title>这是我的第一个JSP页面</title> </head> <body> <form method="post" action="ParaServlet"> <input type="text" name="test"> <input type="submit" value="提交"> </form> </body> </html>
?ParaServlet.java的代码如下:
import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.ServletException; public class ParaServlet extends HttpServlet { public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,java.io.IOException{ } public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,java.io.IOException{ request.setCharacterEncoding("GBK"); String str = request.getParameter("test"); System.out.println(str); } }
?
post方式是通过request.setCharacterEncoding("GBK");?来转码的
如果想要传递中文参数? get方式比较少用? 因为get方式麻烦
?