如何在使用javax.servlet.RequestDispatcher.include()时将参数传递给.jsp文件? [英] how to pass arguments to .jsp file when using javax.servlet.RequestDispatcher.include()?

查看:274
本文介绍了如何在使用javax.servlet.RequestDispatcher.include()时将参数传递给.jsp文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过将传入的HttpRequest路由到以Java编写的Controller Servlet中,然后当Servlet完成时,使用.jsp文件渲染回View,我使用建模Web应用程序的Model / View / Controller样式。 (这是Rails风格。)



这样做需要在Controller Servlet的末尾有这样一行:

  getServletContext()。getRequestDispatcher(/ Bar.jsp)。include(req,res); 

主要问题是我想将参数传递给Bar.jsp,就像它是一个函数一样我在打电话。如果这是不可能的,我最终会在Bar.jsp的顶部放置大量Java,以找出Bar.jsp需要呈现的所有内容,这相当难看。



其他web框架提供了一种方法来实现这一点,所以似乎必须有一种方法可以用Servlets来实现。特别是我在Java Google App Engine中工作。

解决方案

您可以使用

  request.setAttribute(attributeName,attributeValue); 

和其他jsp文件中可以使用的方法 getAttribute()像这样

  request.getAttributeNames(); 
request.getAttribute(attributeName);


I'm using the Model/View/Controller style of building web apps by routing an incoming HttpRequest to a Controller Servlet written in Java and then when the Servlet is finished, having it render back a View using a .jsp file. (This is very much the Rails style.)

Doing this requires a line like this at the end of the Controller Servlet:

getServletContext().getRequestDispatcher("/Bar.jsp").include(req, res);

The main problem is I want to pass arguments to Bar.jsp just as if it were a function that I am calling. If this is not possible, I end up putting lots of Java at the top of Bar.jsp to find out everything that Bar.jsp needs to render itself, which is rather ugly.

Other web frameworks provide a way to do this, so it seems that there must be a way to do it with Servlets. In particular I am working in Java Google App Engine.

解决方案

you can use

request.setAttribute("attributeName",attributeValue);

and in other jsp file you can using methodgetAttribute() like this

request.getAttributeNames();
request.getAttribute("attributeName");

这篇关于如何在使用javax.servlet.RequestDispatcher.include()时将参数传递给.jsp文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