|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Generate data entry and reporting .NET Web apps in minutes, straight from your database. Read our FREE whitepaper “Build Web 2.0 Applications Without Hand-Coding” Download now! |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
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(); |
|
#3
|
|||
|
|||
|
Thanks for that. It would seem I can't set session variables in classes, how do I get around this?
|
|
#4
|
|||
|
|||
|
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(). |
|
#5
|
|||
|
|||
|
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). |
|
#6
|
|||
|
|||
|
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. |
|
#7
|
|||
|
|||
|
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(); |
|
#8
|
|||
|
|||
|
Pretty much.
You could also make another jsp that handles the logout. Call whatever bean methods you need then call session.invalidate(). |
|
#9
|
|||
|
|||
|
OK thanks for your help.
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Java Help > Seperating Java from my JSPs |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|