我正在向servlet添加一些HTML代码但是当我粘贴代码时,Eclipse会抛出一个错误,说String literal is not properly closed by a double-quote
。
我试图通过检查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 :(得分: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 :(得分: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);
}