標籤: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