<p>Apa itu Cookies? Cookies adalah informasi yang berupa file kecil yang ditanam pada komputer client. Cookies biasanya digunakan oleh sebuah <a href="https://www.tutorialpedia.net/cara-membuat-website-profesional/">website </a>untuk mengidentifikasi data pengguna. Data yang disimpan pada Cookies dikirim bersamaan dengan request yang dilakukan oleh browser terhadap suatu halaman website.</p>
<p>Dengan menyimpan data informasi berupa cookie di <a href="https://www.tutorialpedia.net/cara-install-xampp-di-windows/">komputer </a>client, kita bisa mengidentifikasi user tersebut ketika mengakses website kita secara berulang. Penggunaan cookies banyak sekali digunakan <a href="https://www.tutorialpedia.net/4-situs-terbaik-untuk-belajar-bahasa-pemrograman-secara-otodidak/">situs-situs besar didunia </a>seperti <em>facebook,google dsb. </em></p>
<div class="bacajuga">
<div class="titlebaca"><strong>Baca Juga:</strong></div>
<div class="contentbaca">
<ul>
<li><a href="https://www.tutorialpedia.net/tutorial-lengkap-cara-membuat-web-sederhana-dengan-java/">Cara Membuat Web Dengan Java</a></li>
<li><a href="https://www.tutorialpedia.net/cara-membuat-website-profesional/">Cara Membuat Web Profesional Dengan WordPress</a></li>
<li><a href="https://www.tutorialpedia.net/10-contoh-desain-form-login-keren-dengan-bootstrap/">10 Desain Form Login Keren Dengan Bootstrap</a></li>
</ul>
</div>
</div>
<div class="partner-banner-aftc-baca-juga" style="text-align: center; margin-top: 30px;"></div>
<p>Cookie biasanya ditetapkan dalam header HTTP (meskipun JavaScript juga dapat menetapkan cookie langsung pada browser). Sebuah JSP yang menetapkan cookie mungkin mengirim header yang terlihat seperti ini:</p>
<pre class="lang:default decode:true">HTTP/1.1 200 OK 
Date: Fri, 04 Feb 2000 21:03:38 GMT 
Server: Apache/1.3.9 (UNIX) PHP/4.0b3 
Set-Cookie: name=xyz; expires=Friday, 04-Feb-07 22:03:38 GMT; 
 path=/; domain=tutorialpedia.net 
Connection: close 
Content-Type: text/html</pre>
<p>Contoh penggunaan cookie bisa kita lihat pada website yang mempunyai fasilitasi untuk melakukan <a href="https://www.tutorialpedia.net/cara-mengubah-url-login-wordpress/">login</a>. Biasanya akan terdapat sebuah checkbox <em>&#8220;Remember Me&#8221; </em>dimana <em>&#8220;Remember Me&#8221; </em>itu berfungsi untuk menyimpan segala informasi detail login pada web browser berupa cookies. Yang nanti nya akan diambil informasi detail login tersebut ketika akan masuk ke website tanpa perlu mengetikkan ulang.</p>
<h2><strong>Cara Menggunakan Cookies dengan JSP</strong></h2>
<h3><strong>Membuat Cookie :</strong></h3>
<pre class="lang:default decode:true"> Cookie firstName = new Cookie("first_name", 
 			 request.getParameter("first_name")); 
 Cookie lastName = new Cookie("last_name", 
			 request.getParameter("last_name"));</pre>
<p>Pada kode diatas berfungsi untuk membuat sebuah cookie dengan nama <strong>first_name </strong>dan <strong>last_name.</strong></p>
<p>Mengatur Expires Cookie untuk 24 jam</p>
<pre class="lang:java decode:true "> firstName.setMaxAge(60*60*24); 
 lastName.setMaxAge(60*60*24);</pre>
<p>Menambahkan kedua cookie tersebut pada response header</p>
<pre class="lang:java decode:true "> response.addCookie( firstName ); 
 response.addCookie( lastName );</pre>
<p>Sehingga kode lengkap pada halaman <strong>cookie.jsp </strong>akan terlihat seperti berikut ini:</p>
<p><strong>Cookie.jsp</strong></p>
<pre class="lang:default decode:true"><;%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
 pageEncoding="ISO-8859-1"%>; 
