Kevin
Kevin

Reputation: 51

Printing multiple HTML lines from JSP

    out.println(%><form> + 
        <p><label for="username">Username:</label><input type="text" name="username" /></p>  + 
        <p><label for="password">Password:</label><input type="password" name="password" /></p> +
        <p class="submit"> +
            <input type="submit" name="button" value="Login" /> +
            <input type="submit" name="button" value="Registrer" /> +
        </p> +
    </form> <%);

That is what i thought i could do, but obviously i can't so my question is, how do i print multiple lines of HTML? Is there a better way to do it than the way i'm doing ?

Upvotes: 0

Views: 1932

Answers (1)

BalusC
BalusC

Reputation: 1108732

You can't. You've to put them in one large quoted string.

out.println("<form>" + 
    "<p><label for=\"username\">Username:</label><input type=\"text\" name=\"username\" /></p>"  + 
    "<p><label for=\"password\">Password:</label><input type=\"password\" name=\"password\" /></p>" +
    "<p class=\"submit\">" +
        "<input type=\"submit\" name=\"button\" value=\"Login\" />" +
        "<input type=\"submit\" name=\"button\" value=\"Registrer\" />" +
    "</p>" +
"</form>");

But much better is to just not use out.println() (and all other scriptlets in JSP). Put HTML plain in JSP and use if necessary JSTL core tags to control the flow.

E.g.

<c:if test="${empty activeuser}">
  <form>
    <p><label for="username">Username:</label><input type="text" name="username" /></p>
    <p><label for="password">Password:</label><input type="password" name="password" /></p>
    <p class="submit">
      <input type="submit" name="button" value="Login" />
      <input type="submit" name="button" value="Registrer" />
    </p>
  </form> 
</c:if>

See also:

Upvotes: 2

Related Questions