aspose file tools*
The moose likes JSP and the fly likes Sessin Not Working Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Sessin Not Working " Watch "Sessin Not Working " New topic
Author

Sessin Not Working

tangara goh
Ranch Hand

Joined: Dec 27, 2009
Posts: 125
Hi Expert out there,

I'm trying to create session.jsp to allow 2 types of users to accss certain pages only. Admin can access update.jsp whereas members can access addmember.jsp. If you are neither both, then you will go to the log in page. But, my program is not running.

Hope the experts here can advise me what's wrong with my codes. tks.

my session.jsp is:-


my login page:-



my database field is userid, password and access.

access will indicate what kind of user.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I'm not even sure where to start, to be honest.

No imports? Scriptlets? Select *? out.println? ItDoesntWorkIsUseless? Take your pick, and we'll begin.
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 645
I have a few comments/questions which I hope will be helpful.

In your session.jsp, what are you doing with the database lookup? You create a query and a Result Set, but then don't do anything with it. It is just ignored.

At line 21 of session.jsp, you're setting accessType to the value of a session attribute named login, but this session attribute was never set (as far as I can tell from the code shown).

The login.jsp page includes an input parameter named login, which I think may be confusing you. Note that when the user enters a value for the login field and submits the form, the action page (session.jsp in this case) can access that parameter value using request.getParameter("login"), not as a session attribute (unless/until it is assigned as a session attribute, of course).

It is my guess that you expected accessType to be set to the access column from the database based on the user's provided login name, but you have no code to accomplish this.

I'm guessing what the problem is, but actually, you never said. You just said that your application is not running. What does this mean? For example, is your computer plugged in? It is very helpful if you explain clearly what you expect and what you are getting. If you're getting an error message, make sure you provide full details of that as well.

As for security, this is a non-standard way to deal with it. Normally, you would employ a JAAS security module and use role-based security to limit access to application resources to authorized uses. Do you have a reason for wanting to roll this yourself, rather than using a more industry-standard method?

Finally, it is very bad form to combine application login and the display page. You should have a look at the Model View Controller (MVC) design pattern (see the Servlets FAQ on this site) and create a servlet to manage the application logic and use the JSP page strictly for displaying information to and getting input from the user.

Here is a link to the Servlets FAQ: http://faq.javaranch.com/java/ServletsFaq
Here is a link to the JSP FAQ: http://faq.javaranch.com/java/JspFaq
And here's a great article by Bear which includes among other things, the reasons why we want to separate application and display logic: http://www.javaranch.com/journal/200603/Journal200603.jsp#a5

Best Regards,


tangara goh
Ranch Hand

Joined: Dec 27, 2009
Posts: 125
Hi David,

I've added in this line :

<%@page language="java" import ="java.sql.*" %>

and changed the out.println to redirect.

Not sure why you mentioned select * ?

I thought I need to select * from my database table in order for the session to map to the access type?

The programme is not working. It returns a 404 error. Hope you can give me your expert view.

Tks.
tangara goh
Ranch Hand

Joined: Dec 27, 2009
Posts: 125
Mark E Hansen wrote:I have a few comments/questions which I hope will be helpful.



It is my guess that you expected accessType to be set to the access column from the database based on the user's provided login name, but you have no code to accomplish this.




Is this the way to do it? Kindly advise please.
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 645
tangara goh wrote:
Mark E Hansen wrote:I have a few comments/questions which I hope will be helpful.



It is my guess that you expected accessType to be set to the access column from the database based on the user's provided login name, but you have no code to accomplish this.




Is this the way to do it? Kindly advise please.


Of course not. That's not even valid Java. I hope you won't take this the wrong way, but you simply can't learn Servlets and JSPs by trying random logic until you find some that work. I would recommend that you have a look at some tutorials available on the Internet which would give you a great start. There is a great book named Core Servlets and JavaServer Pages, which is available for free download from the author's web site here: http://pdf.coreservlets.com/. Note that on that page you will also have access to various tutorials, etc. These helped me a lot when I was learning the topics.

