JSP获取用户输入

 
       request对象的类型为HttpServletRequest。在JSP翻译成的Java类中,有这样的函数:
 public void _jspService(HttpServletRequest request, HttpServletResponse response)
        throws java.io.IOException, ServletException
       当客户请求到达服务器时,该函数将被调用。该函数负责生成并输出客户请求的html代码。
HttpServletRequest是一个接口,扩展自ServletRequest接口。该接口提供了一系列方法,主要用于http服务器了解客户端的请求。下面的例子演示了如何获取位于表单中的编辑框中的用户输入数据。
第一个页面让用户输入:
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@page import= "java.io.*"%>
<%@page import= "huangxiaoxing.*"%>
 
<%--
The taglib directive below imports the JSTL library. If you uncomment it,
you must also add the JSTL library to the project. The Add Library... action
on Libraries node in Projects view can be used to add the JSTL 1.1 library.
--%>
 
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
 
 
<!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=UTF-8">
        <title>Test</title>
       
    </head>
   
    <jsp:useBean id = "person" scope="page" class="huangxiaoxing.Person" />
   
    <jsp:useBean id ="factory" scope="page" class="huangxiaoxing.PersonHelp" />
   
 
    <body>
 
    <%
        //read info
        factory.loadPerson(person);    
    %>
 
    <h1 align="center"> Save </h1>
    <%--
    This example uses JSTL, uncomment the taglib directive above.
    To test, display the page like this: index.jsp?sayHello=true&name=Murphy
    --%>
    <%--
    <c:if test="${param.sayHello}">
        <!-- Let's welcome the user ${param.name} -->
        Hello ${param.name}!
    </c:if>
    --%>
        <form name="test" method="get" action="SaveBean.jsp" >
            <table border="0" width = "300px" align="center">
                <thead>
                    <tr>
                        <th></th>
                    </tr>
                </thead>
               
                <tbody>
                    <tr>
                        <td align="right">
                  saveName:
                           
                        </td>
                        <td>
                            <input type="text" name="name" value="${person.name}" />
                        </td>
                    </tr>
                   
                    <tr>
                        <td align="right">
                 saveAddress:
                        </td>
                        <td>
                            <input type="text" name="address" value="${person.address}" />
                        </td>   
                    </tr>
                   
                    <tr>
                        <td align="right">
                 savePhone:
                           
                        </td>
                        <td>
                            <input type="text" name="phone" value="${person.phone}" />
                        </td>                       
                       
                    </tr>
                   
                    <tr>
                        <td align="right">
                          
                           
                           
                        </td>
                        <td >
                            <input type="submit" value=" save " name="save" />
                        </td>
                    
                    </tr>
                </tbody>
            </table>
 
        </form>
    
    </body>
</html>
 
请注意红色部分 <form name="test" method="get" action="SaveBean.jsp" >代表了当前index.jsp页面的这个表单提交按钮被点击的时候,将向服务器请求SaveBean.jsp页面。然后编写SaveBean.jsp脚本的程序员就可以通过调用request. getParameter方法获取通过index.jsp的表单提交来的GET命令里面附带的query string中的参数值。如下:
<%
        //save info
        String name = request.getParameter("name");
        String address = request.getParameter("address");
        String phone = request.getParameter("phone");
              //......
    %>
这种方式十分容易理解。
通过EL方式
       这一次我们使用EL方式获取用户的请求数据,在SaveBean.jsp文件里,我们加入这样的代码:
        UserInput:<br>
        name:<c:out value="${param.name}" /> <br>
        address:<c:out value="${param.address}" /> <br>
        phone<c:out value="${param.phone}" /> <br>
       但是这种方式不能用于<%...%>块中的Java脚本代码编写。
 
通过与JavaBean属性映射
      
       这种方式,主要是在jsp:useBean行为元素中使用了jsp:setProperty行为元素,并且注意property=”*”代表html控件的名称和JavaBean的属性名称自动匹配,注意一定要完全相同擦才行。这样将自动用html控件中的值设置JavaBean的属性。
 
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值