Categories
java

How Would You Implement This…

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…

6 replies on “How Would You Implement This…”

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.

Hey Kevin,

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.

Erik

I’m not big on javascript/java and stick more to php so I would do this. And actually I used pretty much the same thing to randomize pictures on a website.

x = rand(1,4);
.
.
.
echo(“index.php?which_index=x”);

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.

i.e.
count = Parse_dir();
x = rand(1, count):

That way it automatically updates when more indexes are added.

Hey Mark…the fact that you lump javascript and java together with a slash tells me that probably don’t have enough of a clue to say you’re ‘not big on’ either technology. It makes me think you’re just another PHP guy that assumes he doesn’t like java because that’s what all the other PHP guys are saying. However, I try to keep an open mind. I’m sure this was just a typo and you’re really a great guy.

Vu – You’re way off. Did alot of javascript at my last job along with coldfusion and php. I’m not a big fan of anything that runs client side since the user can just shut if off. But I did spend two years using it and was even sent for classes; not my choice given my background with C, Ada, Mathlab just to name a few languages. Yes you can alert them (user) that they have to allow it but still that’s not something I like to see happen.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *