ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Hiding a link in JSP page

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Hiding a link in JSP page

    Good day,

    I have the following JSP code insert.
    My objective is to show the link in the page, only when the user is logged on.
    I am testing as follows, but get the link displayed at all times followed by JSP closing bracket
    Code:
     %>
    Any suggestions, please.

    Thanks you

    Code:
    <%
    String space = " " ;  
       // test if user is loged on and only show link if user is logged on 
      //  if (nullInd != request.getSession().getAttribute("mySavedUserid") ) 
       
     if (space != request.getSession().getAttribute("mySavedUserid") )
           
         {%>
    
           <a href="http://myurl.com?USRNAM=<%=session.getAttribute("mySavedUserid")%>" target="_new"><img src="webfacing/images/IBM/systemscreens/print.jpg" hights="50" width="60">View User Spool Files</a>
       <%}; %>

  • #2
    Re: Hiding a link in JSP page

    Hi,
    noting wrong with your code, check it again & will work

    Code:
    if (space != request.getSession().getAttribute("mySavedUserid") )
           
         {%>
    
         <p>  <a href="http://myurl.com?USRNAM=<%=session.getAttribute("mySavedUserid")%>" target="_new"><img src="theme/images/logo.jpg" 
           hights="50" width="60">View User Spool Files</a></p>
       <%}; %>
    Last edited by dhanuxp; September 2, 2009, 09:26 PM.

    Comment


    • #3
      Re: Hiding a link in JSP page

      getAttribute() returns null if no value is associated with the given name (not a blank space...I'm not aware of any Java functions that return a single blank space...). Try removing the silly space variable and doing something like the following:

      PHP Code:
      <%
      String user request.getSession().getAttribute("mySavedUserid");
         
       if (
      user != null)
             
           {%>

             <
      a href="http://myurl.com?USRNAM=<%=user)%>" target="_new"><img src="webfacing/images/IBM/systemscreens/print.jpg" hights="50" width="60">View User Spool Files</a>
         <%}; %> 

      Comment


      • #4
        Re: Hiding a link in JSP page

        RPGnoobie,
        I have tried to code as you have suggested.
        It gives a compile error "Type mismatch, cannot convert from Object to String". This is the reason I have defined "space" variable.
        You are RIGHT about null. The method returns null.
        Original question is resolved
        Thank you

        I still have to code :
        String varNull = null;
        if (varNull != request.getSession().getAttribute("mySavedUserid") )

        I get a closing JSP bracket displayed on screen. How can I avoid it?

        Thank you
        Lenny

        Comment


        • #5
          Re: Hiding a link in JSP page

          It gives a compile error "Type mismatch, cannot convert from Object to String".
          This is easy to fix without resorting to inelegant workarounds. It is possible to store any type of object in a session, not just strings! This is possible because all Java objects descend from the type Object.

          If you look at the method setAttribute, it receives a String name and an Object value.

          When you store the "user" String in the session it will be cast to an Object. If this didn't happen then you would need separate methods for each type that you would want to store in the session. Every time you write a new class you would have to add a method to the session to be able to save it!

          When doing the reverse, getAttribute returns an Object. To resolve the type mismatch you just cast it back to a string.

          PHP Code:
          String user = (String) request.getSession().getAttribute("mySavedUserid"); 
          It's completely unnecessary to create a variable just containing a null value. If all you want is to test if "user" is not null then try the following.

          PHP Code:
          if (user){...} 
          I'm not sure why you see the extra bracket on your screen but it may or may not have something to do with the unnecessary semi-colon after the if brackets.

          Perhaps it should look something like this?
          PHP Code:
          <%
          String user = (String) request.getSession().getAttribute("mySavedUserid");
          if (
          user)
          {
               %>
          <
          a href="http://myurl.com?USRNAM=<%=user%>" target="_new"><img src="webfacing/images/IBM/systemscreens/print.jpg" height="50" width="60" />View User Spool Files</a>
               <%

          %> 
          This style of JSP syntax is a bit out of date now. I would look at the JSP 2.0 syntax using the EL (expression language). This combined with a good MVC framework will look much neater and give more benefits.

          Then it becomes something like this. This might not be exactly right as I'm a bit rusty.
          PHP Code:
          <%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
          <
          c:if ${sessionScope.mySavedUserid != null}>
             <
          a href="http://myurl.com?USRNAM=${sessionScope.mySavedUserid}target="_new"><img src="webfacing/images/IBM/systemscreens/print.jpg" height="50" width="60" />View User Spool Files</a>
          </
          c:if> 
          It sounds to me like you need to buy yourself a good book on Java and learn the language first. The type mismatch and if statement problems are quite rudimentary and ought to be covered in the first few chapters of any good Java book. Trying to code around the language will cause you a mountain of problems later. The head first books are all excellent and I would say you need one on the language itself and one on JSP and servlets.
          Ben

          Comment


          • #6
            Re: Hiding a link in JSP page

            Ben,

            Thank you very much.. I searched the code for all closing script bracket and found that RAD IDE inserted one next to closing html tag at the end of page.
            Your comments are greatly appreciated and are right on the money. I am a cobol developer and the syntax is not natural to me.

            Thanks,
            Lenny

            Comment


            • #7
              Re: Hiding a link in JSP page

              Good luck with it. Secretly I'm a bit jealous!

              Java is my favourite of all the languages I've worked with. It can have a bit of a learning curve but I found it relatively easy to pick up given a good book, of which there are hundreds! Equally there are some good tutorials online that may help to learn the basics. I find books are usually more detailed and explain more of the subtle nuances of a language.
              Ben

              Comment


              • #8
                Re: Hiding a link in JSP page

                Thank you Ben.
                Honestly, I am a bit frastrated. I am the only developer in the company... (Forums and books is the only source).
                I started converting iSeries 5250 using Webfacing.
                Webfacing has a "mind of it's own"
                Now, I have projects containing hundreds of files that webfacing generated. Anytime I need a modification, it becomes a complexed project even for the minor thing.
                I found that there are many Frameworks used today.
                Could you please advise as to which framework is best suitable for iSeries and relatively easy to learn and utilize.

                Thanks,
                Lenny

                Comment


                • #9
                  Re: Hiding a link in JSP page

                  You mean Java web frameworks? There are hundreds of those as well! There's no right or wrong one to use. It depends on your business needs. Having said that my favourite is spring MVC. If you're not a fan of writing your own HTML etc then GWT is pretty nifty.

                  The good thing with spring is that you can use as little of it as you want to begin with and then use more and more as you get more confident with it.
                  Ben

                  Comment


                  • #10
                    Re: Hiding a link in JSP page

                    Thank you Ben I will look into Spring

                    Please, kindly look at this statement:

                    String testUser = (String) request.getSession().getAttribute("mySavedUserid") ;
                    [9/9/09 12:31:37:696 EDT] 0000002a SystemOut O testUser =

                    As seen from SystemOut, it returns testUser that is
                    not "null". Neither is it space.
                    What value is it? I need to code for "THAT" value, so that a certain action takes place when returned testUser is "not null" and "NOT THAT"

                    Thank you

                    Comment


                    • #11
                      Re: Hiding a link in JSP page

                      Could you post the exact details please. I would need to see the exact lines of code used and the output generated.

                      I'm not sure what this bit:
                      [9/9/09 12:31:37:696 EDT] 0000002a SystemOut O testUser =
                      ... is all about?
                      Ben

                      Comment


                      • #12
                        Re: Hiding a link in JSP page

                        Ben,

                        Original objective was to hide a link if user is not loged on.
                        I used setSession().setAttribute() and getSession.getAttribute() methods to save the userid and get it back respectively.

                        In the case where user would not logon and clisk "cancel" testUser came back as what I thought was space.
                        but when I coded:
                        Code:
                          if ( (null != testUser) && (testUser != " " )
                                           "show link";   )
                        my link was showing on page. This left me guessing...

                        I used a length() method to test if user keyed data.
                        For the time user clicked "cancel" at logon time, testUser.length() was zero.
                        I coded this and it now works fine:
                        Code:
                          if ( (null != testUser) && (testUser.length() != 0 ) )
                        Thank you!

                        Comment


                        • #13
                          Re: Hiding a link in JSP page

                          PHP Code:
                          testUser != " " 
                          This is a classic Java beginners mistake. That code is checking to see if the two String objects are the same, rather than checking their respective values. It is possible to have two strings with the same value that are not the same object. Instead you should use the equals method of the string class.

                          I would have written your condition like this:
                          PHP Code:
                          if(testUser!=null && !testUser.equals("")) 
                          I'm afraid I gave you a bumsteer saying you could just use the object name in the if condition. I've been coding JavaScript mostly this last month where that is legal syntax.

                          Bit like how I always make this mistake when switching from RPG to Java.
                          PHP Code:
                          while(condition);{...} 
                          Ben

                          Comment

                          Working...
                          X