I'm having the exact same problem stated in run applet in web application. It throws a ClassNotFoundException for my applet. I tried the solution from there but still no luck.
Here is my code for embedding Applet in html:
<body>
<applet codebase="/DaaS/applet" archive="/DaaS/applet/firstApplet.jar" code="FirstApplet.class" width="300" height ="300"> </applet>
I've a folder DaaS/applet which contains firstApplet.jar and my index.html is in Daas/Webcontent.
The URL in the codebase (and archive) attribute is relative to the current request URL (the one as you see in browser address bar), not to the disk file system in the server side. Imagine that you've the index.html page in some subfolder like so:
http://localhost:8080/somecontext/index.html
The URL as you have in the codebase (and archive) attribute starts with a leading slash / which makes it relative to the domain root instead of the current folder. So the webbrowser will look for the archive and the JAR in the following URL
http://localhost:8080/DaaS/applet/firstApplet.jar
This may not be correct per se. You need to make sure that the codebase (and archive) URL points to the right URL relative to the current request URL. Based on the information given so far, the /DaaS folder is basically in the same parent as index.html, so this should do:
<applet codebase="DaaS/applet" archive="firstApplet.jar" ... />
(note that I simplified the archive attribute, it will be resolved relative to codebase anyway)
This way the browser will load the JAR from:
http://localhost:8080/somecontext/DaaS/applet/firstApplet.jar
Related
How can I include my java applet class into my web page using JSP on a netbeans web application project.
I don't want it as a jar. When I use a jar it works correctly.
I have a package named "com.example" which is the applet named Scan.java
Also have a web page named main.jsp which I added the code below.
I am using this code to include the class but
<APPLET CODE="Scan.class" WIDTH=150 HEIGHT=250>
On the browser the applet doens't open and it says "ClassNotFoundException"
How to fix that?
The class "Scan" should be on the same directory than your HTML/JSP page, or you could specify the relative path (from the JSP) to reach your Scan.class with the attribute CODEBASE.
<APPLET CODE="Scan.class" CODEBASE="../classes/com/example" WIDTH=150 HEIGHT=250>
I'm working on a web project using java/ jsp/ servlets/ html/ css in eclipse tomcat, where everything is in the WebContent folder.
In my jsp files... When I try to include other jsp files (using a link like "/fileName.jsp" in jsp include directive) I can do that successfully.
But When I try to include image files (using a link like "/fileName.jpg" in the <img src=""> tag) nothing happens.
Nothing happens because instead of looking in the WebContent folder for image file it looks in the tomcat home directory, i.e.
Instead of looking at "http ://localhost:port/projectName/..." it looks at "http: //localhost:port/..."
Why does it look at the wrong location only with <img src=""> tags but not in <%# /> tag.
A workaround for this is that I start giving absolute paths "/projectName/..." However doing this means I'm hardcoding project name everywhere. This is what I do not want.
Don't include binary content in an ascii output. Why not just use the img tag? If you need to do something to produce a jpeg, I would use a Servlet.
Because the jsp-Links in the website are getting processed and the image links not. Either change the image path or develop an filter that changes the images'links.
Yes Templar, that could have been a way to solve my problem.
However, I simply changed the Context Root of my project from "Project Name" to "/" in Eclipse. This solved my problem.
I'm returning a Viewablein my java while which returns a jspwhich is located in the WebContent directory. In that directory there are my cssand jsdirectories where my files are located. My jsp's reference these files using relative paths. e.g js/javascript.js.
However, if I load any of these .jsp's via a return new Viewable("myPage.jsp"); in my .javafiles the paths seem to mess up as when the url is loaded the browser shows the url of the java file and all relative paths fail. How can I correct this issue?
The path you are using should be relative to the resources that return these, rather than to the JSP's themselves. Are these files accessible when you enter their URL's to the browser directly? If yes, then you just need to make sure you put the right path to the JSP files - e.g. use the absolute path instead of the relative one - like this: <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/css/style.css" />
I use firefox version > 3.5 (3.5.,3.6.,4.*) and I try to specify archive and codebase property correctly but it doesn't work. My main class for applet is situated in the archive and some necessary classes that are loaded during runtime are situated in the codebase. If I specify only the archive then the applet is loaded but the classes from codebase are missing. If I specify the archive and the codebase then the applet can't be loaded. It looks like applet try to load main class from codebase folder and doesn't look into the archive file.
<html>
<body>
<applet width=600 height=300 code="MyClass.class"
type="application/x-java-applet;jpi-version=6"
archive="http://myurl.com/archive/myjar.jar"
codebase="http://myurl.com/classes">
no applet
</applet>
</body>
</html>
Main class is situated in http://myurl.com/archive/myjar.jar and runtime classes are situated in http://myurl.com/classes.
Attribute codebase specifies the base URL of the applet - the directory that contains the applet's code. It is used while searching jar files in archive attribute, in such a way that all jars in archive attribute are searched relative to codebase.
So. When you use archive="http://myurl.com/archive/myjar.jar" and codebase="http://myurl.com/classes" together it means: find "http://myurl.com/archive/myjar.jar" in "http://myurl.com/classes" folder.
I.e. the full search path is "http://myurl.com/classes/http://myurl.com/archive/myjar.jar". And of course it can't be found!
Also, classes, whose jar-files aren't specified in the archive attribute, can't be found without codebase attribute. I.e. if there is no codebase then there is no way to find your classes in "http://myurl.com/classes" folder.
You can find more details in the Deploying With the Applet Tag tutorial.
I suggest the following solution:
Place myjar.jar in the http://myurl.com/classes folder;
Assuming your MyClass.class is in default package, and in the "http://myurl.com/archive/myjar.jar", the following code should work:
<html>
<body>
<applet width=600 height=300 code="MyClass"
type="application/x-java-applet;jpi-version=6"
archive="myjar.jar"
codebase="http://myurl.com/classes">
no applet
</applet>
</body>
</html>
What is the difference between relative and absolute url in servlet container. for example if there is an jsp called forum.jsp under webinf folder. when i want dispatch the current request to the jsp from the current jsp file which is under the same webinf folder, is the following correct way
/forum.jsp
relative url means relative to the web-inf folder or to the jsp location.
Absolute URL is : http://stackoverflow.com/questions/3591899/relative-url-and-absolute-url-difference
and a relative URL is: /questions/3591899/relative-url-and-absolute-url-difference
Also, a relative URL can be just: ../questions/3591899/relative-url-and-absolute-url-difference depending where is the linking page located...
Or ./3591899/relative-url-and-absolute-url-difference if the linking page is located on the questions folder
I will suggest to always use Relative URL... and it goes hard, keep trying to use them...
One question, why your JSPs are in the WEB-INF/ folder?
You don't have access to JSP under the WEB-INF folder, if you try to access it the server will throw a 404 error. J2EE only looks for classes and libraries under this folder.
An absolute URL is an URL which includes the scheme (e.g. http:). A relative URL does not include the scheme and is thus dependent on the current context.
How to interpret a relative URL is a bit more complicated. It depends entirely on the context where the URL is been used. E.g. in a webbrowser, or in a servlet, or even in the local disk file system (java.io.File and so on).
When talking in the servlet context, when a relative URL starts with /, it will be relative to the context root (i.e. the root of the webcontent folder, there where the /WEB-INF folder is and where all JSP files are been placed).
So when you want to forward the request to /WEB-INF/forums.jsp, then you just specify that so:
request.getRequestDispatcher("/WEB-INF/forums.jsp").forward(request, response);
But when a relative URL doesn't start with /, then it will be relative to the current request URL. So when the request URL is for example http://example.com/context/servlets/servletname and you use the relative URL forums.jsp, then the following
request.getRequestDispatcher("forums.jsp").forward(request, response);
will actually point to http://example.com/context/servlets/forums.jsp