Hello all! I'm having some confusions about what to use as the "Home Page" in my web app.
Should I use Servlet?(www.foo.com/index) or JSP?(www.foo.com/index.jsp)
I dont know what would be their entry point in my site. I mean, let say, they visit www.foo.com/index.jsp, and they click login, the page then redirects to /index?id=login
(servlet) the servlet then query to the database, and then dispatch to the index.jsp.
But my "index.jsp" file has a lot of IF-Statements which is I don't think a good practice for an MVC Design.

if session is null then say youre not login
else welcome user
if request.getAttribute("username") is not null then
etc. etc.

sorry for my bad english guys! i just hope you get what i mean. Thanks...

Recommended Answers

All 5 Replies

You don't have to put all those ifs in the index jsp because what you are trying to do should be divided into 2 jsps.

Assuming that your first jsp is called: login.jsp. There you don't have to check if the user is logged in. All you have to do is have the login form. You can also have a message displayed if it is not null:

String message = (String)request.getAttribute("message");

if message is not null display it

- login form - with input fields and a submit button

Then submit to your login Servlet. If the user name is not correct, fill the appropriate message:

request.setAttribute("message", "Invalid username, password");

and redirect back to the login.jsp

If the username password are correct then redirect to the main page of your application (welcome page).
In there you will get the username from the session and check if it is null.

So you will not have many ifs in one page. One page for the login. One servlet for validation. Then depending on the results, go back to the login page or go to your main page

Thanks javaAddict, but what im trying to do is to put them all in one/single page "index.jsp" like other sites do. index.php?id=1, index.php?id=2, index.php?id=4, index.php?id=6, etc.

If you want to put them all in one jsp then you will need a lot of ifs. And you will end up with this:

if (id==1) {
 // some html code
} else if (id==2) {
  // some other code
}

And you will end up having a lot of different pages in one file. But you said yourself that you don't want to do that so it's up to you.

another way it can be done is having a lot of different div's with all their id's set (by css) to display: none;
and then, when have to be shown, have your javascript set the right id to display: block. but javaAddict is right: it'll become a huge pile of code which is very hard to maintain.

you all right guys! thanks! I only want to know if you're all doing the same thing :D now i know that every action must have a different files..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.