Best Regards,
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61434
    
  67

David's point is that you are using so many poor practices that it's not surprising that you are having problems. "Best practices" come about because developers have learned that there are ways to do things that create the least amount of problems, and that most people become familiar with so that we can understand each others code better.

For example, you have a JSP that is completely Java code! Given that it's considered an extremely poor practice to have any Java code in a JSP, do you really think that having a JSP that is nothing but Java code is a good idea?

Another example: performing JDBC calls in the UI is a poor practice. You should be following a Model 2 MVC-patterned structure for your program. Doing database calls in a JSP is about as far away from that as you can get.

You want help from the experts. The best expert advice you will get is not how to fix this specific problem, but how to write your code better so that you avoid problems like this altogether.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61434
    
  67

One more thing, programming is all about attention to detail and accuracy. Please show as much care when posting to the forums as when writing code. In your subject, "Sessin" is misspelled. Please take more care.
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 645
In fact, I would ask the OP what you are actually trying to do. Are you really trying to solve a specific problem or are you using this problem as a means to learn JSP programming?

If you're goal is to learn JSP programming, please have a look at the available training resources and learn to do it the proper way. As with any language, you can use it properly or improperly. If you choose the dark path, you will certainly be unhappy in the long run.

Best Regards,
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61434
    
  67

That's a good point Mark.

A lot of the time, people start off learning things "the easy way" figuring that they can learn the "right way" later. But that rarely happens. Habits learned early get hard to break.

It's best to start off on the right foot, even if t takes a little longer to get off the runway.
tangara goh
Ranch Hand

Joined: Dec 27, 2009
Posts: 125
Dear Experts,

Thanks for all the suggestion. It's just that I'm trying to complete a project which is due this week.

Hence, I may not have the time to do the MVC thing.

I just discovred a MVC example and am trying to go through the example. However, I'm not able to create these codes in my netbean IDE:-



The public class sould be public interface MailingBean{

How do I make the netbean IDE create public interface?

OK. Hope to hear from you soon. Really want to experience what this MVC is about, though I don't know how it can apply in my project and the session portion.

Tks.
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 645
How to change it from a Class to a Interface? Can't you just change "class" to "interface"? Otherwise, you may have to ask that question on the IDE forum, as I don't use that IDE.

As far as the MVC issue goes, the point is to separate the Java code implementing the application logic from the HTML markup used to display information to the user or get input from the user.

To implement this, you have a Servlet that deals with the requests from the browser, and one or more JSP pages that deal with the generation of the HTML pages. Really, the JSP page should look like an HTML page with some optional JSTL/EL tags in it. No scriptlets at all. No Java code at all.

The article written by Bear (which I referenced earlier) really does a good job of explaining how this design pattern (MVC) plays into the whole Java web application thing, using Servlets and JSPs.

As far as what you're trying to do with Security, I would ask that you go back and read my comments on the topic and address the questions there, so I don't repeat them over and over again.

One question you really need to ask yourself, though, is how are you expected to create a web application using Servlets and JSPs without understanding how to use Servlets and JSPs? I really don't understand how anyone can expect to do that. I certainly couldn't. Why are you expected to?

I hope you're able to find what you need to get past your deadline.

tangara goh
Ranch Hand

Joined: Dec 27, 2009
Posts: 125
OK. I'll give it a try. Just that the sesion thing, I'm still blur about it.....
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 645
tangara goh wrote:OK. I'll give it a try. Just that the sesion thing, I'm still blur about it.....


I think the very first chapter of Core Servlets and JavaServer Pages will explain it much better than I could in a simple posting here. Have a look at it (remember, free download as PDF from the book's website).

Once you do, I think the answers you're getting here will make much more sense.

Best Regards,
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sessin Not Working