This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Struts and the fly likes How to pass parameters from one JSP to another JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "How to pass parameters from one JSP to another JSP" Watch "How to pass parameters from one JSP to another JSP" New topic
Author

How to pass parameters from one JSP to another JSP

Suresh Kanagalingam
Ranch Hand

Joined: Aug 17, 2001
Posts: 82
Hello:
I am writing a 2 JSP programs. The first calls the second JSP. Firat JSP accesses a dB to get data and passes it to second JSP to do more processing. I can call second JSP from first using <jsp:forward page="/Mydir/test.jsp"/> (without passing parameters). This calls the second jsp fine.
Now I want to pass 2 parameters - one is received from the http call (of 1st JSP) & the other is from dB search - to second jsp using 'forward' tag.
How can I do this? Is this doable? If not what are my other options?
Thanks in advance.
Suresh

------------------
Andrey Opanasets
Greenhorn

Joined: Aug 14, 2001
Posts: 17
Hi
You can use session.getAttribute() and session.setAttribute() methods. Also you can use cookie and url-rewriting
Suresh Kanagalingam
Ranch Hand

Joined: Aug 17, 2001
Posts: 82
Can you provide me a sample for session.getAttribute(0 and setAttribute() please?
Thanks
Suresh
Originally posted by Andrey Opanasets:
Hi
You can use session.getAttribute() and session.setAttribute() methods. Also you can use cookie and url-rewriting

Manjunath Reddy
Ranch Hand

Joined: Jul 26, 2001
Posts: 60
>>Now I want to pass 2 parameters - one is received from the >>http call (of 1st JSP) & the other is from dB search - to >>second jsp using 'forward' tag.
Forward tag doesnt support this.
>>How can I do this? Is this doable? If not what are my other >>options?
There are several options.
1. If the data to be passed is small one can pass them as query string parameters or as hidden field.
2. The other method is more popular. Here one can set the parameters into the request object either as a String param or as a Custom Class containing the parameters. User request.setAttribute("myparams", MyParams) and in the second page use request.getAttribute("myParams").
Hope this gets u going...
cheers,
mpr
Suresh Kanagalingam
Ranch Hand

Joined: Aug 17, 2001
Posts: 82
Hi Manjunath,
The parametes are max 12 bytes in total. So I tried to pass as query string paramentes, but I am getting an error message saying 'attibute not found'. The syntax I am using is as follows:
<%jsp:forward page=="/Mydir/test.jsp"?Key=1234568&Schema=COM/>
Do you know what is wrong with my syntax?
Thanks
Suresh

Originally posted by Manjunath Reddy:
>>Now I want to pass 2 parameters - one is received from the >>http call (of 1st JSP) & the other is from dB search - to >>second jsp using 'forward' tag.
Forward tag doesnt support this.
>>How can I do this? Is this doable? If not what are my other >>options?
There are several options.
1. If the data to be passed is small one can pass them as query string parameters or as hidden field.
2. The other method is more popular. Here one can set the parameters into the request object either as a String param or as a Custom Class containing the parameters. User request.setAttribute("myparams", MyParams) and in the second page use request.getAttribute("myParams").
Hope this gets u going...
cheers,
mpr

Manjunath Reddy
Ranch Hand

Joined: Jul 26, 2001
Posts: 60
I guess you have some syntax errors. And i also am not sure whether you are using request or session objects. Anywayz i post here 2 simple file code that will give you a complete picture.
1.jsp(which contains the forward page attribute)
<pre>
<%@ page import='java.util.*'%>
<html>
<head><title>Test Page 1</title></head>
<body bgcolor=#ffffff>
<%String forwardToPage="/2.jsp?Key=1234568&Schema=COM";%>
<jsp:forward page = "<%=forwardToPage%>" />
</body>
</html
</pre>
2.jsp(which is the redirected page that prints the params)
<pre>
<%@ page import='java.util.*'%>
<html>
<head><title>Test page 2</title></head>
<body bgcolor=#ffffff>
<%
out.println(request.getParameter("Key") + " KEY " + request.getParameter("Schema") + " SCHEMA " );
%>
</body>
</html>
</pre>
When you hit the 1.jsp it must take you to the 2.jsp and print the attributes/parameters.
hope this helps
cheers,
mpr
ruijin yang
Ranch Hand

Joined: Apr 29, 2001
Posts: 96
Also,you can use:
<jsp:forward page ="/2.jsp">
<jsp aram name="Key" value="12345"/>
<jsp aram name="Schema" value="COM"/>
</jsp:forward>
-------------
ruijin yang
SCJP2

SUN Certified Business Component Developer Java EE Platform 5 (2008)<br />SUN Certified Developer for Java Web Services (2007)<br />IBM WebSphere Portal V5.1 Application Development (2006)<br />SUN Certified Web Component Developer (2001)<br />SUN Certified Java 2 Programmer (2001)
Santhosh kumar Ravindran
Greenhorn

Joined: Nov 14, 2009
Posts: 13
There are a few possibilities, depending on what exactly you need to
do.

If the first page is going to automatically invoke the second, you can
use a jsp:include or jsp:forward, and pass the values with one or more
jsp:param tags. This would look something like:

<jsp:forward page="secondpage.jsp">
<jsp:param name="parameter1_name" value="parameter1_value">
<jsp:param name="parameter2_name" value="parameter2_value">
...
</jsp:forward>

paramater1_value could be a simple expression, or a value of the form
<%= something %>.

If the first page requires the user to click a form submit button, the
values can be passed as hidden fields, which would look like this:

<form action="secondpage.jsp" method="POST">
<input type="hidden" name="parameter1_name" value="parameter1_value">
<input type="hidden" name="parameter2_name" value="parameter2_value">
...
<input type="submit">
</form>

Again, the parameter values can be <%= %> expressions.


If the page requires the user to click an href before proceeding, you
can pass the parameters in a query string, as in:

<a href="secondpage.jsp?paramater1_name=parameter1_value¶mater2_name=parameter2_value&... ">

Again, you can use <%= %> tags for the values, but there are some
things to watch for. If the result of the <%= %> is going to contain
any of the characters %, &, = or / you might need to urlencode the
values before they can be used. In this case, it would probably be
easer to use some session variables as suggested by other Ranchers
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60806
    
  65

It is not longer 2001 when this was originally posted. It is no longer considered acceptable to put scriptlets in a JSP.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Santhosh kumar Ravindran
Greenhorn

Joined: Nov 14, 2009
Posts: 13
Bear Bibeault wrote:It is not longer 2001 when this was originally posted. It is no longer considered acceptable to put scriptlets in a JSP.


Dear Bear Bibeault,

i cant understand what you are arriving at. will you explain little broad ?
cow clarke
Greenhorn

Joined: Nov 12, 2009
Posts: 15
You might consider hidden input field.
Also you can use JSTL.
url rewriting can also be an other option.
Soumil Shah
Ranch Hand

Joined: Jul 13, 2009
Posts: 54
Suresh Kanagalingam wrote:<%jsp:forward page=="/Mydir/test.jsp"?Key=1234568&Schema=COM/>
Do you know what is wrong with my syntax?

Might be this is wrong --
you have used two equals == (page==) and you have put quote wrongly <%jsp:forward page=="/Mydir/test.jsp?Key=1234568&Schema=COM" />
The better way could be to use get/set attribute methods as suggested by many here.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60806
    
  65

Santhosh kumar Ravindran wrote:i cant understand what you are arriving at. will you explain little broad ?

In 2002, JSP 2.0 introduced native EL support, along with the JSTL replaced the need for scriptlets in JSP pages. The time to stop using discredited scriptlets is long past.
Samuel Behrman
Ranch Hand

Joined: Oct 02, 2009
Posts: 49
Well if the code fragment you give above is literally what you are writing, then you are setting id to the text "myId". When you try to parse this as a Long it throws an exception because the string is not a valid number -- it doesn't contain any digits.

I presume what you want to say is something more like

frm.action="second.jsp?id="+myId

Assuming that myId has been defined somewhere and is a number.


RAQ Report: Web-based Excel-like Java reporting tool.
Santhosh kumar Ravindran
Greenhorn

Joined: Nov 14, 2009
Posts: 13
Hi Ranchers,

yeah me too dwelling into the same sea. i tried to pass "bean" parameters in url to another jsp which i will get in target jsp using request.

i used window.open function to open the target jsp in new window.

The Problem is one of the parameter to be passed is an "date" parameter. when i tested it, i got URLDecoding failed and java.io.char,,,,, and more errors.

the snippet will be like this :
Integer one=(Integer) session.getAttribute("one");
---- i got the bean using struts iterate tag.
hi.jsp?one=<%=one%>%>+two=<%=bean.getdate()%>

Any help will be appreciated .

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60806
    
  65

You cannot pass anything but strings as request parameters.
Santhosh kumar Ravindran
Greenhorn

Joined: Nov 14, 2009
Posts: 13
what are all the ways by which we can pass parameters from one jsp to another ?

i am using window.open in javascript passing a url in which all parameters are set like :

hi.jsp?one=<%=one%>+two=<%=two%>

but it is not working when i pass date as one of the parameter.(Error: character decoding failed on target jsp)
also, the date is got from a bean using logic:iterate tag in struts

so may i know what are the other ways by which i can pass parameters getting from a bean o another jsp ?
Albareto McKenzie
Ranch Hand

Joined: Apr 08, 2009
Posts: 298
As said before only Strings are suitable as parameters, if you have an String you can pass it. Otherwise you can set the object in as an attribute instead of a parameter.

What kind of object do you have in one and in two? Provide more information if you want us to help you.
Santhosh kumar Ravindran
Greenhorn

Joined: Nov 14, 2009
Posts: 13
############source.jsp################
<head>
<script language="JavaScript">
function openKunden(url)
{
F1 = window.open(url, "","width=800,height=400,left=320,top=100,resizable=yes, scrollbars=yes");
F1.focus();
}
</script>
</head>

<%
Integer pkv=(Integer)session.getAttribute("king");
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
%>

<logic:iterate id="abo" name="Abbo">
<TR align="left">

<html:form action="/KingAction">


<TD width="8%">
<html:text name="abo" property="ktyp" styleClass="readonly" readonly="true" size="20" />
</TD>

<TD width="8%">
<html:text name="abo" property="vo" styleClass="readonly" readonly="true" size="10" />
</TD>

<TD width="6%">
<html:text name="abo" property="bi" styleClass="readonly" readonly="true" size="10" />
</TD>

<TD width="6%">
<html:submit property="save" styleClass="button" ><bean:message key="show.typ" /></html:submit >
</TD>

######### On the click of the below button i want to pass that bean property like vo(Date data type), bi (Date data type), ktyp(String data type) to another jsp ##################

<TD width="6%">
<html:button property="" styleClass="button" onclick="JavascriptpenKunden('/aking.jsp?pk=<%=pkv%>+vo=<%=formatter.format(abo.getVo())%>+bi=<%=formatter.format(abo.getBi())%>+ktyp=<%=formatter.format(abo.getKtyp())%>')">
<bean:message key="show.ktyp" />
</html:button>
</TD>

<TD> </TD>
</html:form>
</TR>
</logic:iterate>

####################destination.jsp###########################


<%

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); \
(ERROR IS FROM THIS BELOW LINE)
Date vo = formatter.parse(request.getParameter("vo"));
Date bi = formatter.parse(request.getParameter("bi"));
Integer pkV = new Integer(0);
String ktyp=request.getParameter("ktyp").toString();
pkV = new Integer(request.getParameter("pkv").toString());
KVO kvo=null;
KVO[] k = SessionHome.getDataSourceConnectionSLLocal().getAkForTyp(pkV, vo, bi, false, false,
ktyp);
%>

Is my approach correct ?

else please suggest some other way Ranchers .

Hope you understood my problem.
Thanks in Advance!


Albareto McKenzie
Ranch Hand

Joined: Apr 08, 2009
Posts: 298
If error is here > Date vo = formatter.parse(request.getParameter("vo"));
look what you are getting in the request.getParameter!!! maybe you are not getting the proper format.

Other small thing, you don't have to do toString() in request.getParameter() as getParameter ALWAYS returns a String.
Santhosh kumar Ravindran
Greenhorn

Joined: Nov 14, 2009
Posts: 13
The error i got is .
Parameter Decoding Failed .
java.io.charconversion: is Hex digit.....

To prevent any formatting error , i used following code to set the date in request in proper format as string:
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
formatter.format(abo.getBi());


And on the Target jsp, to format the date which is received as string i used the following code :
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
Date bi = formatter.parse(request.getParameter("bi"));

Previously , i had not done this formatting and passed directly ! but that time also i got the same error ?

May i know how to see what value i get in the request in target jsp ???

i am using eclipse for my project..

Thanks !
Albareto McKenzie
Ranch Hand

Joined: Apr 08, 2009
Posts: 298
You can put a System.out.println("myParam is [" + request.getParameter("bi") + "]"); and look what appears in the console.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60806
    
  65

Please be sure to use code tags when posting code to the forums. Unformatted code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please read this for more information.

You can go back and change your post to add code tags by clicking the button on your post.
Anoop Krishnan
Ranch Hand

Joined: May 03, 2001
Posts: 163
What about using Eclipse debugger instead of System.out.print


I just want to know is there any body call my bean's Getter and Setter methods with "Please" in front - My favorite quip from Bugzilla
Albareto McKenzie
Ranch Hand

Joined: Apr 08, 2009
Posts: 298
That's a good idea only if he is using Eclipse :P anyway the jsp debugging is not so good either in Eclipse.
Santhosh kumar Ravindran
Greenhorn

Joined: Nov 14, 2009
Posts: 13
Friends,

i need to know a way to pass Date obtained from a bean in one of the iteration of <logic:iterate> of Struts from one jsp to another jsp on the "onclick" event of a button.


So, my logic is to display some rows through iteration, in which each row will consist of

  • text box1 - showing ktyp string
  • text box - showing vo date
  • text box - showing bi date
  • button - upon whose click i will pass the above three items to another jsp


  • Please provide me help on how to pass those three parameters( a string, two dates) to another jsp .

    Thanks in Advance !!!
    Albareto McKenzie
    Ranch Hand

    Joined: Apr 08, 2009
    Posts: 298
    Please provide me help on how to pass those three parameters( a string, two dates) to another jsp .


    As said before some times, you can not pass a Date, you only pass String through the parameters. I mean, is not an option or something that you can choose, if you pass parameters, they are Strings.
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 60806
        
      65

    Because Struts tags are being used liberally in the examples, this has been moved to the Struts forum.
    Patrick Kok
    Ranch Hand

    Joined: Nov 12, 2009
    Posts: 42
    I can hardly believe that there are still a lot of people picking up the damn old JSP scriptlet.
    You are, again, suggested to use EL, JSTL, OGNL on JSP.
    Anoop Krishnan
    Ranch Hand

    Joined: May 03, 2001
    Posts: 163
    Yes but they have an argument ,working in jsp's with EL is like typing a text document . You are just working with strings and you don't see any compilation error and you do not have any code assist from the editor.
    I also prefer using EL & JSTL but some times it sucks.
    stephen wiger
    Greenhorn

    Joined: Dec 08, 2009
    Posts: 1
    This thread is very helpful for me because i am also using jave for web development I am also learn a lot from
    free scripts . you can also try free scripts & can learn more without any difficulty & anxiety
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: How to pass parameters from one JSP to another JSP
     
    Similar Threads
    How to get results into same jsp
    storing variable so it can be used by 2 servlets
    Passing array of checkboxes from one JSP to another
    Servlet - doGet - doPost - parameter
    inserting information in DB from jsp