JSP 第一天:提交表单--获取表单中的数据值

主要用到两个内置的对象:out  和 request

out:用来在小脚本里面输出显示内容

request:用来获取用户提交的信息(包括:用户的IP,表单中的内容等)

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  8. <html>  
  9.   <head>  
  10.     <base href="<%=basePath%>">  
  11.       
  12.     <title>My JSP 'index.jsp' starting page</title>  
  13.     <meta http-equiv="pragma" content="no-cache">  
  14.     <meta http-equiv="cache-control" content="no-cache">  
  15.     <meta http-equiv="expires" content="0">      
  16.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  17.     <meta http-equiv="description" content="This is my page">  
  18.     <!--  
  19.     <link rel="stylesheet" type="text/css" href="styles.css">  
  20.     -->  
  21.       
  22.   </head>  
  23.     
  24.   <body>  
  25.     <form action="getInfo.jsp" name="myform" method="post">  
  26.         <table>  
  27.             <tr>  
  28.                 <td>姓名:</td>  
  29.                 <td><input type ="text" name="txtname"  /></td>  
  30.             </tr>  
  31.             <tr>  
  32.                 <td>密码:</td>  
  33.                 <td><input type ="password" name="txtpwd"  /></td>  
  34.             </tr>  
  35.             <tr>  
  36.                 <td>男<input type="radio" name="sex" value="male" /></td>  
  37.                 <td>女<input type="radio" name="sex" value="female" /></td>  
  38.             </tr>  
  39.             <tr>  
  40.                 <td colspan="4">  
  41.                 籃球<input type="checkbox" name="hobby" value="籃球" />  
  42.                 排球<input type="checkbox" name="hobby" value="排球" />  
  43.                 足球<input type="checkbox" name="hobby" value="足球" />  
  44.                 乒乓球<input type="checkbox" name="hobby" value = "乒乓球"/>  
  45.                 </td>  
  46.             </tr>  
  47.             <tr>  
  48.                 <td colspan="2">  
  49.                     <input type="submit" name="submit" value="提交" />  
  50.                     <input type="reset" name="reset" value="重置" />  
  51.                 </td>  
  52.             </tr>  
  53.         </table>  
  54.     </form>  
  55.   </body>  
  56. </html>  


 

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  8. <html>  
  9.   <head>  
  10.     <base href="<%=basePath%>">  
  11.       
  12.     <title>My JSP 'getInfo.jsp' starting page</title>  
  13.       
  14.     <meta http-equiv="pragma" content="no-cache">  
  15.     <meta http-equiv="cache-control" content="no-cache">  
  16.     <meta http-equiv="expires" content="0">      
  17.     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
  18.     <meta http-equiv="description" content="This is my page">  
  19.     <!--  
  20.     <link rel="stylesheet" type="text/css" href="styles.css">  
  21.     -->  
  22.   
  23.   </head>  
  24.     
  25.   <body>  
  26.     <%  
  27.         request.setCharacterEncoding("UTF-8");  
  28.           
  29.         String name=request.getParameter("txtname");  
  30.         String pwd=request.getParameter("txtpwd");  
  31.         String sex=request.getParameter("sex");  
  32.         String[] hobby=request.getParameterValues("hobby");  
  33.      %>  
  34.      姓名:<%=name %><br />  
  35.      密碼:<%=pwd %><br />  
  36.      性別:<%=sex %><br />  
  37.      愛好:<%  
  38.         for(String h : hobby){  
  39.             out.print(h);  
  40.         }  
  41.       %><br />  
  42.   </body>  
  43. </html>  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值