page.jsp does not work - java

I have a tomcat - spring mvc - jsp application.
I have discovered that a page called page.jsp is not found (404). But if I called page2.jsp then it just works fine.
Is it a bug or is it written somewhere in the spec that you can't call a jsp file page?
(BTW, I called it page because it is a part of the system that allows admins to administer pages. I.e. it is a name I really wanted - although I will readily switch to what works)

404 means 404, jsp is simply not there. This is tomcat, so go to webapps directory, and look into directory your war is unpacked into. Look for your JSPs.
Possible reasons for 404:
File is not there. Failed to package it
File is there, but called PAGE.JSP. URLs after domain name are case-sensitive.
Some funny filter installed in Tomcat that really prohibits page.jsp from being accessed. Unlikely.

Check your spring configuration file for URL mapping or there may be a wrong call to page.jsp from a controller.

Related

Is this Structure for Servlet JSP application correct?

I am building a servlet app to run on tomcat server. However when i was not able to update the jsp page using:
request.setAttribute("operation", "op_name");
request.getRequestDispatcher("\WEB-INF\index.jsp").forward(request, response);
404 index.jsp not found comes.
I think that servlet and web elements like jsp are in different paths that's why it is going wrong. So do i need to make another project using maven structure like having src\main\java etc. or this structure is also fine? Also how to update the index.jsp using this structure?

How to read a file in tomcat 7 folder using SpringMVC

I have a quick question and wonder if anyone has come across this issue before?
I am trying to read a file from my classpath into a .jsp with spring but I keep getting a 404 even when the url is correct.
eg
/var/lib/tomcat7/webapps/ROOT/WEB-INF/classes/file/MyProcess.bpmn
The file is available in the browser from the above url but not through the .jsp in the Spring Framework where a 404 (Not Found)is returned.
Has anyone come across this problem before?
You have to move your file out of the WEB-INF directory I think because it won't be accessible from your jsp page.
This link should be useful
What is WEB-INF used for in a Java EE web application?

Common Error Page for All Directories in Java Web App

I am trying to have a error.html error page in my Java WebApp with web.xml entry as below:
<error-page>
<location>/error.html</location>
</error-page>
having a directory listing as:
--App
-- index.html
-- error.html
-- css/
-- ...
-- js/
-- ...
-- img/
-- ...
-- folder/
-- index.html
When I enter an address say:
/App/some-wrong-address or /App/folder/some-wrong-address
In both the cases I am able to see the Error Page called in as part of 404 error, but in latter case I am not getting my resources like caa, js, or img pulled up.
I understand that the resources are called relatively with ./... path and in the second case it is expecting it to be ../... because of directory change, but I want to OVERCOME this thing, I am not directly working on production server and I cannot use exact URL of everything with localhost as it will have to change later everywhere.
Let me know how can I do this?
I am using Tomcat 8.0.28 and WebApp version is 3.1.
The key is to correctly produce all of the HTML that your webapp produces. The HTML must have the correct URLs (note: they are URLs, not paths) in the elements that refer to other assets (CSS, images, javascript).
You are correct that putting the absolute URL of your production system in your source files is not a workable solution.
In a JavaEE web app I worked on, we used JSF Facelets as the templating system for producing our HTML. In that we wrote each URL like this:
<script src="#{request.contextPath}/foo/bar/baz.js" />
<img src="#{request.contextPath}/img/something.png" />
This allows any template, at any location in the URL hierarchy, to reference any asset. The JavaEE app server handles filling in the correct context path so that the resulting URL the browser handles is correct regardless of where it is hosted.
application.getContextPath() will give you page context,and you can base ur urls accordingly after that..will work on local server as well as deplyment server..
e.g: ... href=" <%=application.getContextPath()%>/css/yourcss.css" ... etc.
N.B.: its a good idea to design your error page in a way that it as no dependencies to any other files..styling and resources in that case should be absolute or inline and images if any should reside in the same folder as your error page

Request Dispatcher to access pages in WEB-INF folder?

according to netbeans e commerce tutorial. https://netbeans.org/kb/docs/javaee/ecommerce/page-views-controller.html#view there are 4 pages include header & footer placed in WEB-INF folder but they can access it via controllerservlet (RequestDispatcher).
i've googling and found a lot of questions about how to access/redirect to pages in WEB-INF folder but the result can't access pages(xhtml,jsp,etc) in WEB-INF folder.
my questions is
1. could i access pages in WEB-INF folder with RequestDispatcher (with JSF 2.X) ?
2. how to access pages in WEB-INF with JSF 2.X ?
A simple solution is to create a page that is outside WEB-INF. Let us call this page placeHolder.xhtml
If you know which page fragment you wish to show from within WEB-INF, then make it available via a bean. Let us say the following method returns the page that has to be included
#{mybean.pageToInclude}
Now, in the placeHolder.xhtml file, use the ui:include tag to include page that is present under WEB-INF
placeHolder.xhtml
...
<ui:inlucde src="#{mybean.pageToInclude}"/>
This way you can get code within WEB-INF to be made available in a page that is outside of it.
There are other techniques like writing a ResourceHandler to locate view resources under different locations that you can use too.
Hope that helps.

How to put jsp in ROOT folder of tomcat so that the jsp is picked by all web apps

I have multiple web applications defined in my tomcat. In case of any exception, I want to throw one jsp (done using SimpleMappingExceptionResolver tag of spring). When I put the jsps in the web-inf folder of the web applications, it works fine which is obvious.
But I want to put this jsp at a common place in tomcat such as ROOT library. But if I do this, tomcat is not able to find my jsp. Can somebody tell me if any changes in web.xml is required to make this happen or I should put this jsp somewhere else.
Thanks in advance.
What do you mean by 'picked up' or 'access'? You can put a jsp file on the tomcat ROOT application, and do a 302 redirect into it everytime you encounter exception.
For example place you all-apps generic exception page on webapps/ROOT/generic_exception.jsp, then on each of your apps, add this to the web.xml
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/myapp_error.jsp</location>
</error-page>
That should redirect request into myapp_error.jsp (inside myapp) if any uncaught exception surfaces. Then inside myapp_error.jsp, just perform html meta redirect to /generic_exception.jsp
However the drawback of this approach is you are redirected into different web-app, it's difficult / require extra work if you want to pass session attributes

Categories