Line break in Java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Alien
    New Member
    • Sep 2007
    • 61

    #1

    Line break in Java

    Hi
    I am currently learning JSP and was wondering how you would implement a line break?

    [code=java]
    <% java.util.Date d1;
    java.util.Date d2;

    d1= new java.util.Date( );
    d2 = new java.util.Date( );

    out.println(d1) ;
    out.println("\n ");
    out.println(d2) ;
    [/code]

    I thought the line would do the trick but it didn't. The out put is:

    This is the heading

    This is the body

    Fri Aug 14 23:04:55 PDT 2009 Fri Aug 14 23:04:55 PDT 2009
    Basically I wanted to have the two dates in two lines.

    Anyone any idea? Thanks.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    You are printing HTML code right? A "<br>" tag prints a new line in HTML, so simply print that tag.

    kind regards,

    Jos

    Comment

    • Alien
      New Member
      • Sep 2007
      • 61

      #3
      Yes its HTML but I dont think you can insert any HTML or non-Java stuff between <% %>

      I tried it didn't work. But outside that yes, <br> works like it normally would.

      Comment

      • Alien
        New Member
        • Sep 2007
        • 61

        #4
        Oh I think I get what you mean,

        Some thing like this:

        date 1 is : <%= d1%> <br>
        date 2 is : <%= d2%>

        and putting the remainder in a scriplet <% %>.

        Thanks.

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          You don't really want to use those scriplets in JSPs these days.
          Better keep all the Java code in 'pure' servlets or in separate helper Java functions.
          You can use EL to access objects in various scopes too.

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by Alien
            Yes its HTML but I dont think you can insert any HTML or non-Java stuff between <% %>

            I tried it didn't work. But outside that yes, <br> works like it normally would.
            Simply do a out.println("<b r>") just as the OP was doing with his dates?

            kind regards,

            Jos

            Comment

            • karthickkuchanur
              New Member
              • Dec 2007
              • 156

              #7
              Use this

              System.getPrope rty("line.separ ator");

              Comment

              • esami
                New Member
                • Aug 2009
                • 1

                #8
                I think the problem here is that JSP pages. They require HTML style line breaks (like any HTML documents). So, try using "<br />" instead of "\n".

                Comment

                Working...