Its been a while since I had to do some Java/JSP...
I have a java class in WEB-INF/classes/MyClass.java
The build in Netbeans is successful and I can see MyClass.class in the classes folder.
In my jsp page, I've got
<%# page import="MyClass" %>
Tomcat says that the import cannot be resolved...
I tried to put MyClass in a package(WEB-INF/com/MyClass) and then import the package into my Jsp file. The import doesnt throw an error anymore then, but I cannot instanciate a MyClass object, says the type is unresolved...
What am I doing wrong here..?
All help appreciated! :)
WEB-INF/classes/MyClass.java
makes me guess that you're using the default package which is'nt a good practice at all. Try assigning your class to a package and do the import according to that.
Do something like:
package myPackage;
class myClass
{
...
}
And afterwards:
<%# page import="myPackage.myClass" %>
.class file must be placed inside the classes folder under WEB-INF.
So, the location of MyClass.class must be WEB-INF/classes/com/ (In case com is a package).
<%
// Instantiate a MyClass
com.MyClass obj=new com.MyClass();
%>
OR
<%# page import="com.MyClass" %>
<%
MyClass obj=new MyClass();
%>
What package is MyClass in? If the default package then you can put the class file in
WEB-INF/classes
if it's in a package, then use the package directory hierarchy under classes
OMG, I found my mistake...
Netbeans wasn't copying the lib files to the right folder, my jsp page was being updated, so it looked like all the files were copied, but actually MyClass.class wasnt in the folder...
Thanks for your help!
Related
My aim is to import my class file in my index.jsp and reading over the internet I learned I'm supposed to use JavaBeans.
So here is my class file placed in WEB-INF/classes/jBean/Bucket.class -
package jBean;
import java.sql.* ;
import java.math.* ;
public class Bucket implements java.io.Serializable{
public static void main(String[] args){
Bucket obj = new Bucket();
obj.DBConn();
}
public static void DBConn(){
try{
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/studentfeedback","root","");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from student");
while(rs.next())
System.out.println(rs.getInt("Roll_No")+" "+rs.getString("Name")+" "+rs.getString("Pass"));
con.close();
}
catch(Exception e){
System.out.println(e);
}
}
And here is my index.jsp where I want to import the class file -
<%# page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<jsp:useBean id="jBean" class="jBean.Bucket" scope="session" />
</body>
</html>
Doing this I get this error message on executing index.jsp file
java.lang.ClassNotFoundException: org.apache.jsp.index_jsp
So, my query is how shall I import the class file, Bucket.java?
And is there any other way to import the file to index.jsp other than using javBeans?
Please help me with this as I can't find what I'm looking for. Maybe cause I might not be asking the right thing.
All I want to do is import the class file Bucket.java to the index.jsp
May God help the helping soul.
Did you observed your project folder structure.
The classes referenced by the jsp must be in the classpath.
And the classpath includes WEB-INF/classes.
The location of the jsp is of no importance.
question regarding jsp page import and <jsp:useBean id
I don't understand why i cannot include a class into my jsp file.
So, the file i want to use is:
public_html/admin/dashboard.jsp
i have this code:
<%#page import="frontEnd.General"%>
<%
String getBasePath = General.getBasePath(request);
%>
the class i want to include is located:
public_html/WEB-INF/classes/frontEnd/General.class
but when i do this i recieve the error:
HTTP Status 500 - Unable to compile class for JSP
Can you help me with this?
Thanks!
another relevant example:
i have a page, page.jsp in route that has this code on the top:
<%#page import="frontEnd.General"%>
<%
String getBasePath = General.getBasePath(request);
%>
if i access like this:
www.mydomain.com/page.jsp - works, the class is found
if i move the file into a directory (let's say "admin") and try to access the file like:
www.mydomain.com/admin/page.jsp - i recieve the error that the class is not found
Check the package structure of your server. Most probably it doesnt find the class in your structure hence it throw compilation error.
Try importing other classes and check whether problem still comes.
I am having trouble referencing a static inner class in a JSP tag file. I am using Glassfish Jersey and Jetty 6.1.X. I am using JSP 2.0 and tagfiles, and I don't have any TagHandler classes or any .tld files. My web.xml also doesn't contain anything specific about JSPs or tag files.
I have broken down the problem into the smallest reproducible:
This is the structure of the class I am using:
package test;
public class OuterClass {
public static class InnerClass {
}
}
This is the complete contents of my .tag file:
<%# attribute name="inner" required="true" type="test.OuterClass.InnerClass" %>
<h1>${inner}</h1>
(The tag file shows an error in IntelliJ):
I use this tag in my parent jsp like so:
<%#taglib tagdir="/WEB-INF/tags" prefix="test" %>
<test:test inner="${inner}"/>
The exception that I get when attempting to use it this way is:
org.apache.jasper.JasperException: /WEB-INF/jsp/test.jsp(54,4)
Unknown attribute type (test.OuterClass.InnerClass) for attribute inner
If I change the type to use the binary notation (OuterClass$InnerClass) I get this error instead:
The nested type test.OuterClass$InnerClass cannot be referenced using its binary name
I have searched Google for this and found others with the same problem, but all of these seem to have been resolved by a fix to Jasper years ago.
https://issues.apache.org/bugzilla/show_bug.cgi?id=41824
https://issues.apache.org/bugzilla/show_bug.cgi?id=35351
I have bypassed the problem by splitting the class into many top level classes instead, but the nested classes in my use case necessarily belong to the top level class, and shouldn't be used outside of this so it feels wrong to change the design due to the limitation I am finding here.
If there is a way to properly used static nested classes in a tag attribute, then that would be the ideal solution.
I'm supporting an older JSP system that may be using non-standard way of including classes and functions. Instead of writing classes files and compiling them into jar files, the classes are written directly in "child" JSPs and then included in a "parent" JSP.
The problem I'm having is that in JDeveloper, even though the IDE recognize the files in the include statement, I get error messages that MyClass and ChildFunction() are not defined. When I drop the 2 JSPs on the server, the page loads and runs with the expected results. As a result, when I work on the real code, I have a 1000+ line JSP with over a hundred syntax errors, so I can't tell if my code is even syntactically correct until I drop it on a server. I'm hoping its something simple (/stupid) like a classloader error or a bad configuration, but I haven't been able to fix it. Any help is greatly appreciated.
I've included two stub files below:
MyClass.jsp FILE:
<%# page import="java.lang.*,java.net.*,java.text.*,javax.servlet.http.*" %>
<%!
class MyClass() {
public MyClass() { super(); }
private int it=0;
public void setIt (int it) {this.it = it; }
public int getIt () { return it; }
}
public String ChildFunction(){
return " ChildFunction ";
}
%>
Parent.jsp FILE:
<%# page contentType="text/html;charset=WINDOWS-1252"%>
<%# page language="java" session="true" import="java.lang.*,java.net.*,java.text.*,javax.servlet.http.*" %>
<HTML>
<HEAD><TITLE>Test page</TITLE></HEAD>
<BODY>
<%# include file="./MyClass.jsp" %>
<br>Result of calling Child Function:<% out.print(ChildFunction()); %>
<br>Result of declaring MyClass:
<%
MyClass mc = new MyClass();
out.print(mc.getIt());
%>
</BODY>
</HTML>
Before someone suggest that I put the code in class files and use EAR/WAR files to deploy, that's not an option. This is an existing legacy system that I need to be able to support.
Don't put functions or subclasses in JSPs expecting to use them in others. (That is what they are after all, subclasses under that particular JSP.) It is very bad design. Create classes in actual .java files, compile them, and then call them from the JSPs. Even that is not particularly recommended (see How to avoid Java code in JSP files), but its better, a lot better.
If you have access to the filesystem, you don't need to put classes in a EAR/WAR. Just put them on the filesystem under webapps/{theApp}/WEB-INF/classes/...
You can even dispense with an IDE altogether, and just save all your .java files under webapps/{theApp}/WEB-INF/classes/... open a console, cd to that directory, and javac them. There is certainly no need to put class files in jars.
You do, of course, have to restart the application afterwards.
My class's code:
package overviewPack;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ButtonScreen extends JApplet implements ActionListener{
JButton middle = new JButton();
Container screen = getContentPane();
public void init(){
setVisible(true);
middle.addActionListener(this);
screen.add(middle);
}
public void actionPerformed(ActionEvent event) {
if (event.getSource() == middle){
System.out.println("hey");
}
}
}
When I attempt to run it using html, I recieve a noclassdefFound error, with the stacktrace as ButtonScreen(Wrong name: overviewPack ButtonScreen)
Here is my html code: (I use brackets so that the code will turn up in chat as the code and not the finished product).
<HEAD>
<TITLE>
A Simple Program </TITLE>
</HEAD>
<BODY>
Here is the output of my program:
<APPLET CODE="overviewPack.ButtonScreen.class" codebase = "bin" WIDTH=150 HEIGHT=25>
</APPLET>
</BODY>
</HTML>
I have tried many different formats for the html, and looked at many other people's similar, sometimes exactly the same errors, but none of the solutions proposed to other people have worked. I have also looked around on the rest of the net for a solution, but I have found none.
This error happens with all my applets, even this extremely simple applet I did above.
The html file is in the same folder as the class
The HTML file is in the same folder as the class
That is no good. You need to understand how the parameters in the applet element work.
<APPLET CODE="overviewPack.ButtonScreen.class" codebase="bin" WIDTH=150 HEIGHT=25>
Let us say that the HTML is located at: our.com/applets/applet1.html.
codebase = "bin" would mean the classpath starts with our.com/applets/bin/.
overviewPack.ButtonScreen.class would therefore need to be found at:
our.com/applets/bin/overviewPack/ButtonScreen.class
Note that the package overviewPack has become an inherent part of the correct path to the class file. That is where the 'wrong name' is originating from. The JRE seems to be searching the directory of the HTML, locating the class in the same directory, then loading it to discover it is in the wrong path.
Code Attribute
<APPLET CODE="overviewPack.ButtonScreen.class" codebase="bin" WIDTH=150 HEIGHT=25>
Note the required value is the Fully Qualified Name of the class file. That consists of the package(s) name, followed by the class name, each separated by a dot. E.G.
overviewPack.ButtonScreen
As opposed to
overviewPack.ButtonScreen.class // combination of FQN with file type
or
overviewPack/ButtonScreen.class // relative file path on server
So the opening APPLET element should best be:
<APPLET CODE="overviewPack.ButtonScreen" codebase="bin" WIDTH=150 HEIGHT=25>
Sometimes there is a problem with the .class file extension at the end of the code= attribute. Some doc I've seen says that the code= attribute has the classname in which case having the .class at the end is wrong. The classname is: overviewPack.ButtonScreen and the filename is:
ButtonScreen.class