JSP內建對象------request

來源:互聯網
上載者:User

標籤:encoding   content   連結   變數   擷取資料   html   文法   表示   內容   

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>request1.jsp</title></head><body>在該頁面下建立一個連結request2.jsp,並傳遞兩個參數<br>當使用 href="request2.jsp?id=2&user="  這種連結時,後面的地址及參數之間不可以隨便加空格<br><a href="request2.jsp?id=2&user=">開啟連結</a></body></html>
<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>request2.jsp</title></head><body>擷取傳遞的參數id、user <br><%String id = request.getParameter("id") ;      //擷取id參數的值String user = request.getParameter("user") ;      //擷取user參數的值String password = request.getParameter("password") ; //擷取password參數的值%>在使用request的getParameter()方法擷取傳遞參數值時,要注意:<br>1.如果指定的參數不存在,將返回null<br>2.如果指定了參數名,但未指定參數值,將返回Null 字元串 ""      <br>id參數的值:<%=id %><br>    user參數的值<%=user %><br>password參數的值<%=password %><br></body></html>

 ========================================================

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>request3.jsp</title></head><body>添加請求屬性 request.setAttribute() 文法:<br>request.setAttribute(String name , Object obj) ;<br>參數說明:<br>name : 表示變數名,String類型,在轉寄後的頁面擷取資料時,就是通過這個變數名來擷取資料的<br>obj :用於指定需要在request範圍內傳遞的資料為Object內容<br><%try //捕獲異常{    int a = 100 ;        int b = 0 ;        request.setAttribute("result", a/b)  ;        //儲存執行結果        }catch(Exception e){    request.setAttribute("result", "抱歉頁面發生錯誤") ; //儲存錯誤提示資訊}%><%request.setAttribute("name", "張三")  ;      //傳遞漢字%><jsp:forward page="request4.jsp" /></body></html>
<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>request4.jsp</title></head><body><%//擷取傳遞值,getAttribute() 返回Object類型,要轉型,String result = request.getAttribute("result").toString() ; String name = request.getAttribute("name").toString() ; %><%=result  %><br><%=name  %></body></html>

 

JSP內建對象------request

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.