how to dynamically change the values of a style sheet

I am creating a 2 level content management system, where the admin would be able to change the content and layout of the entire website. Currently I am working on so that the admin would be able change diiffrent style sheet attributes from the admin menu

e.g.

For the <h1> defined in the stylesheet, the admin would be able to dynamically change the font_size, font_color etc.
It might be looking easy to you but the tricky part is this same web content managenet appliation wll be used by many people, which means that many people will have this same website but with differnt data and layout(color, images) etc, all differntiated by a webiste ID.

Now what I thought was that  I will have a form where I take the users input on the font and save it in a database using that webiste ID. When the users of the wesite try to access the site the style is generated from the data in the database. But this seems to be hectic procedure because everytime the user tried to access a page of the wesite it will have to re-generate the stylesheet again.

Now is there any easy way I can generate the stylesheet only once. Any help will be greatly appreciated
royalcyberAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
pablomoralesConnect With a Mentor Commented:
The solution to your problem is to have one file for each chapter and generate the filename of the CSS file dynamically in the JSP. Like this:

<%@page language="java" %><%

String chapterName="chapter1"; // Get chapter name from wherever you have it.

%>
<html>
<head>
<title>Testing</title>
<link rel="stylesheet" type="text/css" href="<%=request.getContextPath()+"/css/"+chapterName+".ccs"%>">
</head>
<body>
<h1>Testing for chapter1</h1>
<p>This is a test</p>
</body>
</html>

This way when the administrator of the chapter changes the CSS you just modify the corresponding file and all users will see the changes as soon as they reload the page.
0
 
runa_paathakCommented:
Generate the stylesheet once, and store the contents of the stylesheet (text) into your database against the website id for the user. When the user accesses the page, you can obtain the stylesheet directly without generating it everytime.
0
 
royalcyberAuthor Commented:
I actually thought about this method but my question is when should i generate the stylesheet? everytime a change is made and should there be one stylesheet for all different websites? because what if more than one website are being accessed at the same time? If I need to do that that also it will be too cumersome. Would there be an other method easier than this.

Thanks

0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
suprapto45Commented:
Hi,

Can you explain more about your problem? I can't really understand it :)

David
0
 
suprapto45Commented:
Do you want the content management to have a customizable CSS that each user can choose on? or only admin can change the CSS style?

David
0
 
KuldeepchaturvediCommented:
I think you will have to keep as many stylesheets as your web site ids... these style sheets can be stored on your web server locally...
depending upon the web site id, you will need to apply the appropriate style sheet..
this saves your from generating the style sheet on every access of the site.. however you will have to make your pages dynamic to pick up the correct style sheet depending upon the web site id.
0
 
royalcyberAuthor Commented:
David I am actually creating a content management software for a institute, which have students throughpot the country. Each state has a different chapter(hence a website) and an admin assigned to that chapter. The admin should have the priveleges to change the stylesheet of their chapter and the problem I am facing is that if one admin logs on to change values of the stylesheet and then a student logs on to view data in that chapter  so do I need to generate the stylesheet for every page it accesses. Is there any better solution.

Kuldeepchatruvedi if i use differnt stylesheet for all the chapter website then should I probably have to use if statement in my jsp code to use stylesheet based on the id but in that case I new chapters are created i need to add them in the if statement. also how would I change the contents of the different stylesheets.

Thanks
0
 
pablomoralesCommented:
By the way you can include this code in all your pages by using the <%@include file="..." %> directive.

The other option would be to insert Javascript in the page that dynamically loads the correct CSS file. Since the chapter id is in the URL you could extract it and then call a JSP that generates the CSS file.

Unfortunatly there is really no way arround having to modify all your pages one way or another the easiest may be to just include the JSP to generate the correct CSS filename.
0
 
KuldeepchaturvediCommented:
I dont think you need if conditions in your jsp pages..
you can create a table in the database where you can keep the name of CSS & the web id... at runtime query the table to get the CSS & then include it..
you can write a short jsp page which then gets included in all your pages...
like CSS.jsp..
<%
//connect to the database
get the CSS Name

String CSS = CSS name
%>

in all your jsps

<%@include file="CSS.jsp" %>
<link rel=stylesheet href="<%=CSS%>" type="text/css">
0
 
pablomoralesCommented:
The problem with the approach given by Kuldeepchaturvedi is that is not as efficient since the browser will not be able to cache the CSS file. Most modern browsers will ask the server if the file has changed since it was last downloaded and only download it if it has.
0
 
royalcyberAuthor Commented:
pablomorales, If I use your idea, can you please tell me how would I be able to change the stylesheet when the admin makes some changes

Thanks
0
 
pablomoralesCommented:
I'm guessing you hava a Servlet or JSP that processes the changes from Admin interface. This is where you would generate the CSS file and save it to the correct directory. You can use something like this in the JSP that receives the POST of the form data:
<%

String chapterName="chapter1";
String headerSize=request.getParameter("headerSize");
...

PrintWriter css = new PrintWriter(  application.getRealPath( "/css/"+chapterName+".css" );
css.println("h1 { font-size:" + headerSize + "; }");
...
css.close();

%>
<html>
<head>
<title>Style changed</title>
</head>
<body>
Your style has been changed
</doby>
</html>
0
 
KuldeepchaturvediCommented:
>>The problem with the approach given by Kuldeepchaturvedi is that is not as efficient since the browser will not be able to cache
Well I beg to differ on this... at the end of the day browser only gets to see the generated HTML from your jsp..
so no matter how you get to the CSS name, it will always be cached...
basically there is no difference between your approach & mine... I am only saying to get the CSS name from database ( or wherever else it can be stored).. while you are asking the chapter name to be stored somewhere...
End result will be the same in both cases...
0
 
pablomoralesCommented:
I agree with you to a degree. You can get the browser to cache a JSP file generated from a database file, but only if you override the getLastModified() method of javax.servlet.http.HttpServlet in your JSP. You would also need to store the last modified date on your database and preform a query against the database every time a user request the CSS file. This requires more programming work and it is still not as efficient as a file access.
0
All Courses

From novice to tech pro — start learning today.