整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:

Servlet从上一个页面获取参数(中文乱码解决方法)

文乱码问题是比较常见和烦人的问题,本文通过一个小程序介绍了如何通过Servlet从上一个页面获取参数,

方法很简单:调用request.getParameter(String s)方法。

解决中文乱码问题的方法是:

首先要设置response响应的格式:response.setContextType("text/html;charSet=GBK");

然后在加上request.setCharacterEncoding("GBK")

需要注意的是这是解决post方式提交的内容的中文乱码问题。

解决get方式提交内容的中文乱码问题的方法:

在Tomcat->conf文件夹->server.xml-->connecter标签里加上:

URIEncoding="GBK"(注意:在xml里面“=”两边不要有空格)

关于Post和Get之间的区别可以看我转载的另一篇博文:

HTTP POST GET 本质区别详解

例子:

1.threeparams.html


  1. <html>
  2. <body>
  3. <form method="post" action="Threeparams">
  4. <table>
  5. <tr>
  6. <td>
  7. param1
  8. </td>
  9. <td>
  10. <input name="p1" type="text"/>
  11. </td>
  12. </tr>
  13. <tr>
  14. <td>
  15. param2
  16. </td>
  17. <td><input name="p2" type="text"/>
  18. </td>
  19. </tr>
  20. <tr>
  21. <td>
  22. param3
  23. </td>
  24. <td><input name="p3" type="text"/>
  25. </td>
  26. </tr>
  27. <td><input type="submit" value="submit">
  28. </td>
  29. </tr>
  30. </table>
  31. </form>
  32. </body>
  33. </html>

2.ThreeParams.java


  1. import java.io.*;
  2. import javax.servlet.ServletException;
  3. import javax.servlet.http.*;
  4. public class ThreeParams extends HttpServlet{
  5. @Override
  6. protected void doGet(HttpServletRequest req, HttpServletResponse resp)
  7. throws ServletException, IOException {
  8. doPost(req,resp);
  9. }
  10. @Override
  11. protected void doPost(HttpServletRequest req, HttpServletResponse resp)
  12. throws ServletException, IOException {
  13. resp.setContentType("text/html;charset=GBK");
  14. //解决post方式提交内容的中文乱码
  15. //一定要卸载存取第一个参数之前
  16. //不要调用resp.setCharacterEncoding("GBK");
  17. req.setCharacterEncoding("GBK");
  18. //解决get方式乱码问题:修改server.xml中的connector标签-->URIEncoding="GBK"
  19. PrintWriter out = resp.getWriter();
  20. out.println(req.getParameter("p1"));
  21. out.println("</br>");
  22. out.println(req.getParameter("p2"));
  23. out.println("</br>");
  24. out.println(req.getParameter("p3"));
  25. out.println("</br>");
  26. }
  27. }

补充:

上面的这个例子.html中每个name都不一样,如果有多个一样的name时,可以按如下的方法来获取:

1)Enumeration paramNames = request.getParameterNames()

调用此方法获得所有参数的名字,返回一个Enumeration

