首页 > 解决方案 > Tomcat 无法为 JSP 编译类

问题描述

所以我正在做一个项目,它涉及在java中编写一个访问sql数据库的servlet。该 servlet 通过前端 jsp 页面访问,所有这些都托管在 tomcat 上。我把所有文件都放在了正确的位置,当我尝试在 Tomcat 上打开它时,我得到了这个......

Exception Report

Message Unable to compile class for JSP:

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

org.apache.jasper.JasperException: Unable to compile class for JSP: 

An error occurred at line: [32] in the jsp file: [/index.jsp]
textBox cannot be resolved to a variable
29:             <form action = "/Project3/main" method = "post" style="margin-top: 15px;" class="text-center">
30:                 <div class="form-group row">
31:                     <div class=" col-sm-12 col-md-12 col-lg-12">
32:                         <textarea name="textBox" class="form-control" id="textBox" rows="8" cols="50"><%= textBox %></textarea>
33:                     </div>
34:                 </div>
35: 


An error occurred at line: [45] in the jsp file: [/index.jsp]
result cannot be resolved to a variable
42: 
43:     <div class="text-center">
44:         <%-- jsp statement with out sql response--%>
45:         <%= result %>
46:     </div>
47: 
48: 

所以我的jsp文件中有两个无法解析的变量。不知道这意味着什么。这是我的jsp代码的主体:

<body>


    <div class="container-fluid ">
        <row class="row justify-content-center">
            <h1 class="text-center col-sm-12 col-md-12 col-lg-12"> Remote Database Management System </h1>
            <div class="text-center col-sm-12 col-md-12 col-lg-12"> You are connected to the Project 3 Database.</div>
            <div class="text-center col-sm-12 col-md-12 col-lg-12"> Please enter sql query or update statement.</div>
            <form action = "/Project3/main" method = "post" style="margin-top: 15px;" class="text-center">
                <div class="form-group row">
                    <div class=" col-sm-12 col-md-12 col-lg-12">
                        <textarea name="textBox" class="form-control" id="textBox" rows="8" cols="50"><%= textBox %></textarea>
                    </div>
                </div>

                <button style="margin-bottom: 15px;" type="submit" class="btn btn-dark">Execute Command</button>
                <button onClick="reset();" style="margin-bottom: 15px;" type="reset" class="btn btn-dark">Clear Form</button>
            </form>
        </row>
    </div>


    <div class="text-center">
        <%-- jsp statement with out sql response--%>
        <%= result %>
    </div>


    </div>

</body>
</html>

Textbox 和 Result 都是 servlet btw 的 java 代码中的变量。任何帮助将不胜感激谢谢!

标签: javahtmljsptomcat

解决方案


编辑:根据您的评论,我假设您有一个带有doGet如下方法的 servlet:

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  String textBox = req.getParameter("textBox");
  ...
  String result = ...
  req.getRequestDispatcher("/your.jsp").forward(req, resp);
}

servlet 中的局部变量和 JSP 页面(编译成 servlet)中的局部变量是不同的!您既不能使用也textBox不能result在 JSP 页面中使用。

开箱即用的 JSP 页面可以访问 8 个局部变量:隐式对象。由于textBox是一个请求参数,您可以通过以下方式获取它:

<%= request.getParameter("textBox") %>

要检索result,您需要将其保存为 servlet 中的请求属性:

req.setAttribute("result", result);

您可以在 JSP 页面中检索它:

<%= request.getAttribute("result") %>

您可以使用 JSP 声明<%! ... >或 JSP scriptlet声明其他变量<%= ... >,例如:

<% String textBox = request.getParameter("textBox"); %>

备注<%= ... %>:近 20 年来不鼓励使用 JSP 表达式(参见这个问题)。您应该改用 EL 表达式:

${param.textBox}
${result}

EL 表达式有一个更大的可解析标识符列表,其中包括所有页面、请求、会话和 servlet 上下文属性。


推荐阅读