I have Glassfish webserver and I want My application cache the files in client side , I try multiple way And it does not work for any method and some method use apache webserver that is not in my case , basically I have this in my html file(Yes html file):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Wait Application Load</title>
<link type="text/css" rel="stylesheet" href="../theme/management/loader.css">
<script type="application/javascript" src="../js/loader.js"></script>
<link type="text/css" rel="stylesheet" href="../plugins/bootstrap/bootstrap.rtl.min.css">
<link type="text/css" rel="stylesheet" href="../plugins/fontawesome/css/all.min.css">
<link type="text/css" rel="stylesheet" href="../plugins/datepicker/persian-datepicker.min.css">
<link type="text/css" rel="stylesheet" href="../plugins/datatables/datatables.css">
<link type="text/css" rel="stylesheet" href="../theme/management/base.css">
<script type="application/javascript" src="../plugins/jquery/jquery-3.6.3.min.js"></script>
<script type="application/javascript" src="../plugins/bootstrap/bootstrap.bundle.min.js"></script>
<script type="application/javascript" src="../plugins/datepicker/persian-date.min.js"></script>
<script type="application/javascript" src="../plugins/datepicker/persian-datepicker.min.js"></script>
<script type="application/javascript" src="../plugins/datatables/datatables.min.js"></script>
<script type="application/javascript" src="../theme/management/control.js"></script>
<script type="application/javascript" src="../js/management.js"></script>
<script type="application/javascript"
src="../plugins/tiny/tinymce.min.js"
></script>
</head>
All of this load after reload page and total of resources file is 5.9mb that huge for any time I create request! so I want cache this in client side, I try this :
<meta http-equiv="Cache-Control" content="max-age=31536000, public">
not work for me , maybe can create question why html file I use not jsf or servlet or jsp! Thats because I create my own engine site that faster to develop and build.
So In glassfish-web.xml
or web.xml
how can I create cache role?
I check multiple site and found the old version that is not work.
here my basic glassfish-web.xml
and web.xml
:
<!DOCTYPE glassfish-web-app PUBLIC "-//GlassFish.org//DTD
GlassFish Application Server 3.1 Servlet 3.0//EN"
"http://glassfish.org/dtds/glassfish-web-app_3_0-1.dtd">
<glassfish-web-app>
<context-root>/</context-root>
</glassfish-web-app>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_6_0.xsd"
version="6.0">
<error-page>
<location>/Application/Page/Error</location>
</error-page>
<servlet>
<servlet-name>facesServlet</servlet-name>
<servlet-class>jakarta.faces.webapp.FacesServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>facesServlet</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<security-constraint>
<display-name>Restrict direct access to XHTML files</display-name>
<web-resource-collection>
<web-resource-name>XHTML files</web-resource-name>
<url-pattern>*.xhtml</url-pattern>
</web-resource-collection>
<auth-constraint/>
</security-constraint>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
I use this link and its not valid anymore :Caching Example GlassFish Version : 7.0.14
I don't know why Html can't Control the cache, by the way for resolve the cache file with servlet filter :
@WebFilter("/*")
public class AuthFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
long lastModified = file.lastModified();
long now = new Date().getTime();
long expirationTime = now + 86400000; // 24 hours
HttpServletResponse httpResponse = (HttpServletResponse) response;
httpResponse.setDateHeader("Last-Modified", lastModified);
httpResponse.setDateHeader("Expires", expirationTime);
httpResponse.setHeader("Cache-Control", "public, max-age=86400");
//OTHER STUFF
}
//...
}
The important note is that if you need to add response header , you can not added after response content!(It's Stream)