jsp question number 2

Discussion in 'OT Technology' started by D1G1T4L, May 3, 2003.

  1. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area
    well no one answered my first one... hopefully someone answers this one


    Code:
    <html>
    <head>
    <basefont face="Arial">
    </head>
    
    <body>
    <%
    // rootshell.jsp
    
    // get the username from the session
    String username = (String)session.getValue("username");
    out.println("<BR><BR><H3>" + username + "<BR><BR></H3>");
    
    // if null, security breach!
    if (username == null)
    {
    	response.setHeader("Location", "start.html");
    }
    else
    {
    // display the protected page
     %>
    
    Welcome to your root shell, <b><%= username %></b>!
    <p>
    Your session ID is <% out.println( session.getId() ); %>
    <p>
    This session will expire in <% out.println(
    session.getMaxInactiveInterval() ); %> seconds.
    
    <%
    }
     %>
    
    </body>
    </html>
    
    so if the user name is NULL, it should redirect the user to start.html page....... but it seemsthat response.setHeader("Location", "start.html"); doesnt do that even though it should

    anyone know why?
     
  2. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area
    never mind i figured it out

    you also have to use
    response.sendRedirect("start.html"); along with response.setHeader("Location", "start.html");
     
  3. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area
    but the Header thing still should have worked without sendRedirect........ dont know why it didnt work
     
  4. CompiledMonkey

    CompiledMonkey New Member

    Joined:
    Oct 26, 2001
    Messages:
    8,528
    Likes Received:
    0
    Location:
    Richmond, VA
    I always just use sendRedirect() in web apps. :dunno:

    Also, using out.print() to display html reminds me of the old days when there were no jsps (even though I wasn't around :o ). I wouldn't do it like that.
     
  5. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area

    how else would you print it out?
     
  6. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area

    sendRedirect doesnt change the URL in the browser though... so you have to use header thing too
     
  7. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area
  8. CompiledMonkey

    CompiledMonkey New Member

    Joined:
    Oct 26, 2001
    Messages:
    8,528
    Likes Received:
    0
    Location:
    Richmond, VA
    Um, yeah it does.

    Code:
    response.sendRedirect("loginError.jsp?id=1");
    
     
  9. CompiledMonkey

    CompiledMonkey New Member

    Joined:
    Oct 26, 2001
    Messages:
    8,528
    Likes Received:
    0
    Location:
    Richmond, VA
    Something similar to this for all of the outs.

    Code:
    <br /><br /><h3><%= username %></h3><br /><br />
    
     
  10. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area

    no i mean it does redirect it but the URL in the browser shows the same link and the <title> doesnt change
     
  11. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area

    is this some standard? not to use out.println and just use %= instead?
    what if you say you want to print out number 5 or some text? dont tell me you going to assign it to a string and then use <%= StringName >
     
  12. CompiledMonkey

    CompiledMonkey New Member

    Joined:
    Oct 26, 2001
    Messages:
    8,528
    Likes Received:
    0
    Location:
    Richmond, VA
    I :dunno: . It always has for me.
     
  13. CompiledMonkey

    CompiledMonkey New Member

    Joined:
    Oct 26, 2001
    Messages:
    8,528
    Likes Received:
    0
    Location:
    Richmond, VA
    You don't have to assign anything, just use a combo of <% or <%= (depending on if its a var or not) to print out stuff.

    Example:
    Code:
    <%
      for(int i = 0; i < 5; i++) {
    %>
        <%= i %>
    <%
      }
    %>
    
    Or, you can just do it the old servlet way like you are now. Either way works.
     
  14. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area

    well i understand that, but lets say u need to print out "hello world"
    u have to use out.println right?
     
  15. CompiledMonkey

    CompiledMonkey New Member

    Joined:
    Oct 26, 2001
    Messages:
    8,528
    Likes Received:
    0
    Location:
    Richmond, VA
    If you know what you need to display, why do it dynamically? Use XHTML, that's why it's there.
     
  16. D1G1T4L

    D1G1T4L Active Member

    Joined:
    May 4, 2001
    Messages:
    16,489
    Likes Received:
    0
    Location:
    Bay Area
    yea lol... you are right

    only i dont know XHTML, only HTML :wtc:
     
  17. CompiledMonkey

    CompiledMonkey New Member

    Joined:
    Oct 26, 2001
    Messages:
    8,528
    Likes Received:
    0
    Location:
    Richmond, VA

Share This Page