使用EL表达式获得作用域属性

scopedvarssetting.java

package chap08;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;



public class scopedvarssetting extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public scopedvarssetting() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//设置request范围的attribute
		request.setAttribute("firstVar", "Tom");
		//设置session范围的attribute
		HttpSession session = request.getSession();  //获得session对象
		session.setAttribute("secondVar","Jack");
		//设置application范围的attribute
		ServletContext application = this.getServletContext();
		application.setAttribute("thirdVar","Jerry");
		
		//跳转到JSP页面
		RequestDispatcher dis = request.getRequestDispatcher("/getscopedvars.jsp");
		dis.forward(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request,response);
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}

getscopedvars.jsp

<%@ page contentType="text/html; charset=utf-8" language="java" import="java.util.*" errorPage="" %>
<%
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%>">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>My JSP 'MyJsp.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>
	<%
		pageContext.setAttribute("fourthVar", "Marry");
		String s1 = "";
		String s2 = "";
		String s3 = "";
		String s4 = "";
		
		s1 =request.getAttribute("firstVar").toString();
		s2 = session.getAttribute("secondVar").toString();
		s3 = application.getAttribute("thirdVar").toString();
		s4 = pageContext.getAttribute("fourthVar").toString();
	%>
	<h1>使用EL表达式获得作用域属性</h1>
	<table border="1" width="80%">

		<tr>
			<td width="50%">使用getAttribute方法</td>
			<td width="50%">使用EL表达式</td>
		</tr>

		<tr>
			<td width="50%"><%=s1%></td>
			<td width="50%">${firstVar }</td>
		</tr>
		<tr>
			<td width="50%"><%=s2%></td>
			<td width="50%">${secondVar }</td>
		</tr>
		<tr>
			<td width="50%"><%=s3%></td>
			<td width="50%">${thirdVar }</td>
		</tr>
		<tr>
			<td width="50%"><%=s4%></td>
			<td width="50%">${fourthVar }</td>
		</tr>

	</table>
</body>
</html>

发表评论

电子邮件地址不会被公开。 必填项已用*标注