sending variable from one jsp to another jsp
I have one JSP file as jsp 1.jsp and another JSP file as jsp 2.jsp
I've included jsp 2.jsp in jsp 1.jsp using <%@include file="jsp 2.jsp" %>
Now I need a click event on some element. And on that event I want to transfer a string variable to included jsp.
Lets say I have a list and on click of it I want to transfer the name of the list to another JSP,
And in another JSP I am trying to use that string to carry out some task.
And I am doing all these without any servlet. challenging one!! I have google'd a lot, but didnt find anything.
You have a number of options:
-
Store it in the session.
// Memorise any passed in user. String username = request.getParameter("username"); if (username != null && username.length() > 0) { session.setAttribute("username", username); }
-
Store it as a hidden field in the form.
<input name="username" type="hidden" value=""/>
-
Store it in a cookie.
username = getCookie(userCookieName); // Get from cookie. function getCookie(name) { if (document.cookie) { index = document.cookie.indexOf(name); if (index !== -1) { f = (document.cookie.indexOf("=", index) + 1); t = document.cookie.indexOf(";", index); if (t === -1) { t = document.cookie.length; } return(document.cookie.substring(f, t)); } } return (""); }
-
Persist it on the client side in sessionStorage. See here for details.
sessionStorage.setItem("username", "...");
-
Not really another option but a mechanism - pass it in the URL:
.... onclick="window.location='details.jsp?username=...'
If the reason for including your jsp 2
in jsp 1
is for sending variable, then its not needed
.
you just need to set a hidden
variable in jsp 1
, and in your jsp 2
you should able to access it, by using the jsp implicit request
object.