• 5.request对象详解


    可以通过request对象获取表单提交的值,get或者post方式都是可以得

    例子:login.jsp表单

    <%@ page language="java" import="java.util.*"
        contentType="text/html; charset=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 'login.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">
    <!--
        <link rel="stylesheet" type="text/css" href="styles.css">
        -->
    
    </head>
    
    <body>
        <h1>用户注册</h1>
        <hr>
        <form action="request.jsp" name="regForm" 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="注册"></td>
                </tr>
            </table>
        </form>
    </body>
    </html>

    2.request.jsp接收表单的内容并且打印出来

    <%@ page language="java" import="java.util.*" contentType="text/html; charset=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 'login.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">
        <!--
        <link rel="stylesheet" type="text/css" href="styles.css">
        -->
    
      </head>
      
      <body>
       <h1>request内置对象</h1>
       <%
       request.setCharacterEncoding("utf-8");
        %>
       用户名<%=request.getParameter("username") %><br>
       爱好<%
     String[] favorites = request.getParameterValues("favorite");
     for(int i  = 0;i<favorites.length;i++){
     out.println(favorites[i]+"&nbsp;&nbsp");
     }
      %>  
       <hr>
      </body>
    </html>

    3.效果如下:

    注意编码的问题:在request接收的时候需要设置编码,否则中文会乱码

    request.setCharacterEncoding("utf-8");

    4.也可以通过简单的url传递参数而不通过提交表单的方式

    <a href = "request.jsp?username=cai" >URL传参</a>

    获取的时候一样通过request的方法

    <%=request.getParameter("username") %>

    但是url传递参数会出现中文乱码的问题,要解决需要打开tomcat目录下的conf的server.xml

    在这里添加一句,改成这样子

       <Connector port="8080" protocol="HTTP/1.1"
                   connectionTimeout="20000"
                   redirectPort="8443" URIEncoding="utf-8"/>

    重启tomcat服务器即可

    5.可以在request对象中保存一些属性,以键值对的形式存在

    设置password的值是123456

    <% request.setAttribute("password","123465");%>

    获取password的值

    <%=request.getAttribute("password")%>

    6.其他的函数

     获取请求体的MIME类型:<%=request.getContentType()%><br>
       返回请求用的协议类型以及版本号: <%=request.getProtocol()%><br>
       返回接受请求的服务器主机名:  <%=request.getServerName()%><br>
       服务器端口号 :<%=request.getServerPort()%><br>
      返回的编码格式 :<%=request.getCharacterEncoding()%><br>
      请求文件的长度 :<%=request.getContentLength()%><br>
      请求客户端的IP地址 <%=request.getRemoteAddr()%><br>
      请求的真实路径: <%=request.getRealPath("request.jsp")%><br>
      请求的上下文路径: <%=request.getContextPath()%><br>

    结果:

  • 相关阅读:
    课程教学网站(大三上课设)
    mybatis+servlet
    第一个struct2(Hello_World)
    servlet入门
    mybatis入门项目
    XML
    状态模式的介绍及状态机模型的函数库javascript-state-machine的用法和源码解析
    10个JavaScript难点
    markdown基本语法
    Mac OSX 平台安装 MongoDB
  • 原文地址:https://www.cnblogs.com/caimuqing/p/5757432.html
Copyright © 2020-2023  润新知