Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Dynamic JSP

 
Ranch Hand
Posts: 2259
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
I need some direction. I need to know how to accomplish a part of my web app design. I have a JSP with static data and butttons. I will use some of the buttons to perform operations on the data. I need these buttons only accessible to certain types of users. I have the user type determined in a connection class as a variable. Do I try and make the JSP dynamic or do I need to create a JSP for each type of user?

Please help.
 
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
It's quite easy to make role-based decisions in a JSP. It could be something as simple as:



is that the sort of thing you are asking about?
 
Steve Dyke
Ranch Hand
Posts: 2259
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Yes this is what I need. However, how do I declare my variables in my classes so that the JSP's can use them.

My app works like this. The user types in id and password on a JSP, a servlet fires a class to connect to an AS400 file that holds user data. In this class(Code below) I assign values to variables. Then a main menu pops up(JSP). The user can now choose to open a engineering drawing data inquiry form(JSP). It is this form I need to control by user type.

 
Bear Bibeault
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Before your servlet controller forwards to its JSP it can place any construct it likes in the request as scoped variables that can be accessed within the JSP.

OR...

When the user logs in, a user object is placed in the session which contains the information for the current user. This can include information on what role or permissions the user posesses. This is usually the strategy I use rather than looking such stuff up on a page by page basis.

You might find this article helpful. Even though I wrote it to concentrate on how to use Maps with scriptless JSPs, one of the examples I employed fits nicely into this discussion.
[ May 09, 2007: Message edited by: Bear Bibeault ]
 
Steve Dyke
Ranch Hand
Posts: 2259
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
First thanks for helping me. I am very unlearned in this area but my boss wants results. The second suggestion looks the best. How can I set up a User class based on the data received in my connection class?
 
Steve Dyke
Ranch Hand
Posts: 2259
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
This does not do anything eventhough a System.out.println(MainMenuServlet.userTypeString) produces "Administrator" in the calling Servlet.

 
Bear Bibeault
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

<c:if test='$(MainMenuServlet.userTypeString == "Administrator")'>



What is MainMenuServlet.userTypeString?

It doesn't look like anything that a JSP will have access to.

In order for data to be accessible from a JSP, it must be placed as a scoped variable on one of the context scopes. For example, to set a variable in request scope you'd use the request.setAttribute() method.
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
If you want a short and simple example of a servlet that binds a bean to one of the scope objects in order to make its properties available to a JSP, see:
http://simple.souther.us

Look for SimpleMVC.

This example was written to work with older versions of the JSP spec.
If you're using a JSP 2.0 compliant container, you won't need the <jsp:useBean .../> tag and you can use the cleaner ${bean.property} syntax instead of the <jsp:getProperty .../> tags employed in the example.

Either way the servlet code and the general idea will be the same.
 
Bear Bibeault
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Sounds like it's time to update your excellent example code Ben!
 
Steve Dyke
Ranch Hand
Posts: 2259
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
This is my servlet code:



This is my JSP code:

 
Steve Dyke
Ranch Hand
Posts: 2259
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
How do I get this into a session attribute?

DrawingConnectionClass conn = new DrawingConnectionClass(drawingnumber);
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Originally posted by Bear Bibeault:
Sounds like it's time to update your excellent example code Ben!



Can't decide when to upgrade it to ONLY work with JSP2.0.
A lot of commercial app servers are still not fully 2.0 compliant.
 
Steve Dyke
Ranch Hand
Posts: 2259
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Please look at my JSP code and tell me why this part is not working.

 
Bear Bibeault
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Originally posted by Ben Souther:
Can't decide when to upgrade it to ONLY work with JSP2.0.
A lot of commercial app servers are still not fully 2.0 compliant.



"Lead and they shall follow"

[BPS: DONE]
[ May 10, 2007: Message edited by: Ben Souther ]
 
Bear Bibeault
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Originally posted by Steve Dyke:
Please look at my JSP code and tell me why this part is not working.



Based upon your other post, I'd quess that userType does not contain what you think it contains.

By the way, not a good idea to spread the same issue across multiple posts...
 
Steve Dyke
Ranch Hand
Posts: 2259
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Sorry about that. I am desprate. No matter what I try it fails. As I have posted my servlet and jsp code you can see my jsp has:



This displays "Administrator" as big as day. Yet the EL does not recognize it for some reason.
 
Bear Bibeault
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
What does the renderred HTML look like? Are the JSTL and EL tags getting properly executed or are they showing up the HTML?
 
Steve Dyke
Ranch Hand
Posts: 2259
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
If you are asking if the HTML page displays properly it does. I did a cpoule of test one was javascript and an alert which showed "Administrator" as the content of the userTypeBean.userType. Also I changed the value of the userTypeBean.userType to "true" and took ot the condition part of the EL and it worked.

In your last post you said maybe what is in the userTypeBean.userType was not what I thought. Well I am having the same issues with:



The:



Produces "Administrator"

But:



Never finds a match.

Again thanks for all the help.
 
Bear Bibeault
Sheriff
Posts: 67752
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

But:

if(userText.getUserType().equals("Administrator")){

Never finds a match.

This clearly demonstrates that this is not an EL or even a JSP problem.

getUserType() is simply not returning the string "Administrator".
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Having looked at your other post:
https://coderanch.com/t/407022/java/java/Help-Me-Understand-Comparisons
I think your best bet, right now is to focus on the core Java issues (understanding the .equals() method and how and when to override it (and possibly the toString method) before trying to use these objects in JSP/Servlet application.

As Bear has mentioned it's getting very confusing, both for you and the people trying to help you now that the issues are spread out over multiple threads.
I'm going to close this thread.
Once you have a grasp on building objects that can be compared, you probably won't need any help on the JSP/Servlet end, but if you do, feel free to start another thread in the appropriate forum.

-Ben

[Bonk]
[ May 10, 2007: Message edited by: Ben Souther ]
 
Arch enemy? I mean, I don't like you, but I don't think you qualify as "arch enemy". Here, try this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
    Bookmark Topic Watch Topic
  • New Topic