Import a class into a JSP file

I wrote some code as a Java servlet, and now I'm trying to convert it to JSP. I wrote the class in a separate file that I used, and I cannot figure out how to get the JSP file to recognize the class. I guess this has something to do with import. I gave the class the package name ( package mypackagename; ), and I tried using <%@ page import="mypackagename"%> , but I get an error:

Import of "mypackagename" cannot be allowed.

+6
java import class jsp servlets
source share
2 answers

Just import it in the same way as in a real Java class. That is, import mypackagename.MyClassName or import mypackagename.* And therefore not import mypackagename with just the package name.

 <%@ page import="mypackagename.MyClassName" %> 

However, you should not write Java source code in a JSP file. Scripting is considered bad. This code belongs to the real Java class. It was perfectly located in the Servlet class. What is this, the problem by which you think this is the β€œright” solution, move it all to the side of your vision and clutter up the template text with raw Java code? Develop about this in a new question, then we can offer the right solutions. Perhaps you did not know about the existence and authority of taglib, such as JSTL ?

+9
source share

Make sure that your class is located in the WEB-INF / classes directory of the web application and modifies package import by package. *

0
source share

All Articles