I cloned a Maven Git project and imported it into Eclipse Using File>Import>Maven>Existing Maven Projects. The project compiles and runs, but when I attempt to display a web page, the page is blank and when I use a web inspector I see Head and Body Html tags with no content.
The web pages are in src>main>webapp>WEB-INF>jsp>home.html or login.jsp which are among the files in use. In web.xml I have this entry:
<welcome-file-list>
<welcome-file>home.html</welcome-file>
</welcome-file-list>
Any ideas on what to look for would be appreciated. Thanks
If some files are missing check your .gitignore file and it's restrictions.
Otherwise check view resolver prefix and suffix. If your suffix is .jsp and you have home.html it would not be found beacuse resolver tries to get home.jsp which doesn't exists.
If your prefix is other then WEB-INF/jsp then it's trying to find it in some other directory.
At last check what your controller GET method is returning. Now there are combinations of possible mistakes e.g. if it's returning home.html then it tries to find home.html.jsp which doesn't exists - it needs to return only relative path from prefix and only the name of view without suffix.
Related
I have this project structure (WEB-INF is inside src/main/webapp/):
I try to access a login.css from login.jsp using
<link rel='stylesheet' href="${pageContext.request.contextPath}/resources/css/login.css">
However Idea underlines it as cannot resolve directory:
I use servlets for this project, but however in the other project that uses Spring MVC, the same css link works perfectly. I suspect the ${pageContext.request.contextPath} links to a different folder in this case: to the project root (../src) instead of the root/src/main/webapp folder.
Is there a way to change where ${pageContext.request.contextPath} links to? Or some other way to fix it?
"resources" looks like a directory for maven or concept/grouping in your IDE. They are probably combined at the root of your WAR> My guess would be that your you should remove "resources" from the link as they really reside at the root of your web app.
${pageContext.request.contextPath}/css/login.css
Take a look at the WAR that is actually produced and where they would be in that.
I'm making a java web app, and I want it to display an image. However, it doesn´t find my image.
I've made a folder in /src/main/resources/images
Also, in the .jsp file, I´ve tried with the following sentences.
<img src="/src/main/resources/images/Head.png"> </img>
<img src="< c:url value='/src/main/resources/images/Head.png'/>"> </img>
Is there anything bad I'm doing?
Thanks
Edit:
The path of mi .jsp file is /src/main/webapp/WEB-INF/jsp/welcome.jsp
You can find the web app code in https://github.com/Santi-7/hello
Your app is a Spring Boot app. I think that you can also try to use the facilities provided by Spring Boot for serving static content. Anyway, you are doing it right now because you are using webjars for css and js libs!!! Be consistent with the tech that you are using.
The structure of a .war file is as follows:
/
/WEB-INF
/lib
/classes
/META-INF
Now, your application has the following structure (I assume, given the folder structure, you are using Maven)
/
/src
/main
/java
/resources
/webapp
Now, the Maven war plugin will copy everything in the classpath to /WEB-INF/classes during compilation - this is /src/main/java and /src/main/resources by default.
The crux of the matter is that nothing under /WEB-INF or /META-INF can be accessed by requests - this is for security as otherwise someone could simply download /WEB-INF/web.xml for example.
So, in order to add a resource that is accessible by a browser, you need to place it into /src/main/webapp - this will become the root of the application.
So if you place Head.png into /src/main/webapp/images then in the JSP you would use:
<c:url value='/images/Head.png'/>
In short, you need to read up on how the directory structure of a .war works and how that relates to your code.
The path to the image must be relative to the path to the .jsp file.
Because the path to your image is: /src/main/resources/images/Head.png, and the path to your jsp file is: /src/main/webapp/WEB-INF/jsp/welcome.jsp, in your image tag you need to write:
<img src="../../../resources/images/Head.png" />
The ../../../ is for getting out from the jsp folder to the main folder, and the resources/images/Head.png is the path from the main folder to the image.
Thanks everybody, i could resolve my problem.
Changes I made:
So, in order to add a resource that is accessible by a browser, you need to place it into /src/main/webapp - this will become the root of the application.
Now, my images are in /src/main/webapp/images.
The path to the image must be relative to the path to the .jsp file.
Now, the sentence of my .jsp file is
<img src="images/Head.png" />
Edit [1]:
¡ I made a mistake. The path to the image is relative to the /webapp classpath !
I am doing some basic templating in my JSP-based webapp. For example, I want to have a standard header and footer (basic HTML) that I pull into each of my JSPs.
My content JSP is at /WEB-INF/jsp/home.jsp, and I have template JSPs at /WEB-INF/jsp/template/, such as /WEB-INF/jsp/template/Body-Footer.jsp.
So now, within home.jsp, I want to pull in my template files. First, I try the jsp:include action:
<jsp:include page="template/Body-Footer.jsp"></jsp:include>
It generates the error javax.servlet.ServletException: File "/template/Body-Footer.jsp" not found
Strange to me, considering that Eclipse says that the path is valid.
Okay, so then I switch to the include directive:
<%# include file="template/Body-Footer.jsp" %>
This works just fine, pulls in my footer HTML.
But why does the jsp:include not work? After some experimentation, I find that putting in the absolute path does get it to work:
<jsp:include page="/WEB-INF/jsp/template/Body-Footer.jsp"></jsp:include>
Now it works fine, no errors.
So here's my question: why? Why do I (apparently) need to use an absolute path with the jsp:include action, but not with the include directive?
/WEB-INF/jsp/template/Body-Footer.jsp is not an absolute path. Its also a relative path. The problem is that template/Body-Footer.jsp is an incomplete relative path, whereas the other is complete. That is, the paths are relative to your app path. Since /WEB-INF/ is under your app path, you have to include it. Absolute path means like C:/program files/tomcat/webapps/yourapp/WEB-INF/jsp/template/Body-Footer.jsp
Answer to WHY - The jsp:include is a runtime directive unlike the <%# include ... %> directive which happens to be a compile time directive (translation time, actually).
See more on: JSP Performance using jsp:include
Bottom line - directives are run against different folders as a base.
Btw. JSP pages should be outside of WEB-INF folder, if you want to follow official recommendation:
The Java EE 6 Tutorial - Web Module Structure
I read JSP 2.0 spec and here:
Relative URL Specifications
* A context-relative path is a path that starts with a slash (/).
It is to be interpreted as relative to the application to which
the JSP page or tag file belongs. That is, its ServletContext
object provides the base context URL.
* A page relative path is a path that does not start with a
slash (/). It is to be in- terpreted as relative to the current
JSP page, or the current JSP file or tag file, depending on where
the path is being used.
For now javax.servlet.ServletContext.getRealPath("/WEB-INF/test/test.jsp") is null for security reason.
Assume that context-relative path is path from your WAR root.
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 ;)
I am trying to make servlet.
I have installed tomcat6 on ubuntu with admin examples and docs. I am able to run the examples provided. But when i try to make my own servlet it doesnt work.
I did following steps
Under the ROOT i create folder with
-ROOT
----myapp
------WEB-INF
---------classes
I made two files one is index.html which have a button and action on form to call the servlet. Second is .java file. I compiled the .java file and .class is done. So now tree look like
-ROOT
----myapp
------index.html
------WEB-INF
---------classes
-----------TestServ.java
-----------TestServ.class
Now i open this in browser using http://localhost:8080/myapp
It shows up with index.html page with button. But when i click on the button it says
Error 404:
http://localhost:8080/myapp/TestServ not found !!
I dont know where m going wrong. I have set the CATALINA_HOME too. But still this problem continue.
You need to create a web.xml in the WEB-INF directory, and define the servlet mapping in web.xml, so that the myapp/TestServ URL is forwarded to the TestServ servlet class.
Here is a page describing web.xml, and has the example and elements you need to set up. For your class, these elements will probably look something like this:
<servlet>
<servlet-name>testServ</servlet-name>
<servlet-class>TestServ</servlet-class>
</servlet>
<servlet-mapping>
<!-- For any URL starting with /content/, the rewriter servlet will be called -->
<servlet-name>testServ</servlet-name>
<url-pattern>/TestServ</url-pattern>
</servlet-mapping>
You shouldn't be deploying any of your code under ROOT.
You shouldn't have any Java class in the default package. Try putting your TestServ.java in a package.
Your deployment should NOT include any .java files.
You've got to register your servlet properly in web.xml. Include a mapping to a particular URL.
Your best best is to create a WAR file named myapp.war, which includes WEB-INF/classes and WEB-INF/lib and a web.xml for your situation. Put that in the Tomcat /webapps and start the container. If you've registered your servlet properly you should be able to access it via http://localhost:8080/myapp/TestServ.
I'd read the deployment docs carefully.