JSP 第一天:提交表单--获取表单中的数据值
主要用到两个内置的对象: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>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。