importing class to JSP - java

I have a text.class file that is in the same directory in my .jsp file, how can I include it in my jsp file? usually all of the classes should be in the WEB-INF,however I can't put it there.. Usually what I do is:
<%#Test.test" %>
where Test is a folder in the WEB-INF, so how can I do this now?

<%# page import="Test.test" %>
Provided that Test.test is in your classpath .The better place is to put it is:
WEB-INF/classes/Test/test

Not really an answer, but a warning you should check.
Putting your class files at your JSP folder can lead to security concerns.
The servlet container allows HTTP access for everything under the root web application dir (or inside the war file) but the content of the WEB-INF and META-INF folders. These folders are protected by default.
If you put a class at a different location, somebody could access an download it just writing the URL at his browser nav bar:
http://host:port/appContext/Test/test.class
I don't know if your app handles sensitive data, or your class contains code accessing main components of your application, which could be exposed if someone downloads and decompile your code: it is kind of a serious security risk.
Rethink your app structure, an keep your classes under the WEB-INF/classes dir. Or at least, configure your container or your web app to forbid access to *.class resources via HTTP requests.

Related

java applet ClassNotFoundException with codebase

I'm starting to work on an applet that will replace an existing one. Having never developed an applet before, I thought I'd get going with the popular HelloWorld example. I am able to run it a couple different ways: in the appletviewer, and also in a browser if I put the JAR file containing the HelloWorld class in the same directory as the HTML (i.e. http://localhost:8080/myApp). I also got it to work when I put the JAR in a directory called HelloWorld just below the myApp directory and specified the codebase parameter in the <applet> tag as HelloWorld. But when I try to specify WEB-INF directories such as classes or lib, I get a ClassNotFoundException. What am I doing wrong?
But when I try to specify WEB-INF directories such as classes or lib, I get a ClassNotFoundException. What am I doing wrong?
Those directories are only meant for classes/jars that are used in JSP and servlets (i.e. the stuff the server needs). The resources inside them are available to site visitors. In this sense 'visitor' means a User Agent (i.e. a browser) or a client side plug-in (such as Flash or the JRE).
You can confirm this for yourself by pasting the full URL to the Jar in the web browser address bar and hit 'enter' to browser to it. The server will give a message back to the effect 'forbidden'.
See also the WEB-INF info. page which expands:
WEB-INF is the name of a folder found in Java web applications. It is used to store deployment information such as the web.xml, required library files and compiled class files. It is normally not accessible from web. Any files which you want to put on war but do not want to make to public then web-inf is the place where you can keep those files.

Eclipse - How do I publish non-Java resources without a web.xml file?

My WebApp uses Java annotations to map the URL to the servlet (#WebServlet) and I don't have a web.xml file. I want to foward the request to another HTML file and I did it like so:
request.getRequestDispatcher("/WEB-INF/test/testpage.html").forward(request, response);
It works, but I can't get additional resources (javascript) from the server.
<script type="text/javascript" src="testsuite.js"></script>
testsuite.js is located in /WEB-INF/test/testsuite.js, the same folder as testpage.html. When requesting testsuite.js I get 404. How can I configure Tomcat to serve all the resources in my work tree without a web.xml? Worst case I will simply embed JS.
Additionally I let eclipse install the web server on my local machine so I have no idea how they are deployed.
You have to move resources outside of your WEB-INF folder. It is a special folder for compiled Java code, jars, configuration files etc.
The WEB-INF folder is restricted in application servers and servlet containers for security reasons, so for example a request for /appname/WEB-INF/web.xml will be deined of course.
Static resources such as *.html files, *.css files, *.js files, images etc. should be placed outside of WEB-INF folder, in your case the test folder should be moved up next to WEB-INF.

java web app - can't retrieve pages that are not from root folder

i work with Intellij-idea IDE and tomcat. i tried to retrieve pages that are inside
folders in my web-inf and got HTTP error 404. however i can only retrieve the page in the root folder.
what to do?
thanks.
From the servlet spec, section 10.5 Directory structure (emphasis by me):
A special directory exists within the application hierarchy named
“WEB-INF”. This directory contains all things related to the
application that aren’t in the document root of the application. Most
of the WEB-INF node is not part of the public document tree of the
application. Except for static resources and JSPs packaged in the
META- INF/resources of a JAR file that resides in the WEB-INF/lib
directory, no other files contained in the WEB-INF directory may be
served directly to a client by the container. However, the contents of
the WEB-INF directory are visible to servlet code using the
getResource and getResourceAsStream method calls on the
ServletContext, and may be exposed using the RequestDispatcher calls...
So, what you experience is "by design". Place your resources elsewhere if you need to request them through HTTP.

web.xml welcome file from WEB-INF folder

I get the resquested resource is not available error while trying to load a welcome file from the WEB-INF folder, in my web.xml it looks like this:
<welcome-file-list>
<welcome-file>WEB-INF/html/index.html</welcome-file>
</welcome-file-list>
In other words, the html files are located in the WEB-INF directory in the folder named "html"...
So how do I do this correctly? It's so complex all this paths thing, I mean is there some kind of paths guide or anything? Because I just can't develop because I get stuck at these things when something can't be found because the path i write is interpreted differently than I expect it to...
Files in the WEB-INF directory are not directly available for access.
See URL:
Place private files in the WEB-INF directory, under the root directory. All files under WEB-INF are private, and are not served to a client.
You cannot access files under WEB-INF folder directly. Container will look for classes in WEB-INF/classes and jsp files under WEB-INF can be included by other JSP, but any browser requesting resources down there will get a 404 response.
EDIT: About your doubt below, if you have a standard Java EE webapp, below the root folder you should have:
/-
|
|-META-INF/
|-WEB-INF/
|-custom1/
|-custom2/
The first two are mandatory, but you can create extra subfolders (e.g. customX). Personally I create a custom folder "resources" to allocate there html, css and js files (in separate subfolders). If I have special JSP files which should not be accesed directly (only thru includes), I place them inside WEB-INF/.
WEB-INF folder is not accessible directly to web browser since this folder is meant for keeping files which are internal to application i.e. classes and configuration files etc. welcome page is something that should not contain any specific or private information, so can be kept in parallel to WEB-INF folder. All static html files can be placed at same hierarchy as of welcome file. To distinguish better, we can create sub folders.
i'm gonna tell you how i have the wellcome file, i have it like this
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
and the "index" is in the "web" folder, maybe you should set the wellcome file with just the name.
I hope this one helps you ;)

Where to store static files like html/css/javascript in a jetty project?

I have a maven project that I run using jetty:
$ mvn run:jetty
Where in my project should I be storing my static files like HTML, CSS, Javascript, images?
My layout is using a simple web app arch type:
/src/main/java/webapp/web-inf/views/
Should I just create a folder there named e.g. 'assets' ?
And then my view pages will reference the /assets folder somehow? I'm confused as to what path I will use in my html pages to reference an image like:
/assets/images/logo.png
This isn't so much a Jetty question as it is a general Java webapp question. If you plan to serve them out directly (like *.css, *.css, images, etc), put them somewhere above WEB-INF but below your docroot. Java WebApps are all the following basic directory structure.
<docroot>
+WEB-INF/
+lib/
+classes/
Anything in <docroot> is reachable directly through straight up http. Anything WEB-INF and below is not. A really simple webapp with one page (index.jsp), one image in an images directory, and its configuration file (web.xml) would look like this.
index.jsp
images/bob.jpg
WEB-INF/
web.xml
lib/
classes/
In index.jsp you could reference bob.jpg like...
<img src="images/bob.jpg"/>
This is really a Maven question rather than a Jetty question.
Typically you would put your images (etc) in the maven webapp directory - i.e. source/main/webapp/ (not under web-inf)
How you structure things underneath that is up to you, but it will mostly depend on how much content you are expecting to put in, and how you think it is best to organise it.
source/main/webapp/assets/images is fine, but so is source/main/webapp/images or source/main/webapp/static/.
Then, within your HTML, you reference the images using whatever path you put in beneath the webapp bit.
The general answer is - the root of your web application is webapp. Dynamic resources (as JSP pages or Freemarker templates) would better off be in a web-inf/ subfolder (they are accessible through classloader but not from a direct browser request).

Categories