如何在JSP中包含HTML?

时间:2013-05-04 16:03:20

标签: java html jsp

我搜索过这个问题,有一些答案,但不完全是我的问题。所以这是我的jsp代码:

<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" %>

但不是这样,我想只在“else”块中包含“loginForm.html”。 我该怎么做? 当然这不起作用,但你会猜到我想要的东西:

<%
        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" ;
        }
    %>

2 个答案:

答案 0 :(得分:18)

我想告诉你一个方法,试试如下。你可以尝试使用else而不是if

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

</body>

答案 1 :(得分:0)

您还可以在jsp文件函数中包含可重复使用的html,并在需要时调用它进行打印。例如 config.jsp包含

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

home.jsp包含

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