Java Web app: Welcome file list - web.xml

Should the welcome file mentioned in welcome file list tag always be physically present?
i.e. jsp, html etc. Or can it be a URL pattern?
I defined a welcome file list in the web.xml as:
<welcome-file-list>
<welcome-file>/home</welcome-file>
</welcome-file-list>
/home downloads a JSON file from the server to display on the browser. But whenever I start the application, it does not take me to the following page: http://localhost:8080/myapp/home. Instead it always goes to http://localhost:8080/myapp/ only. Please advise what am I doing wrong.

it does need to have a physical file on your webapp folder for a welcome file to be found even if some kind of controller will treat the request, for example, if you have a JSF app and the FacesServlet only handles requests of type *.faces on your app you should place an empty file called home.faces under your webapp folder, so it can be correctly mapped, on your case I guess the issue is slightly different, you want to fetch data from the server side and display it on the browser when user first lands on your app, so what you can do is actually:
Create an HTML called home.html as your welcome file with an empty div as a placeholder for your data.
Use a Javascript library or do an AJAX call to fetch the JSON from the server side
If the result call is OK, render the placeholder DIV with data fetched from server.

Related

SpringMVC: How to prevent client directly visit a JSP view?

I am using SpringMVC to make a simple web app.
My controller request mapping is like this:
#RequestMapping(value = "index.html")
public String index(Model model) {
model.addAttribute("type", "index");
return "index";
}
When I use visit the following URL:
http://localhost:8012/MyCloud/index.html
Things work fine and I can see the properly rendered /views/index.jsp.
But if I directly visit the views/index.jsp file with the following URL, the URL is indeed visitable. And an ugly 500 server error because apparently, there's no attribute named "type" been set so NullReferenceException is thrown.
http://localhost:8012/MyCloud/views/index.jsp
By mapping request to *.html URL, I want to trick my customer into believing they are visiting plain HTML page. But if they somehow managed to know my JSP view locations and visit them directly, they will see ugly errors.
Can I prevent this?
Shall I use an error page?
Servlet containers won't serve any content in WEB-INF. By putting your JSPs there, you prevent anyone from directly accessing a JSP by navigating to it in the browser by name.
so move all your JSPs to the WEB-INF folder, and user will not be able to directly access the urls, while the controller code will be able to render the UI properly with them.
You can cofigure a error page in web.xml
<error-page>
<location>/general-error.html</location>
</error-page>

How to load an entire HTML page from one website into another

I have been working on a website www.xyz.com which is hosted on some server. I have been loading forms & contents from www.abc.com using I Frame which is hosted on Azure. We have now decided not to use I Frames and to load the content from www.abc.com using Jquery AJAX. Now the abc.com is providing me the UI page fragment for my forms and contents from Azure blob storage in form of .html file or .txt file. If i try to do a normal AJAX call for the HTML or TXT file path, i can the see the content coming in the Response tab of that URL but the code does not enters the Success template and hence i am unable to modify or access the content.
Please suggest
You can use Application Request Routing (ARR) for this. This is a module you can install in IIS which also works in Windows Azure.
The following image illustrates how ARR works. An incoming request is intercepted and based on a set of rules the request is forwarded (for you this will be to xyz.com) and the response is the served back to the user. Even though this illustration explains the principle with sub directories, it can also be applied on the full site.

restricting a user's direct access to JSP

I have trouble with restricting user access to some pages. I send an ajax request to servlet. In ajax success I want to redirect a page to another page based on condition. But i set a servlet constraint in web xml, so redirection results in error with 403 code. response.sendredirect also does't work as it is an ajax request.
Any ideas? please, help me to do this redirection with relevant restrictions.
There is a very easy solution for your problem.
That's to put the jsp files(which you don't want to be accessed by users directly) inside WEB-INF folder. The reason? Well,everything inside WEB-INF folder is by default private members of the whole application. That means that, these files can only be accessed by programs i.e. servlets.
So if any user try to directly access the jsp pages, he/she will get "Http 404" error.
In this way you may restrict user access to specific files.

web.xml error-page does not display image

I have an error.html page which I want to redirect to when a user comes accross Error404 for example. The below is working:
<error-page>
<error-code>404</error-code>
<location>/error404.jsp</location>
</error-page>
However, my error404.jsp file contains an image which doesn't get displayed when user is redirected. If I just type in the URL, the full page with image is displayed. But if a user tries to do something which reports error 404, the error404.jsp file gets displayed without the image.
I also tried with having an error404.html but I get the same problem...
Do you have any ideas why this is happening?
Many thanks
Ena
When the error page is rendered, its content is returned in the HTTP response without a redirect. This means that the browser still 'sees' it in context of the URL of the request that had an error. So any relative resource links (images, css, etc.) will be relative to the current page's URL.
So if you have the following setup:
image1.gif in images folder
error404.jsp that references the image as "../images/image1.gif"
You will find that typing http://yourserver.com/YourContextRoot/error404.jsp works fine, but when you use http://yourserver.com/YourContextRoot/path1/path2/missingPage it tries to look for the image in the wrong path (i.e. under /YourContextRoot/path1/images/image1.gif).
The solution is to use JSP EL or scriptlet to create a server-relative link (i.e. starts with a '/'), by including context-root:
<img src="${pageContext.request.contextPath}/images/image1.gif" />
I hope that works!

Secure files from being downloaded by using the absolute path in the URL

in my MVC 3 project I have a folder in the project's root where I store some SWF files. The problem is, when I hit the url in the browser's address bar, e.g
localhost:39217/Files/fg/f_l1.swf
obviously I see the download dialog. Is there any way to prevent it ? In the other words, that file would be visible in my page after the DOM is loaded, but if I just type its URL I don't want it to be downloaded. I'm afraid that both scenarios are threated the same in the IIS. Any ideas ?
One way I can see to solve this issue is don't reveal the real physical path to the user. Basically you should deliver the SWF files from a controller action.
If you are embedding the SWF file through object tag then the object tag will refer to this action passing the filename. You can control the action by Authorize attribute or some other ways and once you see the request is properly authorized then you write the flash file into the response.
The idea is clearly explained here though the code is in PHP you can migrate that to MVC.
UPDATE:
If you don't want to change the SWF file path then you have to do little more work in Global.asax.cs.
routes.IgnoreRoute("Javascript/{*catchall}");
routes.IgnoreRoute("Content/{*catchall}");
routes.IgnoreRoute("Scripts/{*catchall}");
routes.RouteExistingFiles = true;
routes.MapRoute("", "Files/Flash/{file}", new { controller = "File", action = "Flash" });
Now eventhough some one tries to access the SWF file directly knowing the path, the requests are handled by the Flash action of File controller and there you can do the necessary auth. check before sending back the SWF.

Resources