aspose file tools*
The moose likes JSP and the fly likes How to set a request parameter in a JSP and pass it in a c:param tag? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "How to set a request parameter in a JSP and pass it in a c:param tag? " Watch "How to set a request parameter in a JSP and pass it in a c:param tag? " New topic
Author

How to set a request parameter in a JSP and pass it in a c:param tag?

Jeff Wachhorst
Ranch Hand

Joined: Mar 09, 2004
Posts: 41
Here is the relevant swatch of my JSP:



<%@ page language="java" import="java.util.*" %>
<%@ taglib prefix="c" uri="/WEB-INF/c.tld" %>
.
.
.

<tr>
<td>Also show me the best flights departing within: <SELECT NAME="frTimeFrame">
<OPTION>1 <OPTION>2 <OPTION>3 <OPTION>4 <OPTION>5 <OPTION>6 <OPTION>7 <OPTION>8
<OPTION>9 <OPTION>10 <OPTION>11 <OPTION>12
</SELECT> hour(s) of this flight
</td>
</tr>

<tr>
<td>
<INPUT TYPE="checkbox" NAME="lessThan" VALUE="yes"> Don't show me any flights whose travel time is longer than this one
</td>
</tr>

<tr>
<td>
<c:url value="/FltSvltMnB/result2.scrn" var="resultURL">
<c aram name="flightID" value="${depListItem.flightID}"/>
<c aram name="frTimeFrame"/>
<c aram name="lessThan"/>
</c:url>
<a href='<c ut value="${resultURL}"/>'>
<c ut value="Select this flight"/>
</a>
</td>
</tr>

When this screen comes up, I select a value for "frTimeFrame", click on the checkbox for "lessThan" and then click on "Select this flight".
The program that handles this page is successfully invoked. In that program, I have the following:

if(request.getParameter("lessThan") != null)
System.out.println("lessThan: " + request.getParameter("lessThan"));

if(request.getParameter("frTimeFrame") != null)
System.out.println("frTimeFrame: " + request.getParameter("frTimeFrame"));

The output is this:
lessThan:
frTimeFrame:

OK, the parameters aren't null but that's not much help is it? I then tried this in my JSP:

<jsp:useBean
id="fltHelp"
class="fltPkg1.FltHelper"
scope="session"
/>
.
.
.

<tr>
<c:set var="frTimeFrame">
<td>Also show me the best flights departing within: <SELECT NAME="frTimeFrame">
<OPTION>1 <OPTION>2 <OPTION>3 <OPTION>4 <OPTION>5 <OPTION>6 <OPTION>7 <OPTION>8
<OPTION>9 <OPTION>10 <OPTION>11 <OPTION>12
</SELECT> hour(s) of this flight
</td>
</c:set>
</tr>
<c:set value="${frTimeFrame}" target="${fltHelp}" property="frTimeFrame"/>

I got a java.lang.reflect.InvocationTargetException. I think I have to be missing something simple. Would anyone know what that is?


