#1
  1. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Posts
    21
    Rep Power
    0

    Smile Seperating Java from my JSPs


    Hi

    I have developed jsp files with java embedded in them.

    However, I would like to move the java code out and store it in class files.

    I know nothing about class files and don't know where to start. I don't want to start from the beginning as I can already write basic java but I want to know how to seperate the 2 successfully. Can anybody suggest any good websites that deal with this?
  2. #2
  3. No Profile Picture
    Moderator =(8^(|)
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2002
    Location
    Sacramento, CA
    Posts
    1,710
    Rep Power
    14
    The hard of what you're trying to do will be creating you custom class. Are you familiar with doing this? If not, I'd suggest you read part of Sun's Java tutorial [http://java.sun.com/docs/books/tutorial/java/index.html].

    To include your custom class in the jsp page you'd either use:
    Code:
    <jsp:useBean id="myClass" scope="page" class="com.devshed.MyNewClass" />
    or
    Code:
    <%@ page import="com.devshed.MyNewClass"%>
    <%
    MyNewClass myClass = new MyNewClass();
    %>
    Either way you would then access methods like:
    Code:
    myClass.myMethod();
  4. #3
  5. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Posts
    21
    Rep Power
    0
    Thanks for that. It would seem I can't set session variables in classes, how do I get around this?
  6. #4
  7. No Profile Picture
    Moderator =(8^(|)
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2002
    Location
    Sacramento, CA
    Posts
    1,710
    Rep Power
    14
    Two options.

    Easy way: Use a bean scoped to "session" (Change the scope attribute).

    Harder way:
    Pass the session object to your class when you instatiate it. You could also pass the request object and use request.getSession().
  8. #5
  9. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Posts
    21
    Rep Power
    0
    OK I have changes the scope and added this to page one:

    <jsp:useBean id="belgiumstock" scope="session" class="com.stockoptions.beans.BelgiumStock" />

    belgiumstock.getOptionInfo(session);

    String vEmplid = belgiumstock.getEmplId();
    String vUserid = belgiumstock.getUserId();

    If I want to access the vEmplid / vUserid varibables in page four what code would I need to write (i've tried several things but can't get it to work).
  10. #6
  11. No Profile Picture
    Moderator =(8^(|)
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2002
    Location
    Sacramento, CA
    Posts
    1,710
    Rep Power
    14
    Well, this line
    Code:
    belgiumstock.getOptionInfo(session);
    (at least the session part) shouldn't be necessary.

    By scoping the bean to the session you're basically saying that each page a visitor goes to should get the SAME EXACT BEAN. Every page has to include the <jsp:useBean scope="session"/> tag, of course. Any properties you set on one page will be available on all the other pages. It's exactly the same bean, after all.

    That answer your question? I'm not sure where you're setting the values of vEmplid / vUserid, but once they're set to something they should be the same on every page until you reset them.
  12. #7
  13. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Posts
    21
    Rep Power
    0
    so its necessary to post all of this on each page that refers to the variables?

    <jsp:useBean id="belgiumstock" scope="session" class="com.stockoptions.beans.BelgiumStock" />

    belgiumstock.getOptionInfo(session);

    String vEmplid = belgiumstock.getEmplId();
    String vUserid = belgiumstock.getUserId();
  14. #8
  15. No Profile Picture
    Moderator =(8^(|)
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2002
    Location
    Sacramento, CA
    Posts
    1,710
    Rep Power
    14
    Pretty much.

    You could also make another jsp that handles the logout. Call whatever bean methods you need then call session.invalidate().
  16. #9
  17. No Profile Picture
    Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Posts
    21
    Rep Power
    0
    OK thanks for your help.

IMN logo majestic logo threadwatch logo seochat tools logo