RSS Forums RSS
Please support our JSP advertiser: Lunarpages JSP Web Hosting
Views: 805 | Replies: 1 | Thread Tools  Display Modes
Reply
Join Date: Jan 2008
Posts: 1
Reputation: durahman is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
durahman durahman is offline Offline
Newbie Poster

Mismatch Type Error

  #1  
Jan 31st, 2008
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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 7,176
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 26
Solved Threads: 396
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Mismatch Type Error

  #2  
Feb 3rd, 2008
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.
I don't accept change. I don't deserve to live.

Happiness corrupts people.

Failing to value the lives of others cheapens your own.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 8:01 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC