Servlet学习(四)使用request读取所有参数

程序实例:

ShowAllParameters.htm

<html>
    <head>
        <title>A sample show parameters</title>
    </head>
    <body>A sample show parameters  
        <form method="get" action="servlet/ShowParameters">
            Item Number:<input type="text" name="itemNum"><br>
            Quantity:<input type="text" name="quantity"><br>
            Price each:<input type="text" name="price" value="$"><br>
            First Name:<input type="text" name="firstName"><br>
            Last Name:<input type="text" name="lastName"><br>
            Middle Initial:<input type="text" name="initial"><br>
            
            Shipping Address:<textarea name="address" row="3" cols="40"></textarea><br>
            Credit Card:<br>
            <!--单选框示例-->
              <input type="radio" name="cardType" value="Visa">Visa<br>
              <input type="radio" name="cardType" value="Master Card">Master Card<br>
              <input type="radio" name="cardType" value="Amex">Amex<br>
              <input type="radio" name="cardType" value="Discover">Discover<br>
              <input type="radio" name="cardType" value="Java SmartCard">Java SmartCard<br>
            <!--密码输入框-->
            Credit Card password:<input type="password" name="cardNum"><br>
            Repeat Credit Card password:<input type="password" name="cardNum"><br><br>
            
            <center><input type="submit" value="Submit Order"></center>
            
        </form>
    </body>
</html>
ShowParameters.java

public class ShowParameters extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=gb2312");
        PrintWriter out = response.getWriter();
        
        String title = "Reading All Request Parameters";
        out.println("<html><head><title>读取所有参数</title></head>"
                + "<BODY BGCOLOR='#FDF5E6'>\n" + "<H1 ALIGN=CENTER>" + title
                + "</H1>\n" + "<table border=1 align=center>\n"
                + "<TR BGCOLOR=\"FFAD00\">\n"
                + "<TH>Parameter Name<TH>Parameter Value(s)");
        
        // Returns an Enumeration of String objects containing the names of the parameters contained in this request. 
        Enumeration paramNames = request.getParameterNames();
        // Tests if this enumeration contains more elements. 
        while(paramNames.hasMoreElements()) {
            // Returns the next element of this enumeration if this enumeration object has at least one more element to provide. 
            String paraName = (String)paramNames.nextElement();
            out.println("<tr><td>" + paraName + "/n<td>");
            // Returns an array of String objects containing all of the values the given request parameter has, or null if the parameter does not exist. 
            // 注意参数paraName(变量)不能加双引号,否则就是找参数名叫paraName的对应值了。
            String[] paramValues = request.getParameterValues(paraName);
            // 这个参数只有一个值
            if(paramValues.length == 1) {
                String paramValue = paramValues[0];
                if(paramValue.length() == 0) {
                    out.println("<I>no value</I>");
                } else {
                    out.println(paramValue);
                }
            }else {
                // 这个参数有好几条值
                out.println("<UL>");
                for(int i = 0; i < paramValues.length; i++) {
                    out.println("<LI>" + paramValues[i]);
                }
                out.println("</UL>");
            }
        }
        out.println("</table>/n<body><html>");
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("doPost");
        doGet(request,response);
    }

}
访问localhost:8080/testServlet/ShowAllParameters.htm



提交,获得request读取的全部数据



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值