request.getPramamter()的用法

表單

<form action = "" method = "">
action

 action:目標地址,把請求交給誰處理;action可以不設置,也可以是空字符串(提交給自己處理)

method

 method: get | post,默認是get

get和post的區別
  1. get:參數比較小用get,要取服務器數據
  2. post:向服務器提交數據
  3. get會把參數顯示在地址欄,post不會
  4. 中文參數儘量不要使用get方式
  5. 兩種請求的請求體大小,get是有限制的
輸入手段

 文本框,密碼框,文本區域,下拉列表,但選框,複選框,隱藏框......,提交按鈕,重置按鈕  要求:往服務器傳值的地方必須指定name屬性,作爲參數名傳到服務器

<input type="text" name="code" class="....">

basePath

 在jsp代碼的上方能看到一個String變量basePath,被賦值爲

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

 對於我的web項目來說,basePath其實就等於http://localhost:8088/jsp/,所以當你想要訪問其他網頁,寫路徑的時候,可以直接利用basePath

提交信息給input_action.jsp頁面

 提交信息,分三步:

  1. 設置form的action屬性爲被提交的網站名稱
  2. 填寫輸入部分的name,後面獲取信息也要通過name
  3. 被提交網頁通過request.getPramamter(String arg0)方法獲取信息
<!-- input.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%>"><!-- 指定當前頁面相對路徑得根目錄在哪,目前的basePath = "http://localhost:8088/jsp/" -->
    
    <title>My JSP 'input.jsp' starting page</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">
    <jsp:include page = "../res.jsp"/>
  </head>
  
  <body class = "container">
    <div>
        <h3 class = "page-header">request.getPramamter()的用法</h3>
        <form action = "<%=basePath%>c02/input_action.jsp" method = "post" class = "form-horizontal">
            <div class = "form-group">
                <label class = "col-md-3 control-label">學號:</label>
                <div class = "col-md-5">
                    <input type = "text" name = "id" class = "form-control"><!-- 指定name -->
                </div>
            </div>
            <div class = "form-group">
                <label class = "col-md-3 control-label">姓名:</label>
                <div class = "col-md-5">
                    <input type = "text" name = "name" class = "form-control"><!-- 指定name -->
                </div>
            </div>
            <div class = "form-group">
                <div class = "col-md-offset-3 col-md-5">
                    <button type = "submit" class = "btn btn-primary">提交</button>
                </div>
            </div>
        </form>
    </div>
  </body>
</html>

 這裏我設置了action提交的時候傳給input_action.jsp這個網頁,傳輸方式爲post,兩個輸入框分別命名爲id和name

<!-- input_action.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>My JSP 'input_action.jsp' starting page</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">
  </head>
  <body>
    <%
        // 設置編碼格式,避免中文亂碼
        request.setCharacterEncoding("UTF-8");
        // 獲取請求參數,參數名爲id,對應學號
        String id = request.getParameter("id");
        // 獲取請求參數,參數名爲name,對應姓名
        String name = request.getParameter("name");
    %>
    <ul>
        <li>學號:<%= id %></li>
        <li>姓名:<%= name %></li>
    </ul>
  </body>
</html>

 爲了避免輸入框輸入中文,導致傳到input_action.jsp網頁的信息是亂碼,這裏要利用request.setCharacterEncoding("UTF-8")方法將其設置爲中文編碼格式。然後利用request.getParameter(String arg0)方法接受請求的參數,最後打印出來

中文亂碼問題解決辦法

  1. 將請求參數以ISO-8859-1編碼形式轉換成字節數組,再將字節數組轉換成字符串
  2. request.setCharacterEncoding(“UTF-8”);

訪問web手段

  1. 地址欄
http://localhost:8088/jsp/c02/input_action.jsp?name=x&code=1234

 只要在URL後面跟上一個"?",然後繼續跟上pname1=pvalue1&pname2=pvalue2&即可

  1. 超鏈接
<a href="<%= basePath %>c02/input_action.jsp?code=00000&name=張三">張三訪問</a>

題目:提交三條邊,輸出面積

<%@page import="java.util.regex.Pattern"%>
<%@ 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>My JSP 'input_action.jsp' starting page</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">
  </head>
  <body>
    <%
        String a = request.getParameter("a");
        String b = request.getParameter("b");
        String c = request.getParameter("c");
        if(a != "" && b != "" && c != "") {
            Pattern pattern = Pattern.compile("[1-9]");
            if(pattern.matcher(a).matches() && pattern.matcher(b).matches() && pattern.matcher(c).matches()) {
                double bian_a = Double.parseDouble(a);
                double bian_b = Double.parseDouble(b);
                double bian_c = Double.parseDouble(c);
                if(bian_a + bian_b <= bian_c || bian_a + bian_c <= bian_b || bian_b + bian_c <= bian_a) {
                    out.println("Sorry三條邊不能構成三角形,請重新輸入");
                %>
                    <a href = "<%= basePath %>c02/triangle.jsp">返回</a>
                <%
                } else {
                    double p = (bian_a + bian_b + bian_c) / 2.0;
                    out.println("邊長爲" + bian_a + "," + bian_b + "," + bian_c + "的面積爲:" + Math.sqrt(p * Math.abs(bian_a - p) * Math.abs(bian_b - p) * Math.abs(bian_c - p)));
                }
            } else {
                out.println("邊不能含有非數字信息,請重新輸入");
                %>
                    <a href = "<%= basePath %>c02/triangle.jsp">返回</a>
                <%
            }
        } else {
            out.println("缺少邊的信息,請重新輸入");
            %>
                <a href = "<%= basePath %>c02/triangle.jsp">返回</a>
            <%
        }
    %>
  </body>
</html>

 input.jsp就不附上了,原來的代碼改一下name,在新增一個輸入框就行了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章