欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 前端技术 > javascript >内容正文

javascript

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

发布时间:2025/6/15 javascript 48 豆豆
生活随笔 收集整理的这篇文章主要介绍了 JSP 第一天:提交表单--获取表单中的数据值 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

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

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

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

[java] view plaincopyprint?
  • <%@ page language="java" import="java.util.*" pageEncoding="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 'index.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>  
  •     <form action="getInfo.jsp" name="myform" method="post">  
  •         <table>  
  •             <tr>  
  •                 <td>姓名:</td>  
  •                 <td><input type ="text" name="txtname"  /></td>  
  •             </tr>  
  •             <tr>  
  •                 <td>密码:</td>  
  •                 <td><input type ="password" name="txtpwd"  /></td>  
  •             </tr>  
  •             <tr>  
  •                 <td>男<input type="radio" name="sex" value="male" /></td>  
  •                 <td>女<input type="radio" name="sex" value="female" /></td>  
  •             </tr>  
  •             <tr>  
  •                 <td colspan="4">  
  •                 籃球<input type="checkbox" name="hobby" value="籃球" />  
  •                 排球<input type="checkbox" name="hobby" value="排球" />  
  •                 足球<input type="checkbox" name="hobby" value="足球" />  
  •                 乒乓球<input type="checkbox" name="hobby" value = "乒乓球"/>  
  •                 </td>  
  •             </tr>  
  •             <tr>  
  •                 <td colspan="2">  
  •                     <input type="submit" name="submit" value="提交" />  
  •                     <input type="reset" name="reset" value="重置" />  
  •                 </td>  
  •             </tr>  
  •         </table>  
  •     </form>  
  •   </body>  
  • </html>  

  •  

    [java] view plaincopyprint?
  • <%@ page language="java" import="java.util.*" pageEncoding="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 'getInfo.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>  
  •     <%  
  •         request.setCharacterEncoding("UTF-8");  
  •           
  •         String name=request.getParameter("txtname");  
  •         String pwd=request.getParameter("txtpwd");  
  •         String sex=request.getParameter("sex");  
  •         String[] hobby=request.getParameterValues("hobby");  
  •      %>  
  •      姓名:<%=name %><br />  
  •      密碼:<%=pwd %><br />  
  •      性別:<%=sex %><br />  
  •      愛好:<%  
  •         for(String h : hobby){  
  •             out.print(h);  
  •         }  
  •       %><br />  
  •   </body>  
  • </html>  
  • 总结

    以上是生活随笔为你收集整理的JSP 第一天:提交表单--获取表单中的数据值的全部内容,希望文章能够帮你解决所遇到的问题。

    如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。