views:

1641

answers:

2

I have an ID that I need in the next jsp once the user click a button. I am trying to do the following:

FirstJSP.jsp:

function getSecond() { var frm = document.getElementById("frm"); frm.action = "**second.jsp?id=myId;"** frm.submit(); }

... form id="frm" ..... input type="button" value="Next" onclick="getSecond()"/

......

This code takes me to my second page, but if I tried to access the id, it gives me an error since the id is null.

I access the id in the second page by:

final Long passedId = Long.parseLong(request.getParameter("id"));

I think I am not passing the parameter correctly, but I don't know how to do it. Any ideas? Thank you so much!

A: 

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.

Jay
You are right Jay. But if I tried just adding myId, the variable is "unresolved variable or Type". myId is defined at the beginning as <% final Long myId = Long.parseLong(request.getParameter("id")); %>I have changed the action line to frm.action="second.jsp?id="+<%=myId%>; and still doen't work
+1  A: 

Ok... I found my mistake... I wans't using any method in my form, so I updated it to form name="frm" method="post"

Also, like I mentioned in my last note, the action line is now frm.action="second.jsp?id="+<%=myId%>;

It works now!

You figured it out before I could reply to your reply? Cool. Then I can stop thinking about it and resume my nap.
Jay
By the way, if the HTML form element doesn't have any 'method' attribute specified, then it will default to "GET". In such case, you would need to implement Servlet's doGet() instead.
BalusC