日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

web02--jsp数据传递

發布時間:2025/3/20 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 web02--jsp数据传递 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.創建一個login.jsp登陸界面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head><base href="<%=basePath%>"><title>login.jsp</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--></head><body><%-- action:我們需要提交的地址 method:請求的方式 --%><form action="doMain.jsp" method="get"><table><tr><td>用戶名:</td><td><input type="text" name="userName"></td></tr><tr><td>密碼:</td><td><input type="password" name="password"></td></tr><tr><td><input type="submit" value="登錄"></td></tr></table></form></body> </html>

2.創建對應的處理界面doMain.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" contentType="text/html; charset=ISO-8859-1" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head><base href="<%=basePath%>"><title>doMain.jsp</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--></head><body><h1>登錄成功</h1> <%-- login.jsp中的form表單 get方式請求亂碼 01.治標不治本 不推薦使用String userName=new String(name.getBytes("iso-8859-1"),"utf-8");02.治本在服務器中的conf文件夾中找到server.xml文件中的Connector節點中 新增屬性URIEncoding="UTF-8"--%><%//根據login.jsp頁面 name的屬性值 獲取 value //post亂碼 解決 是不是每個頁面都需要設置 請求編碼格式??? 后面 我們會用Filterrequest.setCharacterEncoding("utf-8"); //請求的編碼response.setCharacterEncoding("utf-8"); //響應的編碼String name=request.getParameter("userName"); //獲取用戶名String pwd=request.getParameter("password"); //獲取密碼%><%-- 就是想把login頁面的值 傳遞給last.jsp --%><%out.print("用戶名:"+name);out.print("密碼:"+pwd);//把從login頁面拿到的值 存儲到了 request作用域中了request.setAttribute("userName", name);request.setAttribute("password", pwd);//轉發到了last.jsp 攜帶了數據 last頁面能取得數據//request.getRequestDispatcher("last.jsp").forward(request, response);//重定向last.jsp 數據都會丟失! last頁面不能取得數據response.sendRedirect("last.jsp");%><%-- get請求 --%><a href="last.jsp?userName=小黑黑2&password=123456">跳轉到最后一個界面</a></body> </html>

3.創建last.jsp看能不能獲取login.jsp的值?

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head><base href="<%=basePath%>"><title>last.jsp</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--></head><body><h1>last.jsp</h1><%//在doMain中使用了轉發后 能直接獲取 login頁面的值String name=request.getParameter("userName"); //獲取用戶名String pwd=request.getParameter("password"); //獲取密碼out.print("用戶名:"+name);out.print("密碼:"+pwd);%><%//從request.getAttribute()取值request.setCharacterEncoding("utf-8"); //請求的編碼String name1=(String)request.getAttribute("userName");//獲取用戶名String pwd2=(String)request.getAttribute("password"); //獲取密碼out.print("用戶名:"+name1);out.print("密碼:"+pwd2);%></body> </html>

?4.通過request獲取界面的多個值

01.創建request1.jsp

<form action="request2.jsp" method="post"><input type="checkbox" name="box" value="別玩手機">別玩手機"<input type="checkbox" name="box" value="就玩手機">就玩手機"<input type="checkbox" name="box" value="還玩手機">還玩手機"<input type="checkbox" name="box" value="真玩手機">真玩手機"<input type="checkbox" name="box" value="玩手機">玩手機"><button type="submit">提交</button></form>

02.創建request2.jsp

<body><%request.setCharacterEncoding("utf-8");//獲取選中復選框的值String [] boxs=request.getParameterValues("box");//首先進行判斷 必須先判斷非空if(boxs!=null&&boxs.length!=0){for(String box:boxs){out.print(box+"<br/>");}}else{//重定向到request1界面response.sendRedirect("request1.jsp");}%><h1>request對象常用的方法</h1>獲取http請求中使用的方法名稱 <%=request.getMethod() %><br/>獲取http請求中調用servlet的url部分 <%=request.getServletPath() %><br/>獲取http請求中MIME類型 <%=request.getContentType() %><br/>獲取請求中服務器主機名稱 <%=request.getServerName() %><br/>獲取請求中服務器的端口號名稱 <%=request.getServerPort() %><br/>獲取請求中服務器的ip地址 <%=request.getRemoteAddr()%><br/>獲取請求中服務器的ip地址 <%=request.getRemoteHost()%><br/>獲取請求中使用的協議 <%=request.getScheme() %><br/></body>

? ? ? ? ? ? ? ? ? ? ? ???

轉發和重定向

?

轉載于:https://www.cnblogs.com/xtdxs/p/7094442.html

總結

以上是生活随笔為你收集整理的web02--jsp数据传递的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。