Here is a simple question/challenge. I’m tasked with creating a virtual “revolving door” for a website. I’m creating an index.jsp that randomly loads 1 of 4 different index pages. How would you do this? What is the quickest/easiest way to accomplish this?
Ready. Set. Break…
This Post Has 6 Comments
If we tell you how to do it, then we spoil the fun for you and take away your challenge. Here’s some tip, very quickly off the top my head as I read your blog: (assuming you use straight JSP, no Struts or any other web framework)
1. Write a little Java snippet that generates a random number, of course from 1 to 4 because that’s in your requirement. That can go in your index.jsp.
2. Use JSP forward in your index.jsp or redirect. Of course, there’s huge difference and implications. Again, only you can decide what’s best.
3. The page that’s supposed to be forwarded to can be data-driven, hard-coded, …again your decision.
As I said, this is off the top my head, and there are probably other ways of doing it, and many variations based on different web frameworks.
Thanks for the quick response. The solution I implemented is very similar to what you’ve mentioned.
I just created an ArrayList and populated it with the various pages I’ll be using. I get a random number and then use that as the index to pull from my Arraylist. Then it’s just a jsp include.
x = rand(1,4);
I personally would put all variences of the indexes in a dir. Then parse thru the directory to see how many indexes you have. Then take that number and put that in the above rand.
count = Parse_dir();
x = rand(1, count):
That way it automatically updates when more indexes are added.
I find it interesting that two guys that use different languages (php and java) used the same solution for the problem.
As for Java and design patterns and UML, I started in on it and it never really clicked. I also think the programs that I have run have been memory hogs (blame the language or the programmer??) and the enviroments that I have programmed in, low resources have never let me really get into it.
So yes I meant to put the slash not because they are the same but because of Erik’s blog background in Java and the question showing a .jsp file.
Hope that explains it.