HTML elements can be controlled by server-sided languages using conditional statements. A JSP script using an "if / else" statement can set the display property for the <div> tag before outputting the HTML to the user's computer. The variable value used in the "if" statement is easily passed in the URL to reflect the user's setting.
- Skill level:
Open the JSP file for editing in your favourite script editor. If you are using a WYSIWYG editor, switch to view the source code.
Enter following code for an "if" statement that uses the URL passed variable to control the output:
String hide = request.getParameter( "hide" );
if( hide.equals( "true" ) )
out.println("<a href='mypage.jsp?hide=false'> Show Section </a>");
out.println("<div id='mydiv' style='display:none;'>");
out.println("<a href='mypage.jsp?hide=true'> Hide Section </a>");
out.println("<div id='mydiv' style='display:block;'>");
The JSP code replaces the opening <div> tag. You need to replace "mypage" with the name of the file and the "div id" with the appropriate reference for your <div> tag.
Save the JSP file, then upload it to your Web server and preview it. As you click on the link to hide or show the <div> element, the page will be reloaded from the server with the appropriate display setting to either hide the element or show it as a block.
Tips and warnings
- You need to change only the opening tag and the link controlling whether to hide or show the . You do not need to replicate the remaining HTML for the content of the and the closing tag.
- 20 of the funniest online reviews ever
- 14 Biggest lies people tell in online dating sites
- Hilarious things Google thinks you're trying to search for