<;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">; 
<;% 
 // Create cookies for first and last names. 
 Cookie firstName = new Cookie("first_name", 
 			 request.getParameter("first_name")); 
 Cookie lastName = new Cookie("last_name", 
			 request.getParameter("last_name")); 
 
 // Set expiry date after 24 Hrs for both the cookies. 
 firstName.setMaxAge(60*60*24); 
 lastName.setMaxAge(60*60*24); 
 
 // Add both the cookies in the response header. 
 response.addCookie( firstName ); 
 response.addCookie( lastName ); 
%>; 
<;html>; 
	<;head>; 
		<;title>;Setting Cookies<;/title>; 
	<;/head>; 
	<;body>; 
		<;center>; 
		<;h1>;Setting Cookies<;/h1>; 
		<;/center>; 
		<;ul>; 
			<;li>;<;p>;<;b>;First Name:<;/b>; 
			 <;%= request.getParameter("first_name")%>; 
			<;/p>;<;/li>; 
			<;li>;<;p>;<;b>;Last Name:<;/b>; 
			 <;%= request.getParameter("last_name")%>; 
			<;/p>;<;/li>; 
		<;/ul>; 
	<;/body>; 
<;/html>;</pre>
<p> ;</p>
<p>Sekarang kita akan membuat sebuah form dengan field <strong>first_name</strong> dan <strong>last_name</strong>. Lalu isi kedua field tersebut akan disimpan di cookies browser.</p>
<p>Buat file jsp baru dengan nama <strong>input.jsp </strong>lalu isi denga kode berikut:</p>
<pre class="lang:default decode:true "><;%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
 pageEncoding="ISO-8859-1"%>; 
<;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">; 
<;html>; 
	<;head>; 
		<;title>;Test Cookie<;/title>; 
	 
	<;/head>; 
	<;body>; 
		<;form action="cookie.jsp" method="post">; 
		First Name: <;input type="text" name="first_name">; 
		<;br />; 
		Last Name: <;input type="text" name="last_name" />; 
		<;input type="submit" value="Submit" />; 
		<;/form>; 
	<;/body>; 
<;/html>;</pre>
<p>form action pada kode diatas akan di arahkan ke <strong>cookie.jsp </strong>yang nantinya akan disimpan informasi <strong>first_name</strong> dan <strong>last_name</strong> pada cookies browser.</p>
<p><a href="https://www.tutorialpedia.net/wp-content/uploads/Screenshot_21.png"><img class="aligncenter size-full wp-image-2983" src="https://www.tutorialpedia.net/wp-content/uploads/Screenshot_21.png" alt="cara menggunakan cookies di jsp" width="538" height="227" /></a></p>
<p>Lalu lihat pada cookies browser, maka akan terlihat beberapa informasi yang sudah kita masukkan tadi.</p>
<p><a href="https://www.tutorialpedia.net/wp-content/uploads/Screenshot_22.png"><img class="aligncenter size-full wp-image-2984" src="https://www.tutorialpedia.net/wp-content/uploads/Screenshot_22.png" alt="cara menggunakan cookies di jsp" width="705" height="323" /></a></p>
<p> ;</p>
<h3><strong>Membaca Cookie</strong></h3>
<p>buat file jsp baru dengan nama <strong>readcookie.jsp </strong>dan berikut ini isi kodenya:</p>
<pre class="lang:default decode:true "><;%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
 pageEncoding="ISO-8859-1"%>; 
<;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">; 
<;html>; 
	<;head>; 
	<;title>;Reading Cookies<;/title>; 
	<;/head>; 
	<;body>; 
	<;h1>;Reading Cookies<;/h1>; 
	<;% 
	 Cookie cookie = null; 
	 Cookie[] cookies = null; 
	 // Get an array of Cookies associated with this domain 
	 cookies = request.getCookies(); 
	 if( cookies != null ){ 
	 out.println("<;h2>; Found Cookies Name and Value<;/h2>;"); 
	 for (int i = 0; i <; cookies.length; i++){ 
	 cookie = cookies[i]; 
	 out.print("Name : " + cookie.getName( ) + ", "); 
	 out.print("Value: " + cookie.getValue( )+" <;br/>;"); 
	 } 
	 }else{ 
	 out.println("<;h2>;No cookies founds<;/h2>;"); 
	 } 
	%>; 
	<;/body>; 
<;/html>;</pre>
<p> ;</p>
<p>Maka hasilnya akan tampil seperti berikut:</p>
<p> ;</p>
<p><a href="https://www.tutorialpedia.net/wp-content/uploads/Screenshot_25.png"><img class="aligncenter size-full wp-image-2986" src="https://www.tutorialpedia.net/wp-content/uploads/Screenshot_25.png" alt="cara menggunakan cookies di jsp" width="509" height="279" /></a></p>
<h3><strong>Menghapus Cookie:</strong></h3>
<p>Untuk menghapus cookie anda bisa melakukannya secara langsung melalui browser. atau bisa menggunakan kode berikut ini.</p>
<p>Buat file baru dengan nama <strong>hapuscookie.jsp</strong></p>
<pre class="lang:default decode:true"><;%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
 pageEncoding="ISO-8859-1"%>; 
<;!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=ISO-8859-1">; 
<;title>;Insert title here<;/title>; 
<;/head>; 
<;body>; 
<;% 
 Cookie cookie = null; 
 Cookie[] cookies = null; 
 // Get an array of Cookies associated with this domain 
 cookies = request.getCookies(); 
 if( cookies != null ){ 
 out.println("<;h2>; Found Cookies Name and Value<;/h2>;"); 
 for (int i = 0; i <; cookies.length; i++){ 
 cookie = cookies[i]; 
 if((cookie.getName( )).compareTo("first_name") == 0 || ((cookie.getName( )).compareTo("last_name") == 0 )){ 
 cookie.setMaxAge(0); 
 response.addCookie(cookie); 
 out.print("<;b>;Deleted cookie: " + 
 cookie.getName( ) + "<;br/>;<;/b>;"); 
 } 
 out.print("Name : " + cookie.getName( ) + ", "); 
 out.print("Value: " + cookie.getValue( )+" <;br/>;"); 
 } 
 }else{ 
 out.println( 
 "<;h2>;No cookies founds<;/h2>;"); 
 } 
%>; 
<;/body>; 
<;/html>;</pre>
<p>Jalankan <strong>hapuscookie.jsp </strong>maka hasilnya akan seperti berikut:</p>
<p><a href="https://www.tutorialpedia.net/wp-content/uploads/Screenshot_26.png"><img class="aligncenter size-full wp-image-2987" src="https://www.tutorialpedia.net/wp-content/uploads/Screenshot_26.png" alt="cara menggunakan cookies di jsp" width="527" height="244" /></a></p>
<p>Sekarang coba tutup browser anda lalu buka kembali <strong>readcookie.jsp </strong>maka hasilnya akan seperti berikut:</p>
<p><a href="https://www.tutorialpedia.net/wp-content/uploads/Screenshot_27.png"><img class="aligncenter size-full wp-image-2989" src="https://www.tutorialpedia.net/wp-content/uploads/Screenshot_27.png" alt="cara menggunakan cookies di jsp" width="498" height="215" /></a></p>
<p> ;</p>
<p><a class="fasc-button fasc-size-medium fasc-type-flat ico-fa fasc-ico-before fa-github-square" style="background-color: #109d58; color: #ffffff;" href="https://github.com/fikryans/JSP-Cookies-Handling" target="_blank" data-fasc-style="background-color:#109d58;color:#ffffff;">Github</a></p>
<p>Itulah Cara Menggunakan Cookies dengan JSP.</p>
<p>Jika ada yang ingin ditanyakan silahkan kirimkan pesan melalui fan page TutorialPedia.NET.</p>
<p> ;</p>

Cara Menggunakan Cookies dengan JSP
Recent Articles
Kumpulan DNS Gratis Tercepat!
DNS merupakan sebuah sistem yang bertugas menyimpan semua informasi data domain dalam jaringan. Dengan adanya DNS, domain atau hostname yang ada akan ditranslate dan...
Apa itu DNS ? Dan Fungsinya
Apa itu DNS? - DNS (Domain Name System) adalah sebuah sistem yang bertugas menyimpan semua informasi data domain dalam jaringan. Dengan adanya DNS, domain...
Apa itu OLT ? Serta Fungsinya Dalam Jaringan
Apa itu olt ? OLT (Optical Line Termination) adalah perangkat keras yang berfungsi sebagai titik akhir (end-point) dari layanan jaringan optik pasif (Passive Optical...
Template Voucher Untuk Mixradius
Template Voucher Untuk Mixradius - Pasti kalian bosen kan dengan template voucher default mixradius? tenang. kali ini saya akan memberikan template untuk voucher mixradius kamu agar...
Daftar Port Game Online Mikrotik Terbaru
Daftar port game online dibawah ini bisa digunakan untuk membuat rule agar koneksi ke game online tersebut bisa lancar, bisa juga untuk memisahkan antara...
Kak! Tutorialnya membantu saya bangeet, makasih dan keep posting yakk
sama sama