JSP内置对象Request的使用方法

发布时间 2023-04-10 10:47:56作者: Freeland98

  JSP request javax.servlet.http.HttpServletRequest 的实例对象,主要用来获取客户端提交的数据。request 对象提供了一系列方法,可以获取请求参数信息、表单数据、HTTP 头信息、cookie HTTP 请求方法等。Request对象最长见的应用就是通过调用它的getParameter方法获取客户端浏览器提交的表单数据。通过request对象获取表单数据时,不同表单元素需要不同的处理方法,如文本元素、复选元素、文件元素等,下面的例子程序演示了如何获取并加工处理不同表单元素的方法(文件上传除外)。例子程序中用到的表单界面文件请从下面的网址获取:https://www.cnblogs.com/Freeland98/p/17214046.html,并设置表单(form1)的action属性值为stuInfoPro.jsp(如:<form id="form1" name="form1" action="stuInfoPro.jsp" method="post"  target="" align="center">)。表单数据获取例子程序(stuInfoPro.jsp)内容如下:

<%@page contentType="text/html;charset=gbk" pageEncoding="UTF-8"%>

<!DOCTYPE html>

<html>

    <head>

        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

        <title>JSP内置对象的例子--Request</title>

    </head>

    <body>

               <%

            String stuno = request.getParameter("stuno");

            out.println("学号:"+stuno+"<br>");

            String stuname = request.getParameter("stuname");

            out.println("学生姓名:"+stuname+"<br>");

            String sex = new String(request.getParameter("sex").getBytes("8859_1"),"gbk");

            out.println("学生性别:"+sex+"<br>");

            String password=request.getParameter("password");

            out.println("密码:"+password+"<br>");

             String birthday=request.getParameter("birthday");

            out.println("出生日期:"+birthday+"<br>");

             String phone=request.getParameter("phone");

            out.println("联系方式:"+phone+"<br>");

             String email=request.getParameter("email");

            out.println("电子邮件:"+email+"<br>");

            String hobby[]=request.getParameterValues("hobby");

            String vHobby="";

            if(hobby!=null){

                int i=0;

                while(i<hobby.length){

                    vHobby+=new String(hobby[i].getBytes("8859_1"),"gbk")+" ";

                    i++;

                }

            }

            out.println("个人爱好:"+vHobby+"<br>");

            String school=request.getParameter("school");

            school=new String(school.getBytes("8859_1"),"gbk");

            out.println("学校名称:"+school+"<br>");

            String major=request.getParameter("major");

            major=new String(major.getBytes("8859_1"),"gbk");

            out.println("所学专业:"+major+"<br>");

            String self=request.getParameter("self");

            self=new String(self.getBytes("8859_1"),"gbk");

            out.println("个人简介:"+self+"<br>");

        %>

    </body>

</html>