how to pass arguments to .jsp file when using java

2019-09-03 05:15发布

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.

3条回答
成全新的幸福
2楼-- · 2019-09-03 05:57

you can use

request.setAttribute("attributeName",attributeValue);

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

request.getAttributeNames();
request.getAttribute("attributeName");
查看更多
我想做一个坏孩纸
3楼-- · 2019-09-03 06:06

This page says it well, also addressing the difference between parameters and attributes: http://www.xyzws.com/Servletfaq/what-is-the-difference-between-the-request-attribute-and-request-parameter/1

Request attributes (more correctly called "request-scoped variables") are objects of any type that are explicitly placed on the request object via a call to the setAttribute() method. They are retrieved in Java code via the getAttribute() method and in JSP pages with Expression Language references. Always use request.getAttribute() to get an object added to the request scope on the serverside i.e. using request.setAttribute().

Attributes are objects, and can be placed in the request, session, or context objects. Because they can be any object, not just a String, they are much more flexible. You can also set attributes programaticly and retrieve them later. This is very useful in the MVC pattern. For example, you want to take values from database in one jsp/servlet and display them in another jsp. Now you have resultset filled with data ready in servlet then you use setAttribute method and send this resultset to another jsp where it can be extracted by using getAttribute method.

Once a servlet gets a request, it can add additional attributes, then forward the request off to another servlet for processing. Attributes allow servlets to communicate with one another.

查看更多
一纸荒年 Trace。
4楼-- · 2019-09-03 06:16

As it is said in one of the comments, you can't pass arguments to a different JSP (within the same request) as if it was a function.

The best thing you can do is to create one (or several) java beans that encapsulate the parameters with its attributes. Then, add those beans as request attributes before invoking the JSP. In the JSP you can reference the values held by those beans using EL expressions like ${myBean.myParameter} without the need of additional java code in the JSP. This how common MVC frameworks for java do it.

Notice that if just need to access the parameters that triggered that request/response processing, you can access them with expressions like ${param.myParam}.

EDIT

Sorry for not adding any link before. An EL expression is... an expression contained between the symbols ${ and } (or #{ and } - but take case as those are different kinds of EL expressions). El expressions allow the JSP developer to access data stored in the request, session or application context (other frameworks may add more contexts to that basic set) without the need of Java code. When writing JSP we must avoid using <% ... %> and just use code that is intended to render output to the view rather than heavy data processing. Follow the link about best practices to obtain more background about it.

There are, mainly, to big groups of EL expressions and I can't explain everything in a SO post but I recommend you to follow this link.

Adding variables, or objects (java beans) to the request or any other scope is fairly simple. To add a bean to the request you do: request.setAttribute("myBeanName", myBean);. Something similar with the other contexts. The Java EE tutorial will explain better that than me and after it you should be able to understand the JavaEE characteristics.

查看更多
登录 后发表回答