2) while(paramNames.hasMoreElements()){

String paramName = (String)paramNames.nextElement();

String[] paramValues = request.getParameterValues(paramName);

...

sp中四种传递参数的方法,我觉得总结一下,挺好的,以备后用!

1、form表单

2、request.setAttribute();和request.getAttribute();

3、超链接:<a herf="index.jsp"?a=a&b=b&c=c>name</a>

1、form表单

form.jsp:

<%@page contentType="text/html; charset=GB2312"%> 
<html> 
 <head> 
 <title> 
 form.jsp file 
 </title> 
 </head> 
 
 <body style="background-color:lightblue"> 
 
 <h2 style="font-family:arial;color:red;font-size:25px;text-align:center">登录页面</h2> 
 
 <form action="result.jsp" method="get" align="center"> 
 姓名:<input type="text" name="name" size="20" value="" maxlength="20"><br/> 
 
 密码:<input type="password" name="password" size="20" value="" maxlength="20"><br/> 
 
 <!--在爱好前空一个空格,是为了排版好看些--> 
 
 爱好:<input type="checkbox" name="hobby" value="唱歌">唱歌 
 <input type="checkbox" name="hobby" value="足球">足球 
 <input type="checkbox" name="hobby" value="篮球">篮球<br/><br/> 
 
 <input type="submit" name="submit" value="登录"> 
 <input type="reset" name="reset" value="重置"><br/> 
 </form> 
 
 </body> 
</html>

result.jsp:

 1 <%@page language="java" import="java.util.*" pageEncoding="GB2312"%> 
 2 <html> 
 3 <head> 
 4 <title> 
 5 result.jsp file 
 6 </title> 
 7 </head> 
 8 
 9 <body bgcolor="ffffff"> 
10 <% 
11 request.setCharacterEncoding("GB2312"); 
12 
13 String name=request.getParameter("name"); 
14 name=new String(name.getBytes("iso-8859-1"),"GB2312"); 
15 
16 String pwd=request.getParameter("password"); 
17 String[] hobby=request.getParameterValues("hobby");//注意这里的函数是getParameterValues()接受一个数组的数据 
18 
19 %> 
20 
21 <% 
22 if(!name.equals("") && !pwd.equals("")) 
23 { 
24 %> 
25 
26 您好!登录成功!<br/> 
27 姓名:<%=name%><br/> 
28 密码:<%=pwd%><br/> 
29 爱好:<% 
30 for(String ho: hobby) 
31 { 
32 ho=new String(ho.getBytes("iso-8859-1"),"GB2312"); 
33 out.print(ho+" "); 
34 } 
35 %> 
36 <% 
37 } 
38 else 
39 { 
40 %> 
41 请输入姓名或密码! 
42 <% 
43 } 
44 %> 
45 </body> 
46 </html>

注意:form表单的提交方式为get,在参数传递时会遇到中文乱码的问题,一个简单的解决方法是,将接受到的字符串先转换成一个byte数组,再用String构造一个新的编码格式的String,如:

1 String name=request.getParameter("name"); 
2 name=new String(name.getBytes("iso-8859-1"),"GB2312"); 

如果form表单的提交方式为post,解决乱码问题的简单办法是,使用 request.setCharacterEncoding("GB2312");设置request的编码方式。

为什么会出现中文乱码问题呢?因为Tomcat服务器默认的系统编码方式为iso- 8859-1,你传递参数给服务器时,使用的是默认的iso-8859-1的编码方式,但是服务器向你返回信息时,是按page指令中设置的编码方式, 如:<%@page language="java" import="java.util.*" pageEncoding="GB2312"%>,这样就混合了两种编码方式,所以会出现乱码,所以解决之道就是统一传递和接收的编码方式。

2、request.setAttribute()和request.getAttribute()

set.jsp:

<%@page contentType="text/html; charset=GB2312"%> 
<html> 
 <head> 
 <title> 
 set.jsp file 
 </title> 
 </head> 
 
 <body style="background-color:lightblue"> 
 <% 
 request.setAttribute("name","心雨"); 
 %> 
 <jsp:forward page="get.jsp"/> 
 </body> 
</html>

get.jsp:

<%@page contentType="text/html; charset=GB2312"%> 
<html> 
 <head> 
 <title> 
 get.jsp file 
 </title> 
 </head> 
 
 <body style="background-color:lightblue"> 
 <% 
 out.println("传递过来的参数是:"+request.getAttribute("name")); 
 %> 
 </body> 
</html> 

request.setAttribute()和request.getAttribute()是配合<jsp:forward>或是include指令来实现的。

3、超链接:<a herf="index.jsp?a=a&b=b&c=c">name</a>

href.jsp:

<%@page contentType="text/html; charset=GB2312"%> 
<html> 
 <head> 
 <title> 
 href.jsp file 
 </title> 
 </head> 
 
 <body style="background-color:lightblue"> 
 <a href="getHerf.jsp?name=心雨&password=123">传递参数</a> 
 </body> 
</html> 

getHref.jsp:

<%@page contentType="text/html; charset=GB2312"%> 
<html> 
 <head> 
 <title> 
 getHref.jsp file 
 </title> 
 </head> 
 
 <body style="background-color:lightblue"> 
 <% 
 String name=request.getParameter("name"); 
 name=new String(name.getBytes("iso-8859-1"),"gb2312"); 
 
 out.print("name:"+name); 
 %> 
 <br/> 
 <% 
 out.print("password:"+request.getParameter("password")); 
 %> 
 </body> 
</html> 

这种传递参数的方法和form表单的get方式类似,是通过地址栏传递的参数,其乱码解决方法也和form 的get方式一样。

4、<jsp:param>

param.jsp:

<%@page contentType="text/html; charset=GB2312"%> 
<html> 
 <head> 
 <title> 
 param.jsp file 
 </title> 
 </head> 
 
 <body style="background-color:lightblue"> 
 
 <%request.setCharacterEncoding("GB2312");%> 
 
 <jsp:forward page="getParam.jsp"> 
 <jsp:param name="name" value="心雨"/> 
 <jsp:param name="password" value="123"/> 
 </jsp:forward> 
 
 </body> 
</html>

getParam.jsp:

<%@page contentType="text/html; charset=GB2312"%> 
<html> 
 <head> 
 <title> 
 getParam.jsp file 
 </title> 
 </head> 
 
 <body style="background-color:lightblue"> 
 <% 
 String name=request.getParameter("name"); 
 out.print("name:"+name); 
 %> 
 <br/> 
 <% 
 out.print("password:"+request.getParameter("password")); 
 %> 
 </body> 
</html> 

这里发现了一个奇怪的问题,还是在中文乱码的问题上,在form表单的例子中,如果传递方式为post,则只需要在接收参数的页面设置request的编 码方式就可以了,即request.setCharacterEncoding("GB2312");,注意是在接收参数的页面,如果将该句放到form 表单里,那么不起作用,仍然是乱码。而在本例中,为了使传递的参数不出现乱码,却是将 request.setCharacterEncoding("GB2312");放在发送参数的页面中,才会正常显示中文,放在接收参数的页面中,不起 作用。也许这就是<jsp:param>和form表单传递参数不同的地方。为什么会有这个不同呢?可能是因为form表单中的参数是由客户 端传送到服务端上的,需要经过一个request的打包过程,但是<jsp:param>传递的参数本身就是在服务器端的,不需要经历由客户 端到服务端这么一个过程,但是服务器里的参数传递是这么回事呢?这个问题,我不知道了!真是知识是一个扩大的圆圈,你知道的越多,那么不知道的就越多!努 力吧!

JavaScript中,将GET请求的参数串转换为POST请求格式通常意味着将查询字符串(query string)转换为一个对象,然后可以通过某种方式(例如,通过fetch API或axios库)发送POST请求。这里是一个基本的步骤指南和示例代码:


步骤指南


  1. 解析查询字符串:使用URLSearchParams API或正则表达式等方法解析GET请求的查询字符串。
  2. 构建POST请求体:将解析后的参数转换为一个对象,这个对象可以作为POST请求的请求体(body)。
  3. 发送POST请求:使用fetch API、axios或其他HTTP客户端库发送POST请求。


示例代码


假设你有一个GET请求的URL,如https://www.bjtimes.cc/api?param1=value1¶m2=value2,你可以这样将其转换为POST请求格式: