Separating static web pages from WAR file - java

I have a web application which is a WAR file. I will be hosting this in Amazon cloud, using Linux and Tomcat. However I now need to create a different UI using HTML and CSS, no Java or WAR. My plan is, user will access this static user interface first and from a link there, they will access the actual java based web application.
There are 2 ways I can do this. First is to put all of this static content into the WAR file. Second is seprating the static content from the WAR file. I prefer the second method, because I can add info to the user interfaces without shutting down the entire web application.
But, how can I do this? How can I put these static files to tomcat and link the WAR file to it? Specially is this is possible in amazon cloud?

Build a java based rest api, use angular on the front end and access the rest endpoints via ajax.
Separate the two projects, upload the static html/javascript to a cdn/cache.
Or alternatively you can keep the html/javascript in the same war in the root of your webapp, it will (probably) be cached by the webserver.
Don't use JSP's.

Usually it is done by providing Apache or other proxy before container. So it will forward some requests to application container, but static files by its own.

Related

Two servers each for HTML and Java Jersey Rest APIs or Single Tomcat Webapp

This is a question to design my dynamic website. It should have a java REST APIs beckend and static HTML view.
Should I keep my HTML content in separate server(like AWS S3) and REST APIs in Tomcat(hosted in a EC3 instance)
OR
Should I keep index.jsp as my starting point of application. Thus making both HTML and JAVA sit together in the same project. And If I do take this option, will I be able to use Angular and Bootstrap with this or not.
Hope you understood the question :)
I will advice to deploy dynamic content on the Tomcat app server and static content (HTML, images, CSS) on Apache web server. I wouldn't keep static content on s3. It being on Apache gives a lot of advantages like - using Apache server you can compress, cache, authenticate, throttle your static content.
The static and dynamic code can be together in the same project and repo. The devops build process can build jar and war and deploy them separately.

Where to upload an image/file to make it appear on HTML?

