Ian Groen
Ian Groen

Reputation: 13

Tomcat Unable to compile class for JSP

So I'm doing a project and it involves writing a servlet in java that accesses an sql database. That servlet is accessed through a front end jsp page, and all this is hosted on tomcat. I've got all my files in the right place, and when I try to open it on Tomcat, I get this...

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: 

So I've got two variables that can't be resolved in my jsp file that can't be resolved. Not sure what this means. Here's the body of my jsp code:

<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 and Result are both variables in the java code for the servlet btw. Any help would be appreciated thanks!

Upvotes: 0

Views: 609

Answers (1)

Piotr P. Karwasz
Piotr P. Karwasz

Reputation: 16185

Edit: Based on your comment I assume you have a servlet with a doGet method like this:

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

The local variables in the servlet and those in the JSP page (which gets compiled to a servlet) are distinct! You can not use neither textBox nor result in your JSP page.

Out-of-the-box the JSP page has access to 8 local variables: the implicit objects. Since textBox is a request parameter you can get it with:

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

To retrieve result you need to save it as request attribute in your servlet:

req.setAttribute("result", result);

and you can retrieve it in the JSP page:

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

You can declare additional variables using JSP declarations <%! ... > or JSP scriptlets <%= ... >, e.g.:

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

Remark: The use of JSP expressions <%= ... %> is discouraged since almost 20 years (cf. this question). You should use EL expressions instead:

${param.textBox}
${result}

EL expressions have a much larger list of resolvable identifiers, which includes all page, request, session and servlet context attributes.

Upvotes: 1

Related Questions