转载自:http://blog.csdn.net/hackerain/article/details/6776083 今天老师讲了jsp中四种传递参数的方法,我觉得总结一下,挺好的,以备后用! 1、form表单 2、request.setAttribute();和request.getAttribute();
转载自:http://blog.csdn.net/hackerain/article/details/6776083
今天老师讲了jsp中四种传递参数的方法,我觉得总结一下,挺好的,以备后用!
1、form表单
2、request.setAttribute();和request.getAttribute();
3、超链接:<a herf="index.jsp"?a=a&b=b&c=c>name</a>
4、<jsp:param>
下面一一举例说明:
1、form表单
form.jsp:
[html] view plain copy- <%@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:
[html] view plain copy- <%@page language="java" import="java.util.*" pageEncoding="GB2312"%>
- <html>
- <head>
- <title>
- result.jsp file
- </title>
- </head>
- <body bgcolor="ffffff">
- <%
- request.setCharacterEncoding("GB2312");
- String name=request.getParameter("name");
- name=new String(name.getBytes("iso-8859-1"),"GB2312");
- String pwd=request.getParameter("password");
- String[] hobby=request.getParameterValues("hobby");//注意这里的函数是getParameterValues()接受一个数组的数据
- %>
- <%
- if(!name.equals("") && !pwd.equals(""))
- {
- %>
- 您好!登录成功!<br/>
- 姓名:<%=name%><br/>
- 密码:<%=pwd%><br/>
- 爱好:<%
- for(String ho: hobby)
- {
- ho=new String(ho.getBytes("iso-8859-1"),"GB2312");
- out.print(ho+" ");
- }
- %>
- <%
- }
- else
- {
- %>
- 请输入姓名或密码!
- <%
- }
- %>
- </body>
- </html>
- String name=request.getParameter("name");
- name=new String(name.getBytes("iso-8859-1"),"GB2312");
为什么会出现中文乱码问题呢?因为Tomcat服务器默认的系统编码方式为iso-8859-1,你传递参数给服务器时,使用的是默认的iso-8859-1的编码方式,但是服务器向你返回信息时,是按page指令中设置的编码方式,如:<%@page language="java" import="java.util.*" pageEncoding="GB2312"%>,这样就混合了两种编码方式,所以会出现乱码,所以解决之道就是统一传递和接收的编码方式。
2、request.setAttribute()和request.getAttribute()
set.jsp:
[html] view plain copy- <%@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:
[html] view plain copy
- <%@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:
[html] view plain copy- <%@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:
[html] view plain copy
- <%@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:
[html] view plain copy- <%@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:
[html] view plain copy
- <%@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>