THANK YOU so much in advance!
[ October 11, 2005: Message edited by: Bear Bibeault ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61661
    
  67

You might want to edit your post and fix your code. With all those &lt and &gt mistakes, the code is impossible to read.

You should be able to post code with angle brackets without resorting to entity tricks -- it's phrases like &111;nclick that drive UBB nuts -- but if you are going to use HTML entities, don't forget the closing semi-colon.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Jeff Wachhorst
Ranch Hand

Joined: Mar 09, 2004
Posts: 41
My post wouldn't post with the less than and greater than signs and told me I'd have to remove them so I changed them to "<" and ">" and it posted. On my computer, they translate to less than and greater than symbols and the code looks as it should. Do you know how I could post with them AS less than and greater than symbols so that my code would be more easily readable on anyone's computer out there?

Thanks-
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61661
    
  67

I'm seeing all garbled code. You should also enclose any posted code in UBB code tags which will preserve the formatting. The CODE button below the text entry area will insert a pair for you.

An invocation target exception is usually a wrapper for another cause. Is there another root exception shown in your stack trace?
[ October 10, 2005: Message edited by: Bear Bibeault ]
Jeff Wachhorst
Ranch Hand

Joined: Mar 09, 2004
Posts: 41
Just a test- will this line post correctly?

<c:set value="${param.frTimeFrame}" target="${fltHelp}" property="frTimeFrame"/>
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61661
    
  67

That posted fine, just be sure to put the code IN BETWEEN the code tags.

Example:

Jeff Wachhorst
Ranch Hand

Joined: Mar 09, 2004
Posts: 41
Thank you! The 'CODE' button did the trick. Here's my original post again:

How to set a request parameter in a JSP and pass it in a <c aram../> tag?

Here is the relevant swatch of my JSP:

<%@ page language="java" import="java.util.*" %>
<%@ taglib prefix="c" uri="/WEB-INF/c.tld" %>
.
.
.

<tr>
<td>Also show me the best flights departing within: <SELECT NAME="frTimeFrame">
<OPTION>1 <OPTION>2 <OPTION>3 <OPTION>4 <OPTION>5 <OPTION>6 <OPTION>7 <OPTION>8
<OPTION>9 <OPTION>10 <OPTION>11 <OPTION>12
</SELECT> hour(s) of this flight
</td>
</tr>

<tr>
<td>
<INPUT TYPE="checkbox" NAME="lessThan" VALUE="yes"> Don't show me any flights whose travel time is longer than this one
</td>
</tr>

<tr>
<td>
<c:url value="/FltSvltMnB/result2.scrn" var="resultURL">
<c aram name="flightID" value="${depListItem.flightID}"/>
<c aram name="frTimeFrame"/>
<c aram name="lessThan"/>
</c:url>
<a href='<c ut value="${resultURL}"/>'>
<c ut value="Select this flight"/>
</a>
</td>
</tr>

When this screen comes up, I select a value for "frTimeFrame", click on the checkbox for "lessThan" and then click on "Select this flight".
The program that handles this page is successfully invoked. In that program, I have the following:

if(request.getParameter("lessThan") != null)
System.out.println("lessThan: " + request.getParameter("lessThan"));

if(request.getParameter("frTimeFrame") != null)
System.out.println("frTimeFrame: " + request.getParameter("frTimeFrame"));

The output is this:
lessThan:
frTimeFrame:

OK, the parameters aren't null but that's not much help is it? I then tried this in my JSP:

<jsp:useBean
id="fltHelp"
class="fltPkg1.FltHelper"
scope="session"
/>
.
.
.

<tr>
<c:set var="frTimeFrame">
<td>Also show me the best flights departing within: <SELECT NAME="frTimeFrame">
<OPTION>1 <OPTION>2 <OPTION>3 <OPTION>4 <OPTION>5 <OPTION>6 <OPTION>7 <OPTION>8
<OPTION>9 <OPTION>10 <OPTION>11 <OPTION>12
</SELECT> hour(s) of this flight
</td>
</c:set>
</tr>
<c:set value="${frTimeFrame}" target="${fltHelp}" property="frTimeFrame"/>

I got a java.lang.reflect.InvocationTargetException. I think I have to be missing something simple. Would anyone know what that is?
-----
"An invocation target exception is usually a wrapper for another cause. Is there another root exception shown in your stack trace?"
Yes. In my post's swatch of code I did not include the following (which was in my actual JSP code):

<c:set value="${param.frTimeFrame}" target="${fltHelp}" property="frTimeFrame"/>

I didn't think it was relevant to the question. However, I do believe it is relevant to the invocation target exception. This line of code attempts to set a property named frTimeFrame in fltHelp (an instance of my FltHelper class) via method setFrTimeFrame(String frTimeFrame). It fails because frTimeFrame is null when it arrives as a parameter to this method. I think that's what the exception was about.
Jeff Wachhorst
Ranch Hand

Joined: Mar 09, 2004
Posts: 41
One more time:

How to set a request parameter in a JSP and pass it in a <c:param../> tag? (Or perhaps more accurately, a <c:url../> tag)

Here is the relevant swatch of my JSP:

<%@ page language="java" import="java.util.*" %>
<%@ taglib prefix="c" uri="/WEB-INF/c.tld" %>
.
.
.

<tr>
<td>Also show me the best flights departing within: <SELECT NAME="frTimeFrame">
<OPTION>1 <OPTION>2 <OPTION>3 <OPTION>4 <OPTION>5 <OPTION>6 <OPTION>7 <OPTION>8
<OPTION>9 <OPTION>10 <OPTION>11 <OPTION>12
</SELECT> hour(s) of this flight
</td>
</tr>

<tr>
<td>
<INPUT TYPE="checkbox" NAME="lessThan" VALUE="yes"> Don't show me any flights whose travel time is longer than this one
</td>
</tr>

<tr>
<td>
<c:url value="/FltSvltMnB/result2.scrn" var="resultURL">
<c:param name="flightID" value="${depListItem.flightID}"/>
<c:param name="frTimeFrame"/>
<c:param name="lessThan"/>
</c:url>
<a href='<cut value="${resultURL}"/>'>
<cut value="Select this flight"/>
</a>
</td>
</tr>

When this screen comes up, I select a value for "frTimeFrame", click on the checkbox for "lessThan" and then click on "Select this flight".
The program that handles this page is successfully invoked. In that program, I have the following:

if(request.getParameter("lessThan") != null)
System.out.println("lessThan: " + request.getParameter("lessThan"));

if(request.getParameter("frTimeFrame") != null)
System.out.println("frTimeFrame: " + request.getParameter("frTimeFrame"));

The output is this:
lessThan:
frTimeFrame:

OK, the parameters aren't null but that's not much help is it? I then tried this in my JSP:

<jsp:useBean
id="fltHelp"
class="fltPkg1.FltHelper"
scope="session"
/>
.
.
.

<tr>
<c:set var="frTimeFrame">
<td>Also show me the best flights departing within: <SELECT NAME="frTimeFrame">
<OPTION>1 <OPTION>2 <OPTION>3 <OPTION>4 <OPTION>5 <OPTION>6 <OPTION>7 <OPTION>8
<OPTION>9 <OPTION>10 <OPTION>11 <OPTION>12
</SELECT> hour(s) of this flight
</td>
</c:set>
</tr>
<c:set value="${frTimeFrame}" target="${fltHelp}" property="frTimeFrame"/>

I got a java.lang.reflect.InvocationTargetException. I think I have to be missing something simple. Would anyone know what that is
------
"An invocation target exception is usually a wrapper for another cause. Is there another root exception shown in your stack trace?"
Yes. In my post's swatch of code I did not include the following which was in my actual code:

<c:set value="${param.frTimeFrame}" target="${fltHelp}" property="frTimeFrame"/>

I didn't think it was relevant to the question. However, I do believe it is relevant to the invocation target exception. This line of code attempts to set a property named frTimeFrame in fltHelp (an instance of my FltHelper class) via method setFrTimeFrame(String frTimeFrame). It fails because frTimeFrame is null when it arrives as a parameter to this method. I think that's what the exception was about.

Thanks again for any help!



Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61661
    
  67

OK, one thing at a time,

OK, the parameters aren't null ...


The won't be null, but they will be blank:



Why would you expect them to have any value when you have not specified any?

Perhaps you are thinking that the similarly named values in the form are going to be magically picked up? That's not the way things work. All the JSP actions, including JSTL tags, happen on the server long before the form gets presented in the browser.

If you want the form values to be submitted, you need to submit the form just like any other HTML page.
[ October 10, 2005: Message edited by: Bear Bibeault ]
Jeff Wachhorst
Ranch Hand

Joined: Mar 09, 2004
Posts: 41
Considering the following in my JSP:
<tr>
<td>
<c:url value="/FltSvltMnB/result2.scrn" var="resultURL">
<c:param name="flightID" value="${depListItem.flightID}"/>
<c:param name="frTimeFrame"/>
<c:param name="lessThan"/>
</c:url>
<a href='<cut value="${resultURL}"/>'>
<cut value="Select this flight"/>
</a>
</td>
</tr>

As it is now, the screen containing this code comes up OK. I click on "Select this flight" and FltSvltMnB is invoked because my web.xml contains this mapping:

<servlet>
<description>My Main Servlet for getting flights</description>
<display-name>FltFrontControllerB</display-name>
<servlet-name>FltSvltMnB</servlet-name>
<servlet-class>fltPkg1.FltSvltMnB</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>FltSvltMnB</servlet-name>
<url-pattern>/FltSvltMnB/*</url-pattern>
</servlet-mapping>

Based on your suggestion, I changed this:

<a href='<cut value="${resultURL}"/>'>
<cut value="Select this flight"/>
</a>

to this:

<input type="submit" value="Select this flight" >

and then put this in the JSP:

<form method="POST" action="resultURL"> <%-- resultURL is the var in <c:url..> that I've set to /FltSvltMnB/result2.scrn. --%>

It didn't work. resultURL does not get resolved to /FltSvltMnB/result2.scrn and I get a java.lang.StringIndexOutOfBoundsException. (In my underlying program
FltSvltMnB, request.pathInfo() shows /resultURL is what was received there). If it seems like I don't know what I'm doing it's because I don't. I tried this too:

<form method="POST" action=resultURL>

No change, still get the java.lang.StringIndexOutOfBoundsException.

Can you straighten me out?

Thank you for your help!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61661
    
  67

Another first thing: what JSP container are you using? Tomcat? Version 4 or 5?

in the following:



you didn't tell the JSP translator that resultUrl was a scoped variable. it thinks it's just a string. In other words, you forgot the <c out>.

Also, you do not put the params on the URL. That's what the form elements are for.

And... put your code inside the code tags... just like you would with HTML markup.
[ October 11, 2005: Message edited by: Bear Bibeault ]
Jeff Wachhorst
Ranch Hand

Joined: Mar 09, 2004
Posts: 41
I'm running JBoss-4.0.1sp1 (which uses Tomcat/5.0.28) inside Eclipse3.0.1 which is running MyEclipse3.8.4. JBoss is running with j2sdk1.4.2_07. My operating system is Windows XP Pro.

I believe that I have the <form..> tag working OK. Again, here's the <c:url..> bit in my JSP (named result1RndTrp.jsp):

<c:url value="/FltSvltMnB/result2.scrn" var="resultURL" scope="request">
<c:param name="flightID" value="${depListItem.flightID}"/>
<c:param name="frTimeFrame"/>
<c:param name="lessThan"/>
</c:url>

<input type="submit" value="Select this flight" />

Here is my <form..> tag:

<body
bgcolor="#cc99ff">
<form method="POST" action=<cut value="${resultURL}" /> >

I'm successfully getting to FltSvltMnB but I don't know how to retrieve "result2.scrn" from there. When running my application, and looking at result1RndTrp.jsp, the address in the address bar shows this:

http://localhost:8080/Flt2/FltSvltMnB/result1.scrn

In FltSvltMnB, I've tried HttpServletRequest methods getPathTranslated(), getRequestURI() and getPathInfo() and all 3 contain result1.scrn, not result2.scrn which is what I need to get. I've also tried getParameter("resultURL") and getAttribute("resultURL") and both return null.

What do I need to do? (I tried taking a break, but no lightbulb came on).

Why not just put the address I want in the <form..> tag? (Like this):

<form method="POST" action="/Flt2/FltSvltMnB/result2.scrn">

Because on result1RndTrp.jsp, there are multiple flights displayed and a <c:url..> tag for each one. The user has to be able to select the flight. In the <c:url..> tag, note this:

<c:param name="flightID" value="${depListItem.flightID}"/>

Each flight has a unique flightID. When they select the flight, I want to assign that flightID to request parameter "flightID" so that in my underlying program, I can identify the flight they selected. Not only that, I also need to assign values to "frTimeFrame" and "lessThan" which are values to be set by the user on result1RndTrp.jsp. That is, they are NOT properties which have already been set that can be retrieved with:

<c:param name="frTimeFrame" value="${beanName.frTimeFrame}"/>
<c:param name="lessThan" value="${beanName.lessThan}"/>

This is what originally led me to believe I could possibly use just the <c:url..> tag to do what I needed to do; I put "resultURL" inside a <a href..> tag (underneath the <c:param..> tags), they click on it, all 3 of the needed parameters are sent, life is wonderful. OK, it isn't that simple. This is the problem I'm trying to sort out.
If I can provide more clarification or am not being clear, please let me know. And thanks so much for your help and for reading this far!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to set a request parameter in a JSP and pass it in a c:param tag?