New Line in Javascript Alert

I'm writing out some javascript code in my
servlet, and if I put a "\n" into my code -
I get a undetermined string error.  If I take
it out, it works fine.

Here's sample code:

 public void doGet (HttpServletRequest request, HttpServletResponse response)
                                                                      throws ServletException, IOException{
             PrintWriter      out;         
       // set content type and other response header fields first
             response.setContentType("text/html");
           // then write the data of the response
       out = response.getWriter();

             // html text ........................

            out.println("<SCRIPT language=\"JavaScript\">");
            out.println("<!--");
            out.println("function Warning(war, war1){");
            out.println("if(war == \"\" || war1 == -1) {");
     ??? out.println("alert(\"1. Save As \n 2.Open \" );");
            out.println("return (false);");
       out.println("}");
       out.println("else {");
      out.println("return (true);");
      out.println("}  }");
          out.println("//-->");
      out.println("</SCRIPT>");

            // html text.........................
       }
LVL 1
mraibleAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

knightEknightCommented:
out.println("alert(\"1. Save As \\n 2.Open \" );");
                                   
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
knightEknightCommented:
You must escape the backslash with a backslash, otherwise the newline character is written to the alert statement, instead of the text that the alert is supposed to display.  For example, your code above would output this:

 alert("1. Save As
 2. Open");


instead of:

 alert("1. Save As \n2. Open");
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.