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.
Related
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 am trying to include a JSP page using directive in another JSP.
My code is:
<%#include file="${pageContext.request.contextPage}/Pages/Loader/
load-resources.jsp" %>
As you can see here clearly that my url should be like
localhost://port/WebApp1/Pages/Loader/load-resources.jsp
But on execution i am receiving this error which says this URL does not exist. But i passed url in reference from contextPage.
Caused by: org.apache.jasper.JasperException: PWC6117: File
"/Pages/Test/Take/${pageContext.request.contextPage}/Pages/Loader/
load-resources.jsp" not found
How to resolve this issue?
Ok, if you want to include Pages/Loader/load-resources.jsp, simply use :
<%#include file="/Pages/Loader/load-resources.jsp" %>
You do not include an URL, but a file. The root of the hierarchy is simply the root of your web application.
By the way, are you aware of the differences between the directive <%#include ... %> and the tag <jsp:include .../> :
with the directive, you include the source file, meaning all page context variables will be shared
with <jsp:include .../>, you include at output level, meaning each page will have its own page context
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.
I creat a web application using eclipse and tomcat7 I had the following code in the html file and the java servlet class
in the html file:
<form action="UserAccessServlet" method = "get">
in the servlet class I had
#WebServlet ("/UserAccessServlet")
then I just made some small changes (new println statements) but it shows no effect I changed the server name with the following peice of code
html file: <form action="SQA_Servlet" method = "get">
java class: #WebServlet ("/SQA_Servlet")
but it seems that no reload take place and I got the following error:
HTTP Status 404 - /SQA_Learning/SQA_Servlet
--------------------------------------------------------------------------------
type Status report
message /SQA_Learning/SQA_Servlet
description The requested resource (/SQA_Learning/SQA_Servlet) is not available.
I tried clean the module, refresh, close the reopen the project with the same result
I replaced #WebServlet ("/SQA_Servlet") with #WebServlet(urlPatterns={"/SQA_Servlet"})
and still have no effect.. any suggestion.
The WebServlet name attribute cannot start with a /. Rather do,
#WebServlet("UserAccessServlet")
or leave it blank (if you want the WebServlet to use the name of your Servlet class name. Example:
#WebServlet
public class UserAccessServlet extends HttpServlet {
//Do stuff
}
I would recommend declaring your WebServlet annotations fully like in this example.
I'm not sure when and in what for conditions you are receiving this error. But if you are deploying to tomcat, the following might occur:
Assuming your webapp is called "my.webapp" resulting in my.webapp.war
assuming you have a Servlet "servlet1" which performs action1 => #WebServlet(urlPatterns = "/action1") (note the slash in front of action1)
Assuming you are calling this action with a html form:
<form action="/action1" method="GET"> this might not work because of the slash in front of action1
When it's there tomcat will redirect to localhost:8080/action1?..
while it should redirect to localhost:8080/my.project/action1?..
Solution alter the html so the form looks like:
<form action="action1" method="GET">, don't change the #WebServlet(urlPatterns = "/action1")
Hope this helps someone!
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!