[opencms-dev] Java beans / Module development
visser arthur (02VOEMDIAHTKAND)
arthur.visser at student.groept.be
Thu Mar 10 15:25:37 CET 2005
My system : OpenCMS 5.0.1 - Tomcat 5.0.18 - Java 1.4.2 - XP Home SP2 - MySQL 4.0.22
I am developing a dynamic website using the software tools mentioned above. I have a database in MySQL that I can successfully access via my JSP pages.
Since I intend to develop a seperate (java)module with some business logic in it I started of by creating a LOGIN page using JSP, declaring session attributes for LOGIN and password. next I create a very simple Java Bean in which I can SET and GET the login parameters. So far, so good. this works.
The next step is to access the SAME instance of the parameters in the Java Bean with a seperate Java Program. This program will then use the login data to query the database and create JPanel to present results etc.
The communication bewteen the Bean and the JSP works perfectly. however, when I write a routine to access the Bean (GET) from the Java program it tells me to make the method in the Bean static (and thus declare static Strings). At this time the JSP <> Bean comm. fails again, so I keep going round in circles!
Can anyone give me a tip on how to make this work? I can of course do my database queries via a JSP pages, but the intention was to create a seperate Java module that can be exported and imported.
Below you'll find the code I am using for thr JSP, the Bean and the Java program (partly).
Thanks in advance!!
JSP page : session attributes are requested (OK)
<%
String user = new String();
String code = new String();
user = (String) session.getAttribute("user");
code = (String) session.getAttribute("code");
out.println("U bent aangemeld als:" + user);
%>
JSP page : session attributes (above) are SET in java bean (Userdata.class) using setProperty (OK):
<jsp:useBean id="gebruik"
class="com.opencms.beans.UserData">
<jsp:setProperty name="gebruik" property="*" />
<jsp:setProperty name="gebruik" property="userNaam" value="<%= user %>" />
<jsp:setProperty name="gebruik" property="userCode" value="<%= code %>" />
</jsp:useBean>
JSP page: session attributes are read (GET) from java bean using getProperty (OK):
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="249" height="55" align="right"
valign="center"><jsp:getProperty name="gebruik"
property="userNaam"/></td>
</tr>
<tr>
<td width="249" height="55" align="right"
valign="center"><jsp:getProperty name="gebruik"
property="userCode"/></td>
</tr>
</table>
UserData.class java bean code (OK) :
package com.opencms.beans;
import java.io.Serializable;
public class UserData implements Serializable {
public UserData() {
String user = getUserNaam();
String code = getUserCode();
}
private String userNaam = ("Naam");
private String userCode = ("Code");
public String getUserNaam() {
return userNaam;
}
public void setUserNaam(String userNaam) {
this.userNaam = userNaam;
}
public String getUserCode() {
return userCode;
}
public void setUserCode(String userCode) {
this.userCode = userCode;
}
}
First lines of code of the Java file. This file needs the same parameters (user & code) as exchanged between the Java Bean and JSP. This code doesnt work. The return (controle met System.out.println(user);) will always give the static default String value Naam (see bean) :
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.io.Serializable;
import com.opencms.beans.UserData;
public class Resultaten implements Serializable{
private Overzicht overzicht;
public static void main(String[] args){
UserData gebruiker = new UserData();
String user = gebruiker.getUserNaam();
String code = gebruiker.getUserCode();
System.out.println(user);
Overzicht overzicht = new Overzicht(user,code);
}
public Resultaten(String user, String code){
overzicht = new Overzicht (user, code);
}
}
class Overzicht {
//System.out.println(userNaam);
PreparedStatement pstmt = null;
String sexe = new String();
String aanhef = new String();
boolean LoginOK = false;
public Overzicht (final String user, final String code){
try {
More information about the opencms-dev
mailing list