访问从sevrlet转发的JSP文件(在scriptlet中)中的数据时出现异常

时间:2016-10-03 03:58:41

标签: jsp java-ee jsp-tags

我正在尝试访问从Servlet发送的JSP文件数据。 每当我运行项目时,我都会收到异常。 我已经尝试过使用 - ${…}
- <c:out value="${loginFailed}"/>
- <?xml version="1.0" encoding="UTF-8" ?> <jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.0"> <jsp:directive.page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" /> <jsp:text> <![CDATA[ <?xml version="1.0" encoding="UTF-8" ?> ]]> </jsp:text> <jsp:text> <![CDATA[ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> ]]> </jsp:text> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/styles/login/css/style.css" /> <title>Connexion</title> </head> <body> <form class="sign-up" method="Post" action="/TP1MiniProjetBanque/home"> <h1 class="sign-up-title">Connexion</h1> <input type="text" name="mail" class="sign-up-input" placeholder="Mail"/> <input type="password" name="mdp" class="sign-up-input" placeholder="Mot de passe"/> <input type="submit" value="Entrer" class="sign-up-button"/> </form> <% out.println((String)request.getAttribute("loginFailed")); %> </body> </html> </jsp:root>

即使我尝试使用评论标记,也会出现异常。

我很无能为力,欢迎任何帮助。

我的一些阅读/研究

  

AdMob in a fragment
  https://stackoverflow.com/tags/el/info
  https://www.tutorialspoint.com/jsp/jsp_actions.htm
  ...

login.jsp

protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.getWriter().append("Served at :").append(request.getContextPath());
    request.getRequestDispatcher("/login.jsp").forward(request, response);
}


protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String mail = request.getParameter("mail");
    String mdp  = request.getParameter("mdp");

    if (mail.isEmpty() || mdp.isEmpty() || /**/) {
        request.setAttribute("loginFailed", true);
        request.getRequestDispatcher("/login.jsp").forward(request, response);
        return;
    } else {
        Client  user = new Client(mail, mdp);

        request.getRequestDispatcher("/home.jsp").forward(request, response);
        return;
    }

    //doGet(request, response);
}    

JRServlet.java

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">

  <display-name>TP1MiniProjetBanque</display-name>

  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>

  <servlet>
    <servlet-name>home</servlet-name>
    <servlet-class>jrservlet.JRServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>home</servlet-name>
    <url-pattern>/home</url-pattern>
  </servlet-mapping>
</web-app>    

web.xml

workspace/TP1MiniProjetBanque/    
├── build    
│   └── classes    
│       └── jrservlet    
│           ├── Client.class    
│           ├── Compte.class    
│           ├── DB.class    
│           ├── JRServlet.class    
│           └── testing.class    
├── src    
│   └── jrservlet    
│       ├── Client.java    
│       ├── Compte.java    
│       ├── DB.java    
│       ├── JRServlet.java    
│       └── testing.java    
└── WebContent    
    ├── home.jsp    
    ├── login.jsp    
    ├── META-INF    
    │   └── MANIFEST.MF    
    ├── styles    
    │   ├── home    
    │   └── login    
    └── WEB-INF    
        ├── lib    
        │   └── mysql-connector-java-5.1.39-bin.jar    
        └── web.xml    

层次结构

org.apache.jasper.JasperException: /login.jsp (line: 23, column: 11) The content of elements must consist of well-formed character data or markup.
    org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:42)
    org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:443)
    org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:308)
    org.apache.jasper.compiler.JspDocumentParser.parse(JspDocumentParser.java:228)
    org.apache.jasper.compiler.ParserController.doParse(ParserController.java:226)
    org.apache.jasper.compiler.ParserController.parseDirectives(ParserController.java:119)
    org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:194)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:374)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:354)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:341)
    org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:662)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:364)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    jrservlet.JRServlet.doGet(JRServlet.java:41)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause

org.xml.sax.SAXParseException; lineNumber: 23; columnNumber: 11; The content of elements must consist of well-formed character data or markup.
    com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:203)
    com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:177)
    com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:400)
    com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:327)
    com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(XMLScanner.java:1437)
    com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.startOfMarkup(XMLDocumentFragmentScannerImpl.java:2637)
    com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2735)
    com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:606)
    com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:118)
    com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)
    com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:848)
    com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777)
    com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
    com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213)
    com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:643)
    com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl.parse(SAXParserImpl.java:327)
    org.apache.jasper.compiler.JspDocumentParser.parse(JspDocumentParser.java:195)
    org.apache.jasper.compiler.ParserController.doParse(ParserController.java:226)
    org.apache.jasper.compiler.ParserController.parseDirectives(ParserController.java:119)
    org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:194)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:374)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:354)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:341)
    org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:662)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:364)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    jrservlet.JRServlet.doGet(JRServlet.java:41)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.70 logs.

启动Servlet时出现的错误

HTTP状态500 - /login.jsp(第23行,第11列)元素的内容必须包含格式良好的字符数据或标记。

输入例外报告

message /login.jsp(第23行,第11列)元素的内容必须由格式正确的字符数据或标记组成。

说明服务器遇到内部错误,导致无法完成此请求。

异常

{{1}}

Apache Tomcat / 7.0.70

1 个答案:

答案 0 :(得分:2)

好的,问题是在标签内部使用了scriplet是不允许的。相反,您必须将scriplet内容包装在<jsp:scriptlet>标记内,

<jsp:scriptlet>
      out.println((String)request.getAttribute("loginFailed"));
</jsp:scriptlet>

对于允许 c:out 和东西你需要在你的jsp:root元素中包含JSTL定义,比如

<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:c="http://java.sun.com/jsp/jstl/core" version="2.0">

背后的原因是<jsp:root>强制使用XML语法,因此需要使用常用语法的XML替代方案,如scriptlet。

此外,请详细了解<jsp:root> here

并且,XML兼容语法here