Well,I'm working on an Eclipse Dynamic Web Project under Tomcat.
I'm trying to make a web application/site.In a jsp/html page,there is a form where a user can upload a photo.
I handle then this action from a servlet that has to store this image/file somewhere so as to make it possible the image appears whenever I want on the site.
Here is the problem.I started by storing it on my file system,(path in a database) but when I wanted to retrieve it the page didn't appear.
I guess the reason is here:
Why can't I do <img src="C:/localfile.jpg">?
Then,I tried to store the file in the eclipse project folder(WebContent/folder) where I've stored manually some images that do appear.
File folder=new File("/TED/res/img");
File file=new File(folder,fileName);
System.out.println(file.toPath());
Files.copy(fileContent, file.toPath());
But this exception happens:
java.nio.file.NoSuchFileException: /TED/res/img/2017-08-13-123524.jpg
It's one the line of files.copy command which means that
new File(folder,fileName) that I tried failed
What should I do? From what I've read,I understood that also saving file in the IDE's project folder is also wrong but what other choice do I have?
Ultimately, the project will be deployed to a server. As such, there are three distinct issues:
Uploaded user content location: content like images should be uploaded to a folder outside your web app (project). Images inside the web app (project) should be those that are necessary for the application and provided by the developer, not user-generated.
In Eclipse, during development and testing, you will want to serve these images through Tomcat. There are many ways to do this. Tomcat configuration is probably not the best for this - please read the answer and discussion here: Simplest way to serve static data from outside the application server in a Java web application
Once the application is deployed to the server, Tomcat will most likely run behind a Web server like Apache or Nginx. In this case, the external image folder and its contained files can be served directly by the Web server. Even if you implemented a servlet in (2) for local testing with Eclipse and this servlet is part of the code that is deployed, it will not be invoked as the URL will be intercepted by the Web server before it reaches Tomcat. For example, if your uploaded image folder is C:\images on your development environment, it can be served by the servlet using the technique in (2) as /images/*. When deployed to a server, the Web server can be configured to servet /images/* from /srv/content/images and this request will never reach Tomcat.

How can I externalize a PDF from a Java EE application?

I have a Java EE application, running under WebLogic 10.3.5 and Java 6.
I used to have a pdf help file, embedded in my war file, but I need to extract it from there and put it in an external directory (it can be in my same WebLogic domain directory).
I tried to put it in my WebLogic domain and then to < a href > it, but it seems that browsers have limitation and for security reason will not allow to download local file with a href or javascript.
This used to work only on a static HTML file saved on my computer but one the HTML page is deployed on the server, it seems not be possible.
Any idea how I can externalize my help.pdf file from my war file?
#limc is right
you should put this static file outside of Weblogic altogether as a file on an Apache web server
However, in Weblogic there is a feature of virtual directory mapping which allows you to declare a folder outside of the weblogic domain as a content store for any static stuff.
http://docs.oracle.com/cd/E11035_01/wls100/webapp/weblogic_xml.html#wp1039396
This entry goes in WEB-INF/weblogic.xml
<virtual-directory-mapping>
<local-path>c:/usr/mypdfs</local-path>
<url-pattern>/pdf/*</url-pattern>
</virtual-directory-mapping>
Although some application servers allow a Java EE app to reference a file outside the web container, in reality, your web app shouldn't have any knowledge about anything outside the web container, and as you have mentioned, it is indeed a huge security concern.
Depending on what you are trying to accomplish with this PDF file, if you merely want to expose this file on the web, do what #duffmo said and it will work fine. If you want the flexibility to modify this PDF file frequently without recreating the war file again and again, you may want to consider hosting this PDF file in some HTTP web server (Apache2, IIS, etc) and now you reference that link from your web app.
You need to put it at the root of your web context, in exactly the same place as HTML pages. Your web server will be able to find it there.

How to use both http server and application server in a java web application

I have some deployment model question for a Java EE web application. Currently we are deploying our web application as a WAR file in Tomcat 6. All the content is packaged with the WAR file including the static content like images, static html pages and so on. But i want to deploy these static content in a HTTP server and use the Application server only for retrieving the dynamic content. How do i split these things? Does any one has done any thing of this sort and have a good deployment model for my scenario. Help will be appreciated.
Is it a good idea to make 2 WAR files one with only static content and deploy that WAR in HTTP server and the rest as a different WAR file and deploy it in the Application server? But this approach will have impact on all the pages where the static content is currently referred and requires code changes which is very cumbersome since our project is Huge and the code based is very very big.
Any strategy and ideas are welcome.
This can be something interesting to do for performance reasons.
You should have separate deployment scripts / deployment files to do this.
Having multiple file/WAR/folder/scripts to deploy for one project is not an issue. We have the same thing when you have to deploy your WAR and to update your database.
I would have a WAR file and a folder with your static content to deploy.
Edit
Deploying the static content in a HTTP server depends on the server.
If you want to use Apache on a Linux server, you have to set up a Virtual Host.
<VirtualHost *:80>
# This first-listed virtual host is also the default for *:80
ServerName www.example.com
DocumentRoot /www/domain
</VirtualHost>
In this example, you have the a virtual host that listens on 80 port, for any IP address and for the server name www.example.com. Then this is redirected to the /www/domain path.
You will find much more examples and configuration options in the documentation.
You can not deploy WAR file into HTTP server. A WAR is used for Java web applications it must be deployed into application server or servlet container (like Tomcat). I don't think that its a good idea to separate static content in a separate web application. If this is one project it should be one web application, besides:
A WAR file has a special folder structure and contains special files
in addition to JSP pages, Java servlets, Java classes, HTML pages etc.
which combined forms a Web Application.
You can hold your static contents in your one application and there is really nothing bad about it.
If your project is very huge and has a lot of files it is no problem, you just need to use the project structure like that, that it should be easily understandable and readable and the application server or servlet container will take care of deploying as many contents as there is.
Up to version 4, Tomcat has been quite slow in serving static content. This is why it was frequently recommended to split dynamic from static content and serve the latter using a regular web server (the book you mentioned was issued in 2002...). Recent Tomcat versions do not face this problem, thus you can IMHO refrain from splitting, which can be a nightmare for both organization and security.
For static resources, you might rather focus on configuring proper caching, so they will not be transferred more often than necessary.

Store and retrieve files from outside the WAR in google app engine?

Well I am pretty happy working at home with a local Apache Tomcat and serving the static files with help of FileServlet, also I am following a instruction as said in this answer that:
You should not store the files in the webcontent. This will fail when the WAR is not expanded and even when it is, all files will get lost whenever you redeploy the WAR.
But now I am using Google App Engine for my application, where I cannot configure the server, also it usesjetty which is new for me. All the things in my web application gets uploaded to server through eclipse but I cannot upload a folder outside to WAR. Now how can I apply such static file serving here using a Servlet, should I use Google Blobstore? , or just simply a database that can be Google Cloud SQL or there is any other way out?
Thanks,
Asif
The answer in the link does not apply to you: it is saying that one should not upload (i.e. programmatically create) files to webcontent folder.
Just put static files that you want to be served inside you application folder. See docs on using static files.
You don't need a FileServlet to serve static content. Instead, declare the files that intend to be static, and App Engine will manage serving them. If for some reason you need to read a file programatically, then declare it to be a resource.
See https://developers.google.com/appengine/docs/java/config/appconfig#Static_Files_and_Resource_Files

Categories