Passing parameters to another JSP

Since past few days, I was working on some Java Server Pages, which are linked with each other. Many times I have to include some JSPs in one JSP. While doing so, I need to call the variables defined in one JSP, in those JSP’s which are included one. I thus have to pass these parameters from one JSP to another at the time of Including those.

I searched for the solutions, and found many different ways of doing so.

For including another JSP into a JSP I used the following command in FirstJSP.jsp

<jsp:include page=”SecondJSP.jsp”/>

I searched for it and found many different ways of doing so.

1. JSP:Param

<jsp:include page=“SecondJSP.jsp” />

<jsp:param name=“name” value=“Sulabh Jain” />

<jsp:param name=“age” value=“24″ />

</jsp:include>

The above code will help in setting the values of param1 and param2 as value1 and value2

which can be called or retrieved in SecondJSP.jsp using,

<%

String myName= request.getParameter(“name”)

String myAge= request.getParameter(“age”)

%>

These can be printed in JSP using ‘equals to’ sign (=) with percentage tags,

My Name  is <%=myName%>
My   Age    is <%=myAge%>

2. JSP:Forward

It also behaves similar to jsp:param

<jsp: forward page=”SecondJSP.jsp”>
<jsp: param name=”name” value=”Amar Patel”/>
<jsp: param name=”age” value=”15″/>
</jsp: forward>

Limitations of above two methods :


This method of passing parameters can be used only for Static variables. For eg.
if we have a dynamic variable declared in JSP we cannot pass it using jsp:param

Here it got little difficult,

Now I need to pass dynamic variables which I defined in the first JSP.
The third method provides solution to it.

3. request.setAttribute

Here we will use an object of javax.servlet.http.HttpServletRequest class named as request.
calling the public abstract void setAttribute(java.lang.String name, java.lang.Object o) method we can set the String name as an attribute.

<%

String name = “Sulabh Jain”;

request.setAttribute(“myName”, name);

%>

Now we can easily get in in SecondJSP as

<%

String nameInNewJSP = request.getAttribute(“myName”).toString();

%>

toString is used here to convert the object into string.

References:

1. Passing parameters, http://www.ezdia.com/Passing_parameters_to_JSP_using_jsp%3Aparam/Content.do?id=968

2. Calling a new JSP, http://www.ezdia.com/Calling_a_new_JSP/Content.do?id=969

3. JSP exercises, http://www.ezdia.com/JSP_Exercises/Content.do?id=806

Please post your comments, on the above provided code, and suggest if there exists better or more options.

Thanks

Displaying hidden Divs on Mouse Clicks

Hello Guys, I was trying to create a Mail Box, where on clicking at every Row, I can read the mail content there itself.

As I click on a div, one Div opens right below it, and when I click on it again the Div must hide again.

I am providing the HTML code for such functionality.

Hidden Div for row one is appearing

<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN”>
<HTML>
<HEAD>
<TITLE> Opening and Hiding Divs on Mouse Clicks </TITLE>
<script>
function showHiddenDiv(divid){
var visibility = document.getElementById(divid).style.display;
if(visibility == “none”){
document.getElementById(divid).style.display = ”;
}
else{
document.getElementById(divid).style.display = ‘none’;
}
}
</script>
</HEAD>

<BODY>
<table width=”350″ border=”1″ datasrc=”#cdcat”>

<tr><td id=”row1″ onclick=”showHiddenDiv(‘hiddenRow1′);” align=”center”>Row One</td></tr>
<tr id=”hiddenRow1″ style=”display:none” height=”50″><td align=”center”>”hidden content for row one”</td></tr>
<tr><td id=”row2″ onclick=”showHiddenDiv(‘hiddenRow2′);” align=”center”>Row Two</td></tr>
<tr id=”hiddenRow2″ style=”display:none” height=”50″><td align=”center”>”hidden content for row two”</td></tr>
<tr><td id=”row3″ onclick=”showHiddenDiv(‘hiddenRow3′);” align=”center”>Row Three</td></tr>
<tr id=”hiddenRow3″ style=”display:none” height=”50″><td align=”center”>”hidden content for row three”</td></tr>
<tr><td id=”row4″ onclick=”showHiddenDiv(‘hiddenRow4′);” align=”center”>Row Four</td></tr>
<tr id=”hiddenRow4″ style=”display:none” height=”50″><td align=”center”>”hidden content for row four”</td></tr>

</table>
</BODY>
</HTML>

This code has been used at various places and uses simplest functions possible.

function showHiddenDiv(divid) is used for displaying and hiding the div whose Id has been passed as divid where ever the function is called.

document.getElementById(divid).style.display is used to get the display style status of the Div whose Id is divid. It will be “none” is Div is hidden & it will be blank if the div is Displayed.

References :

1. Drop Down Menu boxes : http://www.ezdia.com/Drop_down_menus_in_HTML/Content.do?id=495

2. All about element display in html : http://www.ezdia.com/All_about_showelementbyid_in_html/Content.do?id=946

3. Working examples with explanations : http://www.ezdia.com/Favorite_Javascripts_for_designers/Content.do?id=947

Follow

Get every new post delivered to your Inbox.