Kavar Rushikesh
Kavar Rushikesh

Reputation: 143

can not resolve to a type in JSP compilation manually deployment in tomcat

I am new to Web Development in Java. I am trying to deploy web application in Apache Tomcat (9.0.17). I have installed Linux OS in which Java OpenJDK is pre-installed. I am not able to compile JSP.

Output of "java -version" in terminal:

openjdk version "1.8.0_202"
OpenJDK Runtime Environment (build 1.8.0_202-b26)
OpenJDK 64-Bit Server VM (build 25.202-b26, mixed mode)

I have put my java package inside TOMCAT_INSTALLATION_DIR/webapps/app/WEB_INF/classes/ .here TOMCAT_INSTALLATION_DIR is a directory where i extracted apache-tomcat-9.0.17.tar.gz file. Inside classes directory, i have java package named abc. Inside that, i have put Test.java

Here is the code in Test.java

package abc;
public class Test{
    public  String f(){
            return ("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
    }
    public Test(){
        System.out.println("I am created");
    }
}

I successfully compiled it and put in same directory where my Test.java file resided(inside abc directory).

I have created web.xml inside WEB-INF folder. content inside web.xml :

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
  <display-name>app</display-name>
  <welcome-file-list>
      <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

I have index.jsp file inside app/ directory. Content of JSP :

<%@ page  import="abc.*"%>
<!DOCTYPE html>
<html>
<head>

<title>Insert title here</title>
</head>
<body>
    <%
    Test cc=new Test();
    cc.f();
    %>
</body>
</html>

I ran apache by ./cataline.sh run command inside bin directory that is inside installation directory. Then i open localhost:8080/app URI. I got an error

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

An error occurred at line: [10] in the jsp file: [/index.jsp]
Test cannot be resolved to a type
7: </head>
8: <body>
9:  <%
10:     Test cc=new Test();
11:     cc.f();
12:     %>
13: </body>


An error occurred at line: [10] in the jsp file: [/index.jsp]
Test cannot be resolved to a type
7: </head>
8: <body>
9:  <%
10:     Test cc=new Test();
11:     cc.f();
12:     %>
13: </body>


Stacktrace:
    org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:103)

Upvotes: 0

Views: 278

Answers (1)

deepak
deepak

Reputation: 131

Thanks for detailed instructions in the question. I setup the web app per those details and found two issues:

  1. It's not WEB_INF but WEB-INF. Please check if the folder name is indeed WEB-INF.
  2. Here is the updated index.jsp so that returned value is displayed on the web page. The code in the JSP file gets executed in tomcat server and any System.out.println line would get logged in server logs (i.e. TOMCAT_INSTALLATION_DIR/logs/catalina.out ). Similarly, the value returned from function call is not going to get displayed unless you use <%= variable %>.
<%@ page  import="abc.*"%>
<!DOCTYPE html>
<html>
<head>

<title>Insert title here</title>
</head>
<body>
    <%
    Test cc=new Test();%>
    <%= cc.f() %>
</body>
</html>

In case these changes don't work, could you please paste the screenshot of the whole folder structure of all files under the Tomcat installation directory ? I am asking this as everything else is correct and it worked for me.

My Local Setup Folder Structure My Local WebApp Index Page

Upvotes: 1

Related Questions