Jsp传值方式(乱码问题的解决)
生活随笔
收集整理的這篇文章主要介紹了
Jsp传值方式(乱码问题的解决)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一,Jsp頁面的三種傳值方式
1,地址欄傳值(get提交)
2,a標簽傳值 (get提交)
3,表單提交 (默認get方式提交)
Get/post取中文都會亂碼。
Jsp的內置對象request取值。
代碼演示。
- 地址欄傳值(get)
-
a標簽傳值(get)
index.jsp頁面上請求test.jsp
test.jsp頁面上的取值代碼與上面相似,也要用(ISO-8859-1)轉碼
-
post表單提交
test.jsp頁面上
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
情節如下:
頁面
servlet取值
// HttpServletRequest req 對象取值String param = req.getParameter("param");if (param != null)System.out.println(param);頁面
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
頁面取值
<c:forEach items = "${strList}" var = "str"><%-- 頁面上el表達式取值+c標簽 --%>${ strList }</c:forEach>${ session }<br/>jsp頁面
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是什么?并且作用域范圍?
- 先訪問下面的jsp
- 測試session和application, 訪問MyJsp.jsp
- 用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九大內置對象和作用域
總結
以上是生活随笔為你收集整理的Jsp传值方式(乱码问题的解决)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: javafx如何调节按钮位置_安全带高度
- 下一篇: 什么是腾讯云图数据可视化?它有哪些特性以