JSP中的九大隐式对象
2014-10-23 15:59
393 查看
<%@ page import="java.util.*" pageEncoding="UTF-8"%> <html> <head> <title></title> </head> <% String id=session.getId(); String method=request.getMethod(); response.setCharacterEncoding("UTF-8"); //pageContext也具备Attribute,可以通过set/get来存放获取数据 //和request,session的Attribute的差别在于作用域不同 //request中setAttribute的数据只能在转发或包含的页面中通过getAttribute获取 //session中setAttribute的数据可以被当前用户在当前网站的任何页面中通过getAttribute获取 //pageContext中setAttribute的数据只能在当前页面中通过getAttribute获取 pageContext.setAttribute("msg", ""); pageContext.getAttribute("msg"); //application也具备Attribute,可以通过set/get来存放获取数据 //和pageContext,request,session的Attribute的差别在于作用域不同 //request中setAttribute的数据只能在转发或包含的页面中通过getAttribute获取 //session中setAttribute的数据可以被当前用户在当前网站的任何页面中通过getAttribute获取 //pageContext中setAttribute的数据只能在当前页面中通过getAttribute获取 //application中setAttribute的数据可以被任何用户在当前网站的任何页面中通过getAttribute获取,适合做投票 application.setAttribute("msg", ""); application.getAttribute("msg"); //config是配置对象,包含了当前JSP/Servlet的相关信息,其中就包括了当前站点在服务器的物理路径(以后进行文件上传和下载时用) String path=config.getServletContext().getRealPath("/"); //page是当前对象,是的,你可以理解为是JSP版的this,如果有需要将当前页面作为参数传递给某个方法的话。那么可以使用 //但是事实上大家忽略它就可以了,这个玩意是JSP特有的 //page //所谓的隐式对象事实上是Java代码中已经帮我们声明好的变量 //一共有九个,俗称九大隐式对象 //pageContext,request,session,application是作用域对象,它们的共同点是都能存储数据 //out,config是系统对象,out是用于输出, config是用于操作当前网站或页面本身的配置属性 //request和response是请求和响应对象,也是应用中使用最多的对象 //page是个特别对象,表示当前页面本身,用于充当参数使用 //最后还有个exception对象 %> <body> <%out.println(id); %><br> <%=method %> <h1><%=path %></h1><br> </body> </html>
页面的显示如下
这里的id,在当前的浏览器中刷新后(30分钟内)是不变的,但是在其他浏览器中是不同的值
下面做一个投票的界面
vote.jsp
<%@ page import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title></title>
</head>
<body>
<h1>最爱吃什么,请投票 </h1>
<form action="do<span style="font-family:KaiTi_GB2312;">vote</span>.jsp" method="post">
<input type="radio" value="食堂" name="eat"> 食堂
<input type="radio" value="自助餐" name="eat">自助餐
<input type="radio" value="盖码饭" name="eat">盖码饭
<input type="radio" value="粉面" name="eat">粉面
<input type="radio" value="粥饼" name="eat">粥饼
<input type="radio" value="包点" name="eat">包点
<input type="radio" value="KFC" name="eat">KFC
<input type="radio" value="方便面" name="eat">方便面
<input type="submit" value="投票">
</form>
</body>
</html>
dovote.jsp
<%@ page import="java.util.*" pageEncoding="UTF-8"%>
<%
request.setCharacterEncoding("UTF-8");
String msg=request.getParameter("eat");
//统计
HashMap map=(HashMap)application.getAttribute("list");
if(map==null){
map=new HashMap();
application.setAttribute("list",map);
}
if(map.containsKey(msg)){
map.put(msg, ((Integer)map.get(msg))+1);
}else{
map.put(msg, 1);
}
Iterator it_set=map.entrySet().iterator();
%>
<html>
<head>
<title></title>
</head>
<body>
<% while(it_set.hasNext()){
Map.Entry entry=(Map.Entry)it_set.next();
%>
<h1><%=entry.getKey()%>获得了<%=entry.getValue()%>票</h1>
<%} %>
</body>
</html>
相关文章推荐
- JSP运行原理和九大隐式对象01
- Jsp九大隐式对象
- JSP九大隐式对象
- JSP运行原理和九大隐式对象说明
- JSP运行原理和九大隐式对象
- JSP运行原理和九大隐式对象
- JSP运行原理和九大隐式对象说明
- jsp的九大隐式对象及其作用域
- 心得4--JSP标签和其九大隐式对象
- Jsp九大隐式对象和JSP指令
- JSP运行原理和九大隐式对象
- jsp九大隐式对象及其作用
- jsp九大隐式对象
- javaweb-05-jsp九大隐式对象简介
- JSP技术 指令 乱码问题 运行原理和九大隐式对象 web开发的四个域对象 javabean
- JSP运行原理和九大隐式对象及下载文件、访问次数的代码
- jsp中的九大隐式对象
- JSP--JSP语法--指令--include(动态包含/静态包含)--九大隐式对象--四大域对象--JSP内置标签--JavaBean的动作元素--MVC三层架构
- JSP九大隐式对象--pageContext
- 掌握 JSP 九大隐式对象--(Servlet 中使用的对象在 JSP 中的应用)