We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,555 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

JSP or Servlet as Home Page

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...

3
Contributors
5
Replies
1 Week
Discussion Span
1 Year Ago
Last Updated
6
Views
Question
Answered
autorunman22
Light Poster
43 posts since May 2011
Reputation Points: 13
Solved Threads: 3
Skill Endorsements: 0

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

javaAddict
Nearly a Senior Poster
Team Colleague
3,338 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 450
Skill Endorsements: 7

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.

autorunman22
Light Poster
43 posts since May 2011
Reputation Points: 13
Solved Threads: 3
Skill Endorsements: 0

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.

javaAddict
Nearly a Senior Poster
Team Colleague
3,338 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 450
Skill Endorsements: 7

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.

stultuske
Industrious Poster
4,493 posts since Jan 2007
Reputation Points: 1,377
Solved Threads: 630
Skill Endorsements: 25

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..

autorunman22
Light Poster
43 posts since May 2011
Reputation Points: 13
Solved Threads: 3
Skill Endorsements: 0
Question Answered as of 1 Year Ago by javaAddict and stultuske

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page generated in 0.0848 seconds using 2.77MB