jsp中四种传递参数的方法

网友投稿 784 2022-10-17

jsp中四种传递参数的方法

jsp中四种传递参数的方法

jsp中四种传递参数的方法如下:

1、form表单

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

3、超链接:name

4、

下面一一举例说明:

1、form表单

form.jsp:

1. <%@page contentType="text/html; charset=GB2312"%>2. 3. 4. 5. form.jsp file6. 7. 8. 9. 10. 11.

登录页面

12. 13.
14.
15. 16.
17. 18. 19. 20. 唱歌21. 足球22. 篮球

23. 24. 25.
26.
27. 28. 29.

result.jsp:

1. <%@page language="java" import="java.util.*" pageEncoding="GB2312"%>2. 3. 4. 5. result.jsp file6. 7. 8. 9. 10. <%11. request.setCharacterEncoding("GB2312");12. 13. name=request.getParameter("name");14. name=new String(name.getBytes("iso-8859-1"),"GB2312");15. 16. pwd=request.getParameter("password");17. hobby=request.getParameterValues("hobby");//注意这里的函数是getParameterValues()接受一个数组的数据18. 19. >20. 21. <%22. if(!name.equals("") && !pwd.equals(""))23. {24. >25. 26.
27. <%=name%>
28. <%=pwd%>
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. else39. {40. >41. 请输入姓名或密码!42. <%43. }44. >45. 46.

注意: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:

1. <%@page contentType="text/html; charset=GB2312"%>2. 3. 4. 5. set.jsp file6. 7. 8. 9. 10. <%11. request.setAttribute("name","心雨");12. >13. 14. 15.

get.jsp:

1. <%@page contentType="text/html; charset=GB2312"%>2. 3. 4. 5. get.jsp file6. 7. 8. 9. 10. <%11. out.println("传递过来的参数是:"+request.getAttribute("name"));12. >13. 14.

request.setAttribute()和request.getAttribute()是配合或是include指令来实现的。

3、超链接:name

href.jsp:

1. <%@page contentType="text/html; charset=GB2312"%>2. 3. 4. 5. href.jsp file6. 7. 8. 9. 10. 传递参数11. 12.

getHref.jsp:

1. <%@page contentType="text/html; charset=GB2312"%>2. 3. 4. 5. getHref.jsp file6. 7. 8. 9. 10. <%11. name=request.getParameter("name");12. name=new String(name.getBytes("iso-8859-1"),"gb2312");13. 14. out.print("name:"+name);15. >16.
17. <%18. out.print("password:"+request.getParameter("password"));19. >20. 21.

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

4、

param.jsp:

1. <%@page contentType="text/html; charset=GB2312"%>2. 3. 4. 5. param.jsp file6. 7. 8. 9. 10. 11. <%request.setCharacterEncoding("GB2312");%>12. 13. 14. 15. 16. 17. 18. 19.

getParam.jsp:

1. <%@page contentType="text/html; charset=GB2312"%>2. 3. 4. 5. getParam.jsp file6. 7. 8. 9. 10. <%11. name=request.getParameter("name");12. out.print("name:"+name);13. >14.
15. <%16. out.print("password:"+request.getParameter("password"));17. >18. 19.

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

龙腾一族至尊龙骑

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Purenode- NodeJS Web 开发框架
下一篇:Gym - 101550C Card Hand Sorting——思维
相关文章

 发表评论

暂时没有评论,来抢沙发吧~