一,Jsp页面的三种传值方式
1,地址栏传值(get提交)
2,a标签传值 (get提交)
3,表单提交 (默认get方式提交)
Get/post取中文都会乱码。
Jsp的内置对象request取值。
代码演示。
- 地址栏传值(get)
<%// 地址栏输入 http://localhost:8080/ThirdJsp?test.jsp&name=麦?age=19String name = request.getParameter("name");String age = request.getParameter("age");// 解决get提交方式的乱码name = new String(name.getBytes("ISO8859-1"), "utf-8");out.print(name + age);%>
-
a标签传值(get)
index.jsp页面上请求test.jsp
<a href = "test.jsp?name=麦&age=19"> 点我 </a>
test.jsp页面上的取值代码与上面相似,也要用(ISO-8859-1)转码
-
post表单提交
test.jsp页面上
<form action="index.jsp" method = "post"><input type = "text" name = "name"/><input type = "submit" value = "提交"/>
</form>
index.jsp
<%
request.setCharacterEncoding("UTF-8");
String name = request.getParameter("name");
out.print(name);
%>
结论:get的转码方式(ISO-8859-1)post也适用,但post只转成utf-8就行了。
二,非jsp和jsp页面间传值
地点:页面和Servlet之间
人物:表单,checkbox,radio,超链接,pageContext,request
Session,application,el表达式,下拉框select
情节如下:
- pageContext
<% // 页面的上下文,本页面pageContext.setAttribute("p", "pageContext");String p = pageContext.getAttribute("p").toString();out.print(p);%>
- 超链接
页面
<button onclick = "send(12)">点我传值</button><script type="text/javascript">function send(param) {location.href = "s2_12?param=" + param; }</script>
servlet取值
// HttpServletRequest req 对象取值String param = req.getParameter("param");if (param != null)System.out.println(param);
- checkbox和radio两种框的传值
页面
<div><form action="s2_12" method="post"><input type = "checkbox" name = "checkBox" value = "A">A<input type = "checkbox" name = "checkBox" value = "B">B<input type = "checkbox" name = "checkBox" value = "C">C<input type = "checkbox" name = "checkBox" value = "D">D<br/><input type = "radio" name = "gender" value = "male">male<input type = "radio" name = "gender" value = "female">female<br/><input type = "submit" value = "提交"/></form></div>
servlet取值
// 得到checkbox和radio的值 比request大的,可以用session取,也可用application取String[] cks = req.getParameterValues("checkBox");if (cks != null) {for (String str : cks) {System.out.print(str + " ");} }System.out.println();param = req.getAttribute("gender");//req.getParameter("param");if (param != null)System.out.println(param);
- servlet传值到界面,用el表达式+c标签在界面取值
servlet
//2, 传值到界面 request session servletContext 也可以传List<String> list = new ArrayList<String>();list.add("abc");list.add("jack");list.add("rose");req.setAttribute("strList", list);req.setAttribute("session", "session");// 转发req.getRequestDispatcher("J2_12.jsp").forward(req, resp);
页面取值
<c:forEach items = "${strList}" var = "str"><%-- 页面上el表达式取值+c标签 --%>${ strList }</c:forEach>${ session }<br/>
- select下拉框的取值
jsp页面
<form method = "post" action = "getSelectServlet"><select name = "mySelect"><%-- 有value,后台就取的value,没得value,取的值就是jack --%><option>jack</option><option value = "2">rose</option><option value = "3">tom</option></select><input type = "submit" value = "233"></form>
servlet
protected void service(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {req.setCharacterEncoding("utf-8");String str = req.getParameter("mySelect");System.out.println(str);}
- session 和 application的传值
首先,搞清楚session和application是什么?并且作用域范围?
- session指的是会话,你用session.setAttribute(key, value) 这个key-value对在当前浏览器中默认保存30分钟。在当前浏览器任意的页面可以取到。主要用于保存用户的登录信息。
- application指的是web应用程序,只要服务器不挂掉,application一直存在。用来统计访问次数等。它是公开的,服务器开启,所以,你用Google和IE都可以取到application的值。
- 先访问下面的jsp
// Scope_Session_Application.jsp设置值<% application.setAttribute("a1", "application内置对象的值");session.setAttribute("s1", "session内置对象的值");%>
- 测试session和application, 访问MyJsp.jsp
// MyJsp.jsp取值application的值<%=application.getAttribute("a1")%> <br/>session的值<%=session.getAttribute("s1")%>
- 用IE访问MyJsp.jsp
三,服务器响应编码
protected void service(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {// 设置post的请求编码req.setCharacterEncoding("utf-8");// 设置响应编码resp.setContentType("text/html; charset=utf-8");resp.getWriter().write("<a href='index.jsp'>首页</a>");}
页面显示
更多见jsp九大内置对象和作用域