我的jsp给出生成的servlet错误:无效的转义序列(有效转义序列是\ b \ t \ n \ f \ r \“\'\\ \)

时间:2013-08-23 09:01:46

标签: java jsp

我尝试过双反斜杠,但它一直给我同样的错误。以下是我正在使用的代码:

<%@ page  import="java.io.*"%>
<%@ page  import="java.sql.Connection"%>
<%@ page  import="java.sql.DriverManager"%>
<%@ page  import="java.util.HashMap"%>
<%@ page  import="java.util.Map"%>
<%@ page  import="net.sf.jasperreports.engine.*"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML>

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Planning</title>
    </head>
    <body>
        <h2>Planning</h2>
        <%
            Connection conn = null;
            try {
                Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                conn = DriverManager.getConnection("jdbc:sqlserver://DC1VCWSQL105\sql05:1433;databaseName=TD_DA_Test;integratedSecurity=True");
        } catch (Exception ex) {
            ex.printStackTrace();


        }
        File reportFile = new File(application.getRealPath("//bespoke//rapport//Planning.jasper"));
        Map parameters = new HashMap();
        byte[] bytes = JasperRunManager.runReportToPdf(reportFile.getPath(), parameters, conn);

        response.setContentType("application/pdf");
        response.setContentLength(bytes.length);
        ServletOutputStream outStream = response.getOutputStream();
        outStream.write(bytes, 0, bytes.length);
        outStream.flush();
        outStream.close();
    %>

</body>

为什么我的情况不起作用? 确切的错误是:

发生了错误:

PWC6033: Unable to compile class for JSP PWC6197: An error occurred at line: 18 in the jsp file: /bespoke/rapportplanning/Planning.jsp PWC6199: Generated servlet error: Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\ )  
org.apache.jasper.JasperException: PWC6033: Unable to compile class for JSP PWC6197 

4 个答案:

答案 0 :(得分:1)

正如@ S.R.I所提到的,String中的“\”必须被转义:“\”。正确的行是:

conn = DriverManager.getConnection("jdbc:sqlserver://DC1VCWSQL105\\sql05:1433;databaseName=TD_DA_Test;integratedSecurity=True");

答案 1 :(得分:1)

更好的方法可能是 -

String url = "jdbc:oracle:thin:@amrood:1521:EMP";
String user = "username";
String pass = "password"
Connection conn = DriverManager.getConnection(url, user, pass);

或者您可以在属性文件中移动这些属性,然后使用 -

DriverManager.getConnection(String url, Properties info);

答案 2 :(得分:0)

在您的连接网址

jdbc:sqlserver://DC1VCWSQL105\sql05:1433;databaseName=TD_DA_Test;integratedSecurity=True

Java编译器会将\s视为转义,并导致它不是有效的转义符。所以发生了错误。

Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\ )  

使用\\s而Java编译器会将\\视为转义,将s视为char。

答案 3 :(得分:0)

http://en.wikipedia.org/wiki/Escape_character这里有一些关于转义字符和序列的信息。 您的代码有一个转义字符\,必须遵循正确的转义序列,在您看来,您希望在字符串中包含单个\,如果是这样,只需键入{{ 1}},这个转义序列将在输出字符串中只替换为\\