问题
I want to server a JSP page that is stored in a database as a blob. So if a request comes in at the url http://mydomain.com/app/list.jsp I know to go to the DB to retrieve the resource list.jsp.
I am using spring and tiles, so have adispatcher servlet and controllers set up and working in traditional sense. Would this be similar in principle to the resource servlet that spring web has to serve javascript files and messages from within jar?
Note that the JSPs would not just be static HTML, I will have beans (model attributes) associated with the page, so will still want to use EL to query the bean.
Cheers
回答1:
JSP loading and compiling is implemented in the servlet container. There are two approaches I can think of using for making this feature possible:
- Modify the servlet container's JSP servlet. If you are using, say, Jetty or Tomcat which are open-source, you can easily look at their JSP servlet and change it so that it reads the JSPs from the DB. You may be able to adapt one of these for use in a proprietary container. This is the most direct way of solving the problem but you are stepping into a minefield of potential bugs.
- Build the CMS in the database as planned, but copy the JSPs to the running application's filesystem while the application is running. Let the application server's normal JSP change detection notice that the changes are occurring. You could wrap all requests with a Filter which checks the DB for updated JSPs, copy the JSPs at modification time, or use a scheduled job to copy them at certain intervals.
In both scenarios you have to worry about memory leaks for unloaded classes, especially if any of your code uses ThreadLocals or other static variables. The normal JSP loaders already suffer from problems if you unload WARs or recompile JSPs at runtime. This is due to limitations in Java and is not easily solved (depending on which JDK is used). I would recommend never, or very rarely, changing JSPs without restarting the server unless you can't avoid it.
回答2:
See for example Eval taglib from Coldtags suite: http://www.servletsuite.com/servlets/evaltag.htm
来源:https://stackoverflow.com/questions/1839938/serve-jsp-stored-in-db