jsp原理笔记(四)
来源:互联网 发布:网络运行情况通报 编辑:程序博客网 时间:2024/05/22 15:11
使用request.getParameterValues()方法:
hobby.jsp:
<%@ page language="java" contentType="text/html;charset=GBK" %><html><head><title>选择自己的爱好 </title></head><body><form name="myform" method="post" action="result.jsp">选择自己的业余爱好:<input type="checkbox" name="hobby" value="唱歌">唱歌<input type="checkbox" name="hobby" value="跳舞">跳舞<input type="checkbox" name="hobby" value="足球">足球<input type="checkbox" name="hobby" value="篮球">篮球<input type="submit" value="提交"></form></body></html>
result.jsp:
<%@ page language="java" contentType="text/html;charset=GBK" %><html><head><title>个人爱好</title></head><body><%request.setCharacterEncoding("GBK");String strhobby="";String[] hobbies=request.getParameterValues("hobby");if(hobbies!=null){for(int i=0;i<hobbies.length;i++){strhobby+=hobbies[i]+" ";}}%>您选择的爱好:<b><%=strhobby %></b></body></html>运行截图:
使用request.getParameter()方法:
猜数:guess.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%><%int guessNum=0;int randomNum=0;int count=0;boolean success=false;boolean compare=false;try{guessNum=Integer.parseInt(request.getParameter("guessNum"));randomNum=Integer.parseInt(request.getParameter("randomNum"));count=Integer.parseInt(request.getParameter("count"));if(guessNum==randomNum){count++;success=true;}else{count++;success=false;if(guessNum>randomNum){compare=true;}else{compare=false;}}if(success){randomNum=(int) (Math.random()*100);}}catch(Exception e){guessNum=0;randomNum=0;count=0;success=false;randomNum=(int) (Math.random()*100);}%><html><head><title>猜数</title><script type="text/javascript">function isNum(){var num=document.getElementsByName("guessNum");if(isNaN(num)){alert("这不是一个数字,请输入数字类型");return false;}return true;}</script></head><body><h1>猜一猜</h1><%if(!success){ %><form action="guess.jsp" method="post" onsubmit="return isNum()"><input type="hidden" name="randomNum" value="<%=randomNum %>" /><input type="hidden" name="count" value="<%=count %>" />输入数字:<input type="text" name="guessNum" checked="checked"/><input type="submit" value="猜一下"><%if(count!=0){ %><h3><%=compare?"猜大了":"猜小了" %></h3><%} %><h2>你猜了<%=count %>次</h2></form><%}else{ %>恭喜你,猜对了,你猜了<%=count %>次 <a href="guess.jsp">再玩一次</a> <%} %></body></head></html>
全局计数器:counter.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%><html><head><title>全局计数器</title></head><body><%int count=0;if(application.getAttribute("count")!=null)count=(Integer) application.getAttribute("count");%>一共有<%=++count %>次访问该页面<%application.setAttribute("count", count) ;%></body></html>
显示日期Date.jsp:
<%@ page language="java" import="java.util.*,java.text.*" pageEncoding="gbk"%><html><head><title>实现当前时间</title></head><body><%Date dt=new Date();SimpleDateFormat smf=new SimpleDateFormat("yyyy年MM月dd日");out.println(smf.format(dt));%></body></html>
自动生成表格table.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%><html><head><title>自动生成表格</title><style type="text/css"><!--.STYLE2{font-size:24px} --></style></head><body><table width="279" border="1"><caption class="STYLE2">自动生成的表格</caption><tr><th width="65" bgcolor="#99cc99" scope="col">第1列</th><th width="65" bgcolor="#99cc99" scope="col">第2列</th><th width="65" bgcolor="#99cc99" scope="col">第3列</th><th width="65" bgcolor="#99cc99" scope="col">第4列</th><th width="101" bgcolor="#99cc99" scope="col">第5列</th></tr><%for(int i=0;i<12;i+=5){ %><tr><td align="center"><%=i+1 %></td><td align="center"><%=i+2 %></td><td align="center"><%=i+3 %></td><td align="center"><%=i+4 %></td><td align="center"><%=i+5 %></td></tr><%} %></table></body></html>
- jsp原理笔记(四)
- JSP原理笔记(一)
- JSP原理笔记(二)
- JSP原理笔记(三)
- jsp基础学习(四)----jsp引擎工作原理
- JSP学习笔记(四):JSTL标签
- 尚学堂马士兵servlet/JSP笔记(四、JSP)
- 尚学堂马士兵servlet/JSP笔记(四、JSP)
- 计算机网络原理笔记(四、网络层)
- Jsp/Servlet读书笔记(四) -- HttpSession会话管理原理
- 开启 J2EE(四)—JSP及运行原理
- JSP/Servlet 学习笔记(四)
- JSP学习笔记之四
- Servlet笔记(四)Session JSP
- JSP 笔记(四 : EL 表达式 )
- SERVLETJSP学习(四)—— JSP基本语法 、JSP运行原理
- JSP&&SERVLET学习笔记(四):关于HttpServletRequest
- JSP学习笔记(四):点击按钮跳转到action
- Android UI 使用更快更高效
- 二分查找法
- poj 1699(2013@USC D题)
- “Windows XP下vs2010中配置OpenCV2.4.3 ”的补充
- tiny6410开机自启动QT4程序,并设置程序为全屏显示
- jsp原理笔记(四)
- jsp标签自动提示,提高编程效率,自动提示
- 数据库内容介绍
- 概率统计方面的底层需要实现的相关函数
- VC单文档分割窗口4--锁定分割条
- HDU1151有向图的最小路径覆盖
- JSON 简介
- 好公司职位要求
- SQL语句大全