Giorgi Margiani
Giorgi Margiani

Reputation: 302

How to include HTML in JSP?

I've searched for this question, there are some answers, but not exactly as my question. So, here is my jsp code:

<body>
    <%
        if (manager.isValid(request.getParameter("username"),request.getParameter("password"))){
            out.print("<h1> Welcome ");
            out.print(request.getParameter("username") + "</h1>");
        } else {
            out.print("<h1> Please Try Again </h1> <br />");
            out.print("Either your username or password is incorrect. Please Try again <br /> <br />");
                        }
    %>
    <%@ include file="LoginForm.html" %>
    

but instead of this, I want to include "loginForm.html" only in "else" block. How can I do it? Of course this doesn't work, but you'll guess what I want:

<%
        if (manager.isValid(request.getParameter("username"),request.getParameter("password"))){
            out.print("<h1> Welcome ");
            out.print(request.getParameter("username") + "</h1>");
        } else {
            out.print("<h1> Please Try Again </h1> <br />");
            out.print("Either your username or password is incorrect. Please Try again <br /> <br />");
            include file="LoginForm.html" ;
        }
    %>

Upvotes: 9

Views: 71675

Answers (2)

Howard J
Howard J

Reputation: 421

You could also include reusable html in a jsp file function and call it for printing where needed. e.g. config.jsp contains

<%!
  public void printMenu()
  {
   %>
   HTML HERE
   <%!
  }
%>

home.jsp contains

<%@include file="config.jsp"%>
<!DOCTYPE html>
<html>
  <body>
      <% printMenu(); %>
    <div id="PeopleTableContainer" style="width: 800px;"></div>

Upvotes: 0

Vinay
Vinay

Reputation: 6881

I would like to show you a way, try like below. You can try with else instead with if.

<body>
        <%
            int x = 10;
            if(x>10) {
        %>
        <%@include  file="some.html" %>
        <%
            }
        %>

</body>

Upvotes: 24

Related Questions