error while importing .java file in jsp page - java

I have a .java file containing a public class. It is located under the 'web pages' folder (not under web-inf folder).
On my jsp page I have imported it as: <%# page import="packagename.javafilename;" %>
When I run jsp file, I get error as:
Unable to compile class for jsp.
Only a type can be imported. Packagename.filename resolves to a package

As mentioned in the comments there seems to be a missing % towards the end. Here is the way to import classes in jsp
// To import one class
<%# page import="com.xyz.MyClass" %>
OR
// To import multiple classes from the com.xyz package
<%# page import="com.xyz.*" %>

From your question things are not very clear.
First of all jsp cannot access .java but a .class file i.e. you have to keep your code compiled.
Secondly the class should be in the WEB-INF/classes folder or a jar in WEB-INF/lib folder.

Use this syntax:
<%# page import="package.filename" %>
In the last import, you might need to add a semicolon after the end of the package.filename.
So, if you have like 3 imports, then in the 3rd import line make sure to put a semicolon as:
<%# page import="package.filename;" %>
One more thing, make sure that the class files are in WEB-INF/classes folder.

Related

Problems with importing org.apache.commons in JSP

I've recently tried to use part of a code of a JSP file, found online, which starts by importing:
<%# page import = "org.apache.commons.fileupload.*" %>
<%# page import = "org.apache.commons.fileupload.disk.*" %>
<%# page import = "org.apache.commons.fileupload.servlet.*" %>
<%# page import = "org.apache.commons.io.output.*" %>
but Eclipse warns me with the error: " The import org.apache cannot be resolved. "
I tried to copy paste in the path
"C:\Users\MyPc\Downloads\apache-tomcat-7.0.96-windows-x64\apache-tomcat-7.0.96\webapps\SL2\WEB-INF\lib
the folder "commons-fileupload-1.4" once extracted, downloaded by the link: http://commons.apache.org/proper/commons-fileupload/download_fileupload.cgi
and yet the error keep remaining. What do I mistake?
You should be putting the jar files from that zip, and only the jar files, into the project's WEB-INF/lib folder. The server has no use for the rest, and expects nothing more complicated in the layout there.

Issue with uri of custom tag library

I have seen a weird problem while using custom tag libraries.
In one of my jsp pages the relative uri path of the custom tld file is wrong.
Taglib is included in jsp page like below
<%# taglib prefix="aaa" uri="WEB-INF/bbb.tld" %>
Location of bbb.tld is : Stores\WebContent\WEB-INF\bbb.tld
This taglib is included in a parent jsp file, and not there jsp file is imported/included from parent.
My application has several other jsp files, where the same taglib is used with the correct path.
The same code is deployed in multiple test environments. In a few environments the code is throwing an error of "Failed to find resource", which is expected. But the taglib is working fine in most of the environments, instead of wrong path.
Why is there discrepancy in the behavior between different server environments? Does the server automatically look for all tld files if the uri can't be resolved?
I would recommend adding a trailing slash ('/') before WEB-INF. Could you try the following?
<%# taglib prefix="aaa" uri="/WEB-INF/bbb.tld" %>
Hopefully, this will give you consistent results across all you server instances. Also, having your TLD in Stores\WebContent\WEB-INF\bbb.tld seems to be related to be using an IDE (e.g. Eclipse) to develop your application. Once you export your application as an EAR or WAR the path will look more like Stores.war/WEB-INF/bbb.tld

NoClassDefFoundError in JSP in spite of class placed in /lib

My JSP instantiates a MovieDetails class. I am currently working using scripting to ensure everything is working fine, before I move to tags.
I am using Tomcat 8.0. Directory structure for MovieDetails.class: com/library/model/beans. A copy of the directory structure with the MovieDetails.class file is also placed under WEB-INF/lib (have tried putting a .jar for the file too)
In the JSP I have:
<%# page import="com.library.model.beans.*" %>
And later:
<%
MovieDetails movDet = (MovieDetails)request.getAttribute("MovieDetailsBean");
....
....
%>
I am getting:
java.lang.NoClassDefFoundError: com/library/model/beans/MovieDetails
Can anyone please tell me why the JSP can't find the class in spite of the class being in the /lib directory?
NoClassDefFoundError and ClassNotFoundException is two different exception

directory folder name to put user defined packages in tomcat 7

I am new to java and jsp. I have just created a small package and that package contains a simple java class. Let me show what i really want to do. I am not working with servlets so please tell me about this simple example so that i can go ahead with my work. Below is my code or a package class and a jsp page.
my java package
package mypack; //this is my package
public class Abc{
public Abc(){
}
public void message(){
System.out.println("My first java package");
}
}
index.jsp this is my jsp page where i need to use this package
<%# page import="java.sql.*"%>
<%# page import="mypack.*"%>
<%
Abc a = new Abc();
a.messsage();
%>
I was using JDK1.5 and tomcat 3. But i want to update my system. Now i have JDK1.7.0_11 and tomcat 7. I know the path of tomcat 3 to put my packages but i don't know where to put these packages in tomcat 7.
tomcat 3 directory path to place packages:
D:\Java\tomcat\examples\WEB-INF\classes //i put my package at this path in tomcat3
tomcat 7 directory path to place packages:
D:\web\Tomcat 7.0\webapps\ROOT\WEB-INF //trying to put my package in here but no use.
I could not find the classes folder under the direcoty WEB-INF in tomcat 7. I made a folder myself named classes inside of WEB-INF but it does not work. Even i have deleted that my "classes folder" and put my package in WEB-INF, but it does not work. Please tell me the path where i can put my java package in tomcat server 7.
I have placed my jsp page in here:
D:\web\Tomcat 7.0\webapps\ROOT\a //folder "a" contains my jsp file. index.jsp and
its working
Problem is, jsp page could not find the package. Please help me out with this.
packages are always located in WEB-INF/classes, not directly in WEB-INF.
Try to put them here thus:
D:\web\Tomcat 7.0\webapps\ROOT\WEB-INF\classes

Folder access in Java EE

I need to access a page allCommonPageLink.jsp from another page named add_edit.jsp
I have done <%# include file="/plan/common/allCommonPageLink.jsp" %>
Error /WEB-INF/ViewPages/plan/add_edit.jsp (line: 8, column: 42) File "/plan/common/allCommonPageLink.jsp" not found
NEW QUESTION::
how can I access an image form images folder from tray.jsp and add_edit.jsp
you should use
<%# include file="../../common/allCommonPageLink.jsp" %>
Using ../ takes you back to the previous directory from where you are now.
If your page is web-inf/viewpages/plan/page.html that means that your working dir is plan. Invoking ../ takes you to view pages. Invoking another ../ takes you to web-inf. And from web-inf you have access to the file by accessing common/anotherpage.jsp
You can read https://www.owasp.org/index.php/Path_Traversal for more info
make it
<%# include file="WEB-INF/common/allCommonPageLink.jsp" %>
the file allCommonPageLink.jsp is not under the plan folder, fix he path and try again, hope this would fix your issue.
This will work
<%# include file="common/allCommonPageLink.jsp" %>
OR
You have to go back up to two folders plan and viewPages in order to access this folder in that file so this will work for you
<%# include file="../../common/allCommonPageLink.jsp" %>
You can use
<%# include file="../../common/allCommonPageLink.jsp" %>
When you add ../ it it will refer its parent folder or directory.

Categories

Resources