JSP内置对象---request对象

这里写图片描述
我们以表单提交的方式(post)和超链接(get)来测试request的这些方法
首先下面是一个注册页面
reg.jsp

<%@ 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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <h1>用户注册</h1>
    <hr>
    <form name = "regFrom" action = "request.jsp" method = "post">
        <table>
            <tr>
                <td>用户名:</td>
                <td>
                    <input type = "text" name="username" />
                </td>
            </tr>
            <tr>
                <td>爱好:</td>
                <td>
                    <input type="checkbox" name="favorite" value="read">读书
                    <input type="checkbox" name="favorite" value="music">音乐
                    <input type="checkbox" name="favorite" value="movie">电影
                    <input type="checkbox" name="favorite" value="Internet">上网
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <input type="submit" value="submit" />
                </td>
            </tr>
        </table>
    </form>

    <br>
    <br>
    <a href = "request.jsp?username=lisi">
        测试超链接
    </a>
</body>
</html>

request.jsp

<%@ 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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <h1>request内置对象</h1>
    <%
        request.setCharacterEncoding("utf-8");//解决中文乱码,无法解决url的中文乱码
        request.setAttribute("password", "1234");
    %>
    用户名:<%=request.getParameter("username") %>
    爱好:<%
        if(request.getParameterValues("favorite") != null){
            String [] f = request.getParameterValues("favorite");
            for(String s :f){
                out.println(s+"&nbsp;&nbsp<br>");
            }
        }

        %>
    密码:<%=request.getAttribute("password") %><br>
    请求体的MIME类型:<%=request.getContentType() %><br>
    协议以及版本号:<%=request.getProtocol() %><br>
    服务器的主机名:<%=request.getServerName() %><br>
    服务的端口号:<%=request.getServerPort() %><br>
    请求文件的长度:<%=request.getContentLength() %><br>
    请求的客户端的IP地址<%=request.getRemoteAddr() %><br>
    请求的真实路径:<%=request.getRealPath("request.jsp") %><br>
    请求的上下文路径:<%=request.getContextPath() %><br>
</body>
</html>

下面是结果
这是注册界面,现在在注册界面中输入‘张山’,爱好选‘读书’和‘电影’
这里写图片描述
结果为:
这里写图片描述
再测试超链接:
这里写图片描述
这也同时测试request的其他方法

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值