2014-12-04 27 views
-1

我将一些HTML代码添加到servlet中,但是当我粘贴代码时,Eclipse会抛出一个错误,说String literal is not properly closed by a double-quote如何纠正由于字符串文字错误而导致的HTML语法?

我试图通过检查HTML块的语法来解决这个问题,但它似乎是正确的。

任何人都可以在声明中发现错误吗?

这是有问题的代码片段:

out.println (
     "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" 
      \"http://www.w3.org/TR/html4/loose.dtd\">\n" + 
     "<html> \n" + 
     "<head> \n" + 
      "<meta http-equiv=\"Content-Type\" content=\"text/html; 
      charset=ISO-8859-1\"> \n" + 
      "<title> My first jsp </title> \n" + 
     "</head> \n" + 
     "<body> \n" + 
      "<font size=\"12px\" color=\"" + color + "\">" + 
      "Hello World" + 
      "</font> \n" + 
     "</body> \n" + 
     "</html>" 
    ); 
+0

这就是为什么你不用Java源代码编写HTML。 – 2014-12-04 18:06:34

回答

2

你只需要加入行:

out.println ("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n" + "<html> \n" + "<head> \n" + "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\"> \n" + "<title> My first jsp</title> \n" + "</head> \n" + "<body> \n" + "<font size=\"12px\" color=\"" + color + "\">" + "Hello World" + "</font> \n" + "</body> \n" + "</html>"); 
1

你有两个换行符,试试这个:

out.println (
     "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" 
      \"http://www.w3.org/TR/html4/loose.dtd\">\n" + // here is linebreak one - error1 
     "<html> \n" + 
     "<head> \n" + 
      "<meta http-equiv=\"Content-Type\" content=\"text/html; 
      charset=ISO-8859-1\"> \n" + // here is linebreak two - error2 
      "<title> My first jsp </title> \n" + 
     "</head> \n" + 
     "<body> \n" + 
      "<font size=\"12px\" color=\"" + color + "\">" + 
      "Hello World" + 
      "</font> \n" + 
     "</body> \n" + 
     "</html>" 
    ); 

更正:

public static void main(String[] args) 
    { 
     String color = ""; 
     String test = 
       "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n" + 
      "<html> \n" + 
       "<head> \n" + 
       "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\"> \n" + 
       "<title> My first jsp </title> \n" + 
       "</head> \n" + 
       "<body> \n" + 
       "<font size=\"12px\" color=\"" + color + "\">" + 
        "Hello World" + 
       "</font> \n" + 
       "</body> \n" + 
      "</html>"; 
     System.out.println(test); 
    } 
相关问题