Sunday 20 February 2011

JQ Forms with AJAX-JSP8

This is basicly the same as Sample4 but there is a small addition to
demonstrate fieldSerialize and fieldValue Form Plugin methods.

sample8.java
------------
<%--
Document : sample4
Created on : 19.Şub.2011, 20:17:55
Originally : belongs to "jQuery Form Plugin"
Author : Ali Riza SARAL
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="jquery.form.js"></script>
<script type="text/javascript">
// wait for the DOM to be loaded
$(document).ready(function() {
// bind form using ajaxForm
$('#jsonForm').ajaxForm({
// dataType identifies the expected content type of the server response
dataType: 'json',

// success identifies the function to invoke when the server response
// has been received
success: processJson
});
});

function processJson(data) {
// 'data' is the json object returned from the server
alert(data.message);
var queryString = $('#jsonForm :text').fieldSerialize();
var pwd = $('#jsonForm :text').fieldValue()[0];
alert(queryString);
}
</script>
</head>
<body>
<div id="sample4">
<p />
This page shows how to handle JSON data returned from the server.
<p />
The form below submits a message to the server and the server
echos it back in JSON format.
<br />
<p />
<form id="jsonForm" action="json-echo.jsp" method="post"><div>
Message: <input type="text" name="message" value="Hello JSON" />
<input type="submit" value="Echo as JSON" />
</div></form>
<br />
</div>
</body>
</html>

json-echo.java
--------------
<%
out.print("{ \"message\": \"" + request.getParameter("message") + "\" }");
%>