NoClassDefFoundError in JSP in spite of class placed in /lib - java

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

Related

Intellij IDE JSP path resolving [duplicate]

This question already has an answer here:
How specify path to .JSP file for request.getRequestDispatcher()?
(1 answer)
Closed 6 years ago.
I have a servelet called RegisterUser mapped as /register this servelet includes a jsp file called register.jsp . here is the structure of the project :
inside the servelet this code cannot find the jsp file :
RequestDispatcher view = request.getRequestDispatcher(registerView);
and the content of registerView is :
private static final String registerView = "../web/WEB-INF/views/Register.jsp";
It worked fine on eclipse but when I switched to Intellij it shows that the file cannot be found .
Liferay has a somewhat strange layout for it's web-inf and html folders. In Eclipse I never really code path completion to work well in JSPs because it always looked at the wrong spot for files. In IntelliJ I just setup different facets that will help the IDE help me. Here is an example. I have a facet called WEB-INF. In Liferay, when you reference a file in WEB-INF (tagfiles are a GREAT example here) you do it like this:
<%# taglib prefix="showMore" tagdir="/WEB-INF/tags/wj/showmore" %>
The problem there is that by default, the IDE wants to autocomplete like this:
<%# taglib prefix="showMore" tagdir="/docroot/WEB-INF/tags/wj/showmore" %>
I simply made a web facet and said that when I type /WEB-INF I really want you to look at /docroot/WEB-INF. This small change saves me so much headache throughout the day. I also did the same thing with /html instead of /docroot/html.
You know how in eclipse when you go to an init.jsp file in the ext environment it always freaks about the init.jsp includes from portal because it can't find it? I once again used a web facet (called portal-web) and I linked / to /liferay/builds/4.3.x/portal-web/docroot. Now when I say:
<%# include file="/
right there it starts autocompleting. It lists out everything in both portal-web and ext-web for me, because I have access to both inside Liferay! Doing this then:
<%# include file="/html/common/init.jsp" %>
it's a problem, I can even click init.jsp and press ctrl (cmd) + b and it will jump me right into the init file in portal.
I tend to run IntelliJ with one "Module" and two "Content Roots". One content root is portal, the other is ext. The plus here is that the debugger knows about the portal source files, and I can easily jump between jsp files as well.
Resource Link:
Why I use IntelliJ instead of Eclipse?

JSP file Include in from Jar file

I have two different project In first Project have all js,ftl java files and I included jsp file also in:
Webcontent/test.jsp
META-INF/resources/test.jsp
META-INF/test.jsp
When I exported this jar file in another project, I'm unable to call this jsp file.
Tried Following way:
<jsp:include page="/test.jsp"></jsp:include>
<jsp:include page="/test.jar!/resources/test.jsp"></jsp>
but it's still showing error while accessing this path in jsp.

error while importing .java file in jsp page

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.

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

Deploying a jar in Jboss

I have some .jspf files that are fragments which I include in a new .jsp file. The reason they are fragments is that they are reused across multiple jsps with some additional components.
My issue now is that I want to use these .jsps in 2 different .war files.
So I created a new .jar file which includes these jspf, now I am trying to deploy this jar so that I am then import it in my new jsp which is inside 1 of the 2 wars.
I am not able to deploy the jar succesfully in Jboss 4.2. I am using Eclipse ide.
Any ideas on this? If there is an alternate approach I would appreciate any ideas.
Thanks for this suggestion, I have followed this idea and deployed my war1 which contains my .jspf files.
So in my war2 on a jsp I do:
<c:import context="/sharedComponents" url="/easyPayNamePaymentOption.jspf" var="easyPayName"/>
<%# include file="easyPayNamePaymentOption.jspf" %>
However I am not able to render this page with the included fragment, I am not sure how to address the imported jspf. I ahve tried several different ways like:
<%# include file="/sharedComponents/easyPayNamePaymentOption.jspf" %>
and also using var name like:
<%# include file="#{easyPayName}" %>
However it keeps looking inside the current war. How can I tell it to include the newly imported fragment and display it?
Thanks in advance.
I can see what you're trying to do, but this isn't going to work with a JAR file. All JSP files (including JSPF) have to inside a WAR, not a JAR.
The simplest solution is to put copies of the JSPF files into each WAR that needs to use them. Assuming that you don't want to do this, then there is an alternative, called a cross-context WAR.
By default, JBoss allows its webapps to request resources from each other. For example, say webapp1 (context path /app1) wants to import JSPF /my.jspf from webapp2 (context path /app2). You can use JSTL to do this, from inside webapp1:
<c:import context="/app2" url="/my.jspf"/>
So if you were to create a "shared" WAR file containing your JSPF files, and deployed this to JBoss, then your other webapps could use the above technique to include the contents of the JSPFs into their own JSPs.
edit: I've read your updated question, and I don't understand why you added var="easyPayName" to the <c:import> tag. All that's doing is importing the contents of easyPayNamePaymentOption.jspf and storing it in a variable called easyPayName, which seems completely unnecessary.
I think perhaps you're associating <c:import> with a java import? If so, don't - they're completely different. <c:import> should really have been called <c:include>, because that's what it does.
Just keep it simple, remove the attribute, and just have
<c:import context="/sharedComponents" url="/easyPayNamePaymentOption.jspf"/>
That's all you need to do - it will include the content of easyPayNamePaymentOption.jspf directly in the JSP.

Categories

Resources