Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

New to JSP

Status
Not open for further replies.

tydavis

Programmer
Dec 14, 2004
6
US
Hello,

I am new to JSP processing and am having a problem with my java program.

I cannot get the JSP to pass the java program.

Can I email the source to someone to see if they can pont me in the right direction?

ANY help would be MOSt appreciated ...

Thank You.
 
Not quite sure what you mean by "JSP to pass the java program" ... could you explain a little more and post a small example of what you are doing, and of course the errors you are getting.

--------------------------------------------------
Free Database Connection Pooling Software
 
Sorry,

Meant to say pass the "parm"(html text field) to java program from either an HTML or JSP page... Im kinda new to both java and JSP, Im an AS/400 RPG programmer.

I have enclosed 2 java programs and 1 JSP. The code is generated by WebSphere.

The JSP "usebeans" the first Java program and that inturn calls a JDBC connection java program. I orginally had the 2 java programs combined but then broke them out.

ANY help would be MOST appriciated ...
Thanx in advance.

Ty Davis
Mobile, Al.



......... JSP ..............

<% if(request.getProtocol().equals("HTTP/1.1")) {
response.setHeader("cache-control", "no-cache");
}
response.setHeader ("pragma", "no-cache");
response.setHeader ("expires", "0");
%>

<jsp:useBean id="inq" class="grmstrInquiry.grmstrInquiry" scope="request">
<jsp:setProperty name="inq" property="incode" param="incode"/>
</jsp:useBean>

<H1 align="center" >Autry Greer and Sons, Inc.</H1>
<FORM NAME="RpgInq" method="post" action="
<%if(request.getParameter("RtnCd") != null ) { %>

Desc: <%=inq.getOutdesc()%> <br>
Pack: <%=inq.getOutpack()%> <br>
Size: <%=inq.getOutsize()%> <br>
RtnCd:<%=inq.getRtncd() %> <br>
<%
}
%>
<P align="center">Enter Greer Code </P>
<P align="center"><INPUT type="text" name="incode" maxlength="6" ></P>
<P align="center"><INPUT type="submit" name="sbmBUTTON"></P>
</FORM>
</BODY>
</HTML>
..........Main Java Program (called from usebean).........
/*
* Created on Dec 3, 2004
*
* To change the template for this generated file go to
* Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
*/
package grmstrInquiry;

/**
* @author tydavis
*
* To change the template for this generated type comment go to
* Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
*/
public class grmstrInquiry {

public String incode;
public String outdesc = null;
public String outpack = null;
public String outsize = null;
public String rtncd = null;
public grmstrConnect bean = null;

public grmstrInquiry()
{
getIncode ();
bean = new grmstrConnect(incode);
getBean();
System.out.println("grmstrConnect inParms " + incode);

}
/**
* @return
*/
public grmstrConnect getBean() {
return bean;
}

/**
* @return
*/
public String getIncode() {
return incode;
}

/**
* @return
*/
public String getOutdesc() {
return outdesc;
}

/**
* @return
*/
public String getOutpack() {
return outpack;
}

/**
* @return
*/
public String getOutsize() {
return outsize;
}

/**
* @return
*/
public String getRtncd() {
return rtncd;
}

/**
* @param string
*/
public void setIncode(String string) {
incode = string;
}

}


............ JDBC(AS/400) Connection Program...............

package grmstrInquiry;
import java.sql.*;

/* To change the template for this generated type comment go to
* Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
*/

public class grmstrConnect {

public String incode;
public String outdesc = null;
public String outpack = null;
public String outsize = null;
public String rtncd = null;
private Connection conn = null;
private CallableStatement rpg = null;

public grmstrConnect (String incode) {

String outdesc;
String outpack;
String outsize;
String rtncd;

try {
Class.forName("com.ibm.as400.access.AS400JDBCDriver");
System.out.println("Driver Loader Ran Sucessfully");
}
catch(ClassNotFoundException e) {System.out.println("JDBC Driver Not Found:" + e);
}

try {
conn = DriverManager.getConnection("jdbc:as400://192.168.1.140", "XXXXXXXX", "XXXXXXXX");

rpg = conn.prepareCall("CALL TYDAVIS.GETGRMSTR (?,?,?,?,?)");
System.out.println("PrePare Call Ran Sucessfully");

rpg.setString(1, incode);
System.out.println("Register inParms Ran Sucessfully");

rpg.registerOutParameter (2, java.sql.Types.CHAR);
rpg.registerOutParameter (3, java.sql.Types.CHAR);
rpg.registerOutParameter (4, java.sql.Types.CHAR);
rpg.registerOutParameter (5, java.sql.Types.CHAR);

System.out.println("Register outParms Ran Sucessfully");

rpg.execute();


System.out.println("Stored Procedure Ran Sucessfully");
}

catch (SQLException e) {
System.out.println("Sql error creating Call statement: " + e);
}

}
/**
* @return
*/
public String getIncode() {
return incode;
}

/**
* @return
*/
public String getOutdesc() {
return outdesc;
}

/**
* @return
*/
public String getOutpack() {
return outpack;
}

/**
* @return
*/
public String getOutsize() {
return outsize;
}

/**
* @return
*/
public String getRtncd() {
return rtncd;
}

/**
* @param string
*/
public void setIncode(String string) {
incode = string;
}

}
 

Sorry forgot to include what's wrong :-\

The java program in the usebean is getting a null value from the JSP.

 
Well, your code's flow will currently be this :

Load JSP
Load your bean, (and call the default constructor)
Invoke your JDBC connection code to connect to the db.
Do your SQL update.
Close db access.
Set the "incode" value in the bean.
Done.

This is all done by these lines :
Code:
<jsp:useBean id="inq" class="grmstrInquiry.grmstrInquiry" scope="request">
<jsp:setProperty name="inq" property="incode" param="incode"/>

So of course the incode value is null - because you call the db SQL before you have set it !



--------------------------------------------------
Free Database Connection Pooling Software
 

Then I dont understand how the property gets set?

I thought that the usebean/setproperty set the value on initiation, after the submit button is pressed?

 
Well yes it does - but in order to create your bean, a constructor must be called - and you have made the mistake of doing all your business transactional logic within the constructor.

What you should have is your bean is just the data holder - and once set, you should call some kind of helper class that calls the database to do the desired transaction, which you pass in the data you wish to transact.

--------------------------------------------------
Free Database Connection Pooling Software
 

Would the helper class be called from the bean or from the JSP?

Im sorry, it's just that Im new to this...
 

WOW it worked !!!!

I cant believe it was that easy of a change. All I did was to create a new method with my connect stuff and an empty constructor ...

Man, you dont know how grateful I am.

Thank you very much,

Ty Davis
Autry Greer and Sons
Mobile, Al.
 
Glad you got it to work !
BTW, I would personally call the helper class method from the JSP - nad keep the bean purely as a data holder (this separates business data from transactional logic).

--------------------------------------------------
Free Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top