【JSP开发】通过ServletContext实现请求转发
2015-01-25 18:03
302 查看
package cn.edu; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; //通过ServletContext实现请求转发 public class ServletDemo8 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String data="ABCDEFG"; //把数据带给1.JSP(不能通过Context域,要通过request域) this.getServletContext().setAttribute("data", data); //获取请求分派器 RequestDispatcher rd=this.getServletContext().getRequestDispatcher("/1.jsp"); //将请求转发到指定资源 rd.forward(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } }
前台显示页面:1.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> <% 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%>"> <title>My JSP '1.jsp' starting page</title> </head> <body> <h1> <font color="red"> <% String value=(String)application.getAttribute("data"); out.write(value); %> </font> </h1> </body> </html>
相关文章推荐
- 通过ServletContext实现对servlet的请求转发
- javaweb开发中异步ajax请求之DWR框架详解(通过直接访问java类实现异步请求处理)
- [JSP] 实现请求转发
- ServletContext实现请求转发
- JSP系统开发学习之四关于用户登录界面的补充——通过request页面传值&wel.jsp的MVC的实现
- servletContext实现请求转发
- jsp 中重定向与请求转发的区别
- 用JSTL实现JSP应用程序快速开发
- 项目开发技巧(四):使用JspSmartupload实现文件上传下载(三):jspsmartupload samples
- 通过HttpWebRequest 发送 POST 请求实现自动登陆
- 通过编程实现log4j配置动态生效。很简单通过jsp实现的。
- 通过HttpWebRequest 发送 POST 请求实现自动登陆
- 如何实现通过右键查看源代码来查看jsp页面的名字
- 提升Web Services性能原来不是通过传说中的压缩Soap请求和响应消息来实现的
- 通过HttpWebRequest 发送 POST 请求实现自动登陆
- Tomcat通过JNDI方式连接SqlServer数据库 开发JSP详解
- C#实现通过HttpWebRequest发送POST请求实现网站自动登陆
- 项目开发技巧(四):使用JspSmartupload实现文件上传下载(二):jspSmartUpload上传下载全攻略(ZZ)
- 通过二次开发在SAP R/3中实现库存日清功能
- 用JSTL实现JSP应用程序快速开发