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

You shouldn't use scriptlets in your JSP file. They hamper readability, maintainability and re usability. Firing SQL queries / database access from JSP code is also bad in itself.

Create a simple HTML login form. Create a form bean. When the form is submitted, redirect to a controller servlet which pulls the form values and creates a form bean out of this. Set this bean in the session scope. Call the validate function on this bean.

If validation succeeds, pass the bean instance to a DAO class (Data Access Object) which can persist in into the database/any other persistent storage and destroy the bean from the session scope. If validation fails, pass a error bean / a list of error messages back to the same page. Use the bean stored in the session to restore the values which were correctly filled and display the contents of error bean / list.

Use JSTL for flow control and looping. Look into the JSTL Core tag. Though it might look intimidating at first, learning it would finally make your code clutter free and is an invaluable for a J2EE developer to have. Look into the J2EE tutorials at the Sun Official site.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.