当前位置 : 主页 > 编程语言 > java >

使用EL内置对象获取JSP内置对象的数据

来源:互联网 收集:自由互联 发布时间:2021-06-25
1.新建一个Dynamic Web Project项目,里面新建1个servlet文件和1个jsp文件,整体的框架如图所示: 2.SaveServlet.java里面的代码如下所示: package servlet;import java.io.IOException;import javax.servlet.Reques

1.新建一个Dynamic Web Project项目,里面新建1个servlet文件和1个jsp文件,整体的框架如图所示:



2.SaveServlet.java里面的代码如下所示:

package servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(name = "saveServlet", urlPatterns = { "/saveServlet" })
public class SaveServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String names[] = { "zhao", "qian", "sun", "li" };
		request.setAttribute("name", names);
		RequestDispatcher dis = request.getRequestDispatcher("show.jsp");
		dis.forward(request, response);
	}

}

3.show.jsp里面的代码如下所示:

<%@page import="java.util.Date"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>example</title>
</head>
<body>
	从servlet转发过来的request内置对象的数据如下:
	<br> ${requestScope.name[0] }
	<br> ${requestScope.name[1] }
	<br> ${requestScope.name[2] }
	<br> ${requestScope.name[3] }
	<br>
</body>
</html>

4.在浏览器里面输入http://localhost:8080/Servlet/saveServlet运行得到如下的结果:

网友评论