File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes Dynamic JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Reply locked New topic
Author

Dynamic JSP

Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1476
    
    1
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.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61611
    
  67

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?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1476
    
    1
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61611
    
  67

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

Joined: Nov 16, 2004
Posts: 1476
    
    1
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

Joined: Nov 16, 2004
Posts: 1476
    
    1
This does not do anything eventhough a System.out.println(MainMenuServlet.userTypeString) produces "Administrator" in the calling Servlet.

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61611
    
  67

<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.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61611
    
  67

Sounds like it's time to update your excellent example code Ben!
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1476
    
    1
This is my servlet code:



This is my JSP code:

Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1476
    
    1
How do I get this into a session attribute?

DrawingConnectionClass conn = new DrawingConnectionClass(drawingnumber);
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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

Joined: Nov 16, 2004
Posts: 1476
    
    1
Please look at my JSP code and tell me why this part is not working.

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61611
    
  67

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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61611
    
  67

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

Joined: Nov 16, 2004
Posts: 1476
    
    1
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61611
    
  67

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

Joined: Nov 16, 2004
Posts: 1476
    
    1
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61611
    
  67

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

Joined: Dec 11, 2004
Posts: 13410

Having looked at your other post:
http://www.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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dynamic JSP