• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to pass parameters from one JSP to another JSP

 
Suresh Kanagalingam
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
You can use session.getAttribute() and session.setAttribute() methods. Also you can use cookie and url-rewriting
 
Suresh Kanagalingam
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>>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
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Santhosh kumar Ravindran
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64993
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is not longer 2001 when this was originally posted. It is no longer considered acceptable to put scriptlets in a JSP.
 
Santhosh kumar Ravindran
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might consider hidden input field.
Also you can use JSTL.
url rewriting can also be an other option.
 
Soumil Shah
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64993
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Santhosh kumar Ravindran
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64993
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot pass anything but strings as request parameters.
 
Santhosh kumar Ravindran
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
############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
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64993
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about using Eclipse debugger instead of System.out.print
 
Albareto McKenzie
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Posts: 299
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Pie
    Posts: 64993
    86
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Because Struts tags are being used liberally in the examples, this has been moved to the Struts forum.
     
    Patrick Kok
    Ranch Hand
    Posts: 42
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 163
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic