I am doing the following process using forms and javabeans:
1. User enter data using a HTML form
2. The form is submiited to form handler for validation using Java bean.
3. If validation is ok data will be inserted into the appropriate table.
When tested data entry and validation works fine. Problem is when trying to get the bean property value to a variable to be used o insert dat to table I received this error message (part):
..................
An error occurred at line: 9 in the jsp file: /temuduga/insert_majikan.jsp
Type mismatch: cannot convert from void to String
6: <jsp:useBean id="formHandler" class="myBeans.MajFormBean" scope="request" />
7:
8: <%
9: String noreg = %><jsp:getProperty name="formHandler" property="noreg_majikan" /><%
................................................
This is the code for the validation bean:
package myBeans;
import java.util.*;
public class MajFormBean {
private String noreg_majikan;
private String nama_majikan;
private String alamat_maj1;
private String alamat_maj2;
private String alamat_maj3;
private Hashtable errors;
public boolean validate() {
boolean allOk=true;
if (noreg_majikan.equals("")) {
errors.put("noreg_majikan","Perrlu diisi.");
noreg_majikan="";
allOk=false;
}
if (nama_majikan.equals("")) {
errors.put("nama_majikan","Perlu diisi.");
nama_majikan="";
allOk=false;
}
if (alamat_maj1.equals("")) {
errors.put("alamat_maj1","Perlu diisi");
alamat_maj1="";
allOk=false;
}
return allOk;
}
public String getErrorMsg(String s) {
String errorMsg =(String)errors.get(s.trim());
return (errorMsg == null) ? "":errorMsg;
}
public MajFormBean() {
noreg_majikan="";
nama_majikan="";
alamat_maj1="";
alamat_maj2="";
alamat_maj3="";
errors = new Hashtable();
}
public String getnoreg_majikan() {
return noreg_majikan;
}
public String getnama_majikan() {
return nama_majikan;
}
public String getalamat_maj1() {
return alamat_maj1;
}
public String getalamat_maj2() {
return alamat_maj2;
}
public String getalamat_maj3() {
return alamat_maj3;
}
}
When the validation is ok process is pass to this page as shown by following code:
<html>
<body>
<%@ page language="Java" import="java.sql.*" %>
<jsp:useBean id="db" scope="request" class="myBeans.DBConn" />
<jsp:setProperty name="db" property="*" />
<jsp:useBean id="formHandler" class="myBeans.MajFormBean" scope="request" />
<%
String noreg = %><jsp:getProperty name="formHandler" property="noreg_majikan" /><%
String nama = %><jsp:getProperty name="formHandler" property="nama_majikan" /><%
String add1 = %><jsp:getProperty name="formHandler" property="alamat_maj1" /><%
String add2 = %><jsp:getProperty name="formHandler" property="alamat_maj2" /><%
String add3 = %><jsp:getProperty name="formHandler" property="alamat_maj3" /><%
int i;
db.connect();
try{
i=db.updateSQL("INSERT INTO majikan(noreg_majikan, nama_majikan, alamat_maj1, alamat_maj2, alamat_maj3) VALUES(\"noreg\",\"nama\",\"add1\",\"add2\",\"add3\")");
} catch(SQLException e) {
System.err.println("SQLException: " + e.getMessage());
}
db.close();
%>
</body>
</html>
Beside that I will be grateful too if someone can tell me whether the inserting page has the correct jsp and SQL syntax.
Thanx