JSP中的九大隐式对象

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




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值