How to Hide a DIV in JSP HTML

Written by peter finnegan
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Hide a DIV in JSP HTML
Set the display property of a tag using server-sided language. (Comstock/Comstock/Getty Images)

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:


  1. 1

    Open the JSP file for editing in your favourite script editor. If you are using a WYSIWYG editor, switch to view the source code.

  2. 2

    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.

  3. 3

    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.

Don't Miss

  • All types
  • Articles
  • Slideshows
  • Videos
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the site, you consent to the use of cookies. For more information, please see our Cookie policy.