Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone



Clound SSD Virtual Server

JSP and JavaScript / Ajax (Java)

JSP and JavaScript / Ajax (Java) เนื่องจาก JSP รูปแบบการเขียนเป็นแบบ Scriptlets คือเขียนร่วมกับ HTML / CSS และก็ JavaScript เพราะฉะนั้นเราจะสามารถทำการแทรก JavaScript เพื่อให้ทำงานในส่วนต่าง ๆ ได้ตามต้องการ โดยสามารถแทรก JavaScript สลับกับ JSP หรือจะแทรกให้ทำงานร่วมกันก็ได้ ลองมาดูตัวอย่างเพื่อความเข้าใจ

Example 1 การเรียกใช้งาน JavaScript ในไฟล์ JSP

index.jsp
<html>
<head>
	<title>ThaiCreate.Com JSP Tutorial</title>
</head>
<body>

	<script type="text/javascript">
	function fncSubmit()
	{
		if(document.form1.txt1.value == "")
		{
			alert('Please input Input 1');
			document.form1.txt1.focus();
			return false;
		}	
		if(document.form1.txt2.value == "")
		{
			alert('Please input Input 2');
			document.form1.txt2.focus();		
			return false;
		}	
		document.form1.submit();
	}
	</script>
	<form action="page.cgi" method="post" name="form1" onSubmit="JavaScript:return fncSubmit();">
	Input 1 <input name="txt1" type="text"><br>
	Input 2 <input name="txt2" type="text"><br>
	<input name="btnSubmit1" type="submit" value="Submit" >
	</form>
	
</body>
</html>


Output

JSP and JavaScript / Ajax

แสดงการทำงานของ JavaScript


Example 2 การแทรก Script ของ JSP Scriptlets กับส่วนของ JavaScript

index.jsp
<html>
<head>
	<title>ThaiCreate.Com JSP Tutorial</title>
</head>
<body>

	<%
		String strInput1 = "Name";
		String strInput2 = "Surname";
	%>
	<script type="text/javascript">
	function fncSubmit()
	{
		if(document.form1.txt1.value == "")
		{
			alert('Please input <%=strInput1%>');
			document.form1.txt1.focus();
			return false;
		}	
		if(document.form1.txt2.value == "")
		{
			alert('Please input <%=strInput1%>');
			document.form1.txt2.focus();		
			return false;
		}	
		document.form1.submit();
	}
	</script>
	<form action="page.cgi" method="post" name="form1" onSubmit="JavaScript:return fncSubmit();">
	<%=strInput1%> <input name="txt1" type="text"><br>
	<%=strInput2%> <input name="txt2" type="text"><br>
	<input name="btnSubmit1" type="submit" value="Submit" >
	</form>
	
</body>
</html>


JSP and JavaScript / Ajax

แสดงการทำงานของ JavaScript กับ JSP








Example 3 การเขียน JSP กับ JavaScript แบบ Ajax

JSP and JavaScript / Ajax

index.jsp
<html>
<head>
<title>ThaiCreate.Com JSP Tutorial</title>
</head>
	<script type="text/javascript">
		var HttPRequest = false;

		function doCallAjax() {
			HttPRequest = false;
			if (window.XMLHttpRequest) { // Mozilla, Safari,...
				HttPRequest = new XMLHttpRequest();
				if (HttPRequest.overrideMimeType) {
					HttPRequest.overrideMimeType('text/html');
				}
			} else if (window.ActiveXObject) { // IE
				try {
					HttPRequest = new ActiveXObject("Msxml2.XMLHTTP");
				} catch (e) {
					try {
						HttPRequest = new ActiveXObject("Microsoft.XMLHTTP");
					} catch (e) {
					}
				}
			}

			if (!HttPRequest) {
				alert('Cannot create XMLHTTP instance');
				return false;
			}

			var url = 'post.jsp';
			var pmeters = 'myName=' + document.getElementById("txtName").value;
			//var pmeters = 'myName='+document.getElementById("txtName").value+'&my2='; // 2 Parameters
			HttPRequest.open('POST', url, true);

			HttPRequest.setRequestHeader("Content-type",
					"application/x-www-form-urlencoded");
			HttPRequest.setRequestHeader("Content-length", pmeters.length);
			HttPRequest.setRequestHeader("Connection", "close");
			HttPRequest.send(pmeters);

			HttPRequest.onreadystatechange = function() {

				if (HttPRequest.readyState == 3) // Loading Request
				{
					document.getElementById("mySpan").innerHTML = "Now is Loading...";
				}

				if (HttPRequest.readyState == 4) // Return Request
				{
					document.getElementById("mySpan").innerHTML = HttPRequest.responseText;
				}

			}

			/*
			HttPRequest.onreadystatechange = call function .... // Call other function
			 */

		}
	</script>


<body>
	<input type="text" name="txtName" id="txtName" value="">
	<input name="btnButton" id="btnButton" type="button" value="Click"
		onClick="JavaScript:doCallAjax();">
	<br>
	<span id="mySpan"></span>
</body>
</html>

index.jsp
<%
	out.print("Sawatdee : " + request.getParameter("myName"));
%>


Output

JSP and JavaScript / Ajax

กรอกข้อมูล

JSP and JavaScript / Ajax

แสดงการทำงานของ JavaScript กับ JSP ในรูปแบบของ Ajax








อ่านเพิ่มเติมการใช้งาน JavaScript

JavaScript Tutorial : สอนการใช้งาน Event ของ JavaScript ในรูปแบบต่าง ๆ

Javascript Tips & Tricks : การใช้งาน JavaScript ร่วมกับ Form

Ajax Tutorial : สอน Ajax เขียน Ajax เรียน Ajax สุดยอดการใช้งาน Ajax อย่างง่าย


   
Share


ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2013-08-20 09:53:43 / 2017-03-27 15:53:40
  Download : No files
 Sponsored Links / Related

 
JSP and Post Request Method (Java)
Rating :

 
JSP and Get Request Method (Java)
Rating :

 
JSP and Request/Application Object (Java)
Rating :

 
JSP and Redirect sendRedirect() (Java)
Rating :

 
JSP and Auto Refresh Webpage (Java)
Rating :

 
JSP and HTML / Input form (Java)
Rating :

 
JSP and jQuery (Java)
Rating :

 
JSP and Ajax using jQuery (Java)
Rating :

 
JSP and Session (Create/Get/Delete) (Java)
Rating :

 
JSP and Cookies (Create/Get/Delete) (Java)
Rating :

 
JSP and Upload File Form (Java)
Rating :

 
JSP and JSON (Java)
Rating :

 
JSP and URLConnection (Java)
Rating :


ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 03
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่