web.xml welcome file from WEB-INF folder - java

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 ;)

Related

Accessing images of website background and other decorations in WEB-INF folder

I am trying to use some images to decorate my website. But I cannot access those images that placed in WEB-INF folder. My code looks like this:
<img id="welcomeGIF" src="/MyCourses/WebContent/WEB-INF/css/welcome.gif " class="ribbon"/>
I have no problems accessing these images if I simply put them outside the WEB-INF folder under WebContent directory. I am wondering what I am doing wrong? I am new to web-app development so I am not sure if all the css files should be put in WEB-INF. If so, how do I access them?
Here is the structure of my program:
the cause:
There is a rule that the resource in the WEB-INF directory can not be accessed by url (the client)
So when the html is loading, the html tag will send a request to the server (to the url MyCourses/WebContent/WEB-INF/css/welcome.gif) while the resource is in the WEB-INF directory so you can not access it;
how to solve
the java programmer can access the web-inf directory,so you can use the Dispatch request api
web:
<img id="welcomeGIF" src="/MyCourses/getWelcome" class="ribbon"/>
java programmer (FileAccess.class)
request.getRequestDispatcher("/WEB-INF/css/welcome.gif").forward(request,response);
web.xml
<servlet>
<servlet-name>getGif</servlet-name>
<servlet-class>FileAccess</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>getGif</servlet-name>
<url-pattern>/getWelcome</url-pattern>
place the static file out of the Web-inf directory,(it is ok to do this althought the security is not better than before)
if you using the springMvc framework,you can use the mvc:resources configuration to set access the static file directly

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.

How to include JSP from jar lib

I have a web-app with a lot of lib projects, which are common to another apps.
I'd like to put some JSP files in one of those libs and then make a <jsp:include... or <%include... But I'm not able to make it working.
Is there a way to achieve this?
If there is, which is better, to do a include directive (#include file..) or action (<jsp:include page...)?
Thanks
I know of only one way.
Keep the JSP in /META-INF/resources folder.
Create a 'lib' folder inside the WEB-INF folder in your WAR.
Place the JAR (containing the JSP in the /META-INF/resources folder) in the lib folder of the WAR.
In the main JSP where you want to include this external JSP residing in the JAR, use the following statement: <jsp:include page="/myJSP.jsp" ></jsp:include>. Note that the '/' is required in front of the jsp file name to make the container look for the JSP in an absolute path (and hence in the jar in the lib folder) rather than in the current path where the main JSP is lying.
This will surely work.

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).

importing class to JSP

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.

Categories