Passing a parameter without using HTML forms in JSP

I did some research, and most of the examples I found use forms (obviously for user input) to collect data, which is then passed to another JSP page through a request object.

My question is: is it possible to pass a parameter between JSP pages if the parameter has not been set in the HTML <form> tags?

+6
java jsp
source share
3 answers

There is no way for the JSP page to distinguish a manual GET URL, for example:

<a href="/foo.jsp?bar=baz">Go to the next JSP page</a> , compared to something like:

 <form method="get" action="/foo.jsp"> <input name="bar" value="baz"> </form> 

In any case, it can be accessed through getParameter or getParameterValues

Is that what you are asking?

+5
source share

There are several ways to transfer information from one JSP page to another.

1. Hidden values.

Just write the data in the input field in the form with the type "hidden", for example.

 <input type="hidden" name="mydata" value="<%=thedata%>"> 

Data recorded in this way will be sent back when the appropriate form is submitted. This can be a useful way to “transfer” information as the user fills out a series of dialogs, since all states are user-defined, and the back and forward buttons work as expected.

2. Record URL.

Attach the parameters to the URLs in the links on the page, for example

 <a href="another.jsp?mydata=<%=thedata>>Go!</a> 

It also maintains state with the client, eliminating the need to submit a form element.

3. Cookies.

Must speak for itself. The state is still user-defined, but it is now processing a cookie. More fragile in some way, as some people disable cookies. In addition, the back and forward buttons can do unexpected things if you are not careful.

4. Server side (session)

Finally, you can store data in a session variable on one JSP and retrieve it on the next, for example.

 session.setAttribute(String key, Object value) 

and

 session.getAttribute(String key) 

Here, the state is maintained on the server side, which has some advantages (the user can browse and return without losing space, but tends to make the back and forth buttons in the browser a little unreliable if you are not careful. Is available for all pages.

However, it has the advantage that information is never sent to the client and, therefore, is more secure and more protected from unauthorized access.

+17
source share

Typically, you pass data between servlet / JSP or JSP pages in cloud attributes (in a request, session, or application). For example. request.setAttribute("key", data) can set the attribute "key" in one JSP, and request.getAttribute("key") receives this data in another JSP (if several JSPs process the same request).

It is possible to create fake parameters by transferring your request and overriding getParameter and a similar method if that is what you really need.

Update: my answer is about transferring data between several parties that all process the same request. This may or may not be what you want, as your question is not clear.

+3
source share

All Articles