首页 > Web开发 > 详细

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

时间:2014-03-24 20:07:42      阅读:355      评论:0      收藏:0      [点我收藏+]

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

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

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

<%@ 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>


 

<%@ 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 第一天:提交表单--获取表单中的数据值,布布扣,bubuko.com

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

原文:http://blog.csdn.net/u011131296/article/details/21971143

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!