This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes JSP and the fly likes Store jsp content in database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Store jsp content in database" Watch "Store jsp content in database" New topic
Author

Store jsp content in database

Jeppe Sommer
Ranch Hand

Joined: Jan 07, 2004
Posts: 270
Hello,

Lets say I want save some content from a jsp page (HTML and jsp code) in a database. To make it simple I could have one field for the subject and one for the page content. But if the page content contains jsp code how is it then possible to execute that code?

/* Data in database */
INSERT INTO page ( pagecontent ) VALUES ("<b><% out.println("Hello world") %></b>");

If I want to query the jsp code from the database and execute it a jsp page, how is that possible...:

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60740
    
  65

Why would you want to do something this convoluted?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Jeppe Sommer
Ranch Hand

Joined: Jan 07, 2004
Posts: 270
Hi,

Not necessary (ups)... I am open for the best solutions. I know some systems store the data like subject, page content and visa versa in a XML file, which is stored in the database...

I just want to get some simple solutions on how to manage a website on the fly, i.e. be able to create and edit the content on a website like you do in a Content management system...

Sometimes your pages will use jsp code (i.e. news, debate) and if you do not prefer to hardcode the jsp code into a file stored on the webserver, but rather want to save it in the database to have easy access to make changes, then how can we solve an issue like that..

Do we use the include page action in the page content data:
<jsp:include page="codeFragment.jsp" />

- and save each code fragments into separated files?
[ August 08, 2007: Message edited by: Jeppe Fjord ]
Remko Strating
Ranch Hand

Joined: Dec 28, 2006
Posts: 893
Just separate the layout and the content. The content could be stored in a database and the layout could be jsp.

This gives your more flexibility in your design.


Remko (My website)
SCJP 1.5, SCWCD 1.4, SCDJWS 1.4, SCBCD 1.5, ITIL(Manager), Prince2(Practitioner), Reading/ gaining experience for SCEA,
Jeppe Sommer
Ranch Hand

Joined: Jan 07, 2004
Posts: 270
Okay I see.

But if you want to include some jsp code in the content and want to have access to the jsp code i.e. stored in the database, how would be best practice?
Jeppe Sommer
Ranch Hand

Joined: Jan 07, 2004
Posts: 270
Originally posted by Bear Bibeault:
Why would you want to do something this convoluted?


Hello Bear Bibeault,

You write that it is convoluted, which solution would you prefer if you want to store more than static content in the database?

Thanks in advance.
[ August 12, 2007: Message edited by: Jeppe Fjord ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

JSP has no 'eval' functionality.

There is no best practice for storing code in a database.
JSP is a templating language.
Why would you want to store code in your database?


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Jeppe Sommer
Ranch Hand

Joined: Jan 07, 2004
Posts: 270
Originally posted by Ben Souther:
JSP has no 'eval' functionality.

There is no best practice for storing code in a database.
JSP is a templating language.
Why would you want to store code in your database?


I wanna store the static content in the database. Each page with a unique ID. So far so good... Let�s say I want to include some code fragments (data from database), i.e. three latest news, job offer etc., between the static content, how can I do this?

It seems that I can�t use code lines like:
<%@ include file="codeFragment.jsp" %>
<jsp:include page="codeFragment.jsp" />

- inside the static content.

But instead it seems that I can use iFrames to include files with jsp code...

Any other ideas?
[ August 12, 2007: Message edited by: Jeppe Fjord ]
Michael Ku
Ranch Hand

Joined: Apr 20, 2002
Posts: 510
I have done something like this (the business users enter HTML into a DB and I retrieve it).

I did not do this in the JSP, but in the servlet (really a Struts Action but not in the JSP). You can then create a view object and place it into the request. Then on the JSP you could check to see if the attribute exists using JSTL. If it is there, then render the html (the view object will have the html as a property).
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60740
    
  65

Originally posted by Jeppe Fjord:
I wanna store the static content in the database.


Fine, static data is no problem. But JSP code is not "static content". And this is where things get rather ugly, You can't just grab JSP fragments and send them to the browser. The JSP needs to be translated and then exectued. And there is no specified means to easily make that happen with JSP fragments read from a database.

You haven't ever answered the question of why you want to store JSP rather than the data in the database.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60740
    
  65

Originally posted by Michael Ku:
If it is there, then render the html (the view object will have the html as a property.


I can't see how this addresses the issue. Sending HTML to the browser is one thing. Sending JSP is another. At what point does your solution trigger the translation and execution of the JSP fragment?
[ August 12, 2007: Message edited by: Bear Bibeault ]
Jeppe Sommer
Ranch Hand

Joined: Jan 07, 2004
Posts: 270
You haven't ever answered the question of why you want to store JSP rather than the data in the database.


I am just looking for an easy way to let the end user be able to "include" data from database (jsp code) and edit this code as well, into the static content. Maybe there is other ways around and then I am open to here such solutions.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41034
    
  43
Originally posted by Jeppe Fjord:
I am just looking for an easy way to let the end user be able to "include" data from database (jsp code) and edit this code as well, into the static content.


Yes, but why JSP? Why not HTML or plain text? Do you really have users that prefer editing JSPs to editing text or HTML?


Ping & DNS - my free Android networking tools app
Jeppe Sommer
Ranch Hand

Joined: Jan 07, 2004
Posts: 270
Originally posted by Ulf Dittmer:


Yes, but why JSP? Why not HTML or plain text? Do you really have users that prefer editing JSPs to editing text or HTML?


Yes, I have some users, including my self, who wish to write/edit jsp code to be able get data from the database and include it as a part of the static content pages. (Is it possible to get data from the database (to query) without using JSP/java code?)

If JSP can�t be stored in the database, as I understand it can�t, then can you have some jsp files (code fragments) stored on the webserver as files in a folder, and then let the end users have access to those files. If they want to include some of the JSP files into the static content pages, they have to use iframes right? Or are there other ways around?
[ August 13, 2007: Message edited by: Jeppe Fjord ]
 
Consider Paul's rocket mass heater.
 
subject: Store jsp content in database
 
Similar Threads
How do i use JSP include pag tag to include a java class file
Web service to get document / image content taking too long
jsp;forward problem
passing jsp variable to java script function on same page
Urgent Help Needed in JSP