Home > Java and Web > Session Tracking [ Part 1 ]

Session Tracking [ Part 1 ]

Session tracking merupakan teknik untuk menjaga state dari user. Ada empat teknik dalam session tracking yaitu:

  • Cookies
  • Hidden Form Field
  • URL Rewriting
  • HttpSession

Cookies

Cookie adalah informasi yang dikirim oleh web server ke client, dan informasi tersebut disimpan oleh client. Sehingga jika client melakukan request maka web server dapat mengetahui state client tersebut berdasar cookie yang disimpan. Class yang menyediakan fungsionalitas cookie adalah javax.servlet.http.Cookie, untuk lebih detail tentang class Cookie tersebut dapat dilihat disini.

Untuk add dan retrieve nilai cookie perlu menggunakan method berikut :

public void addCookie(Cookie cookie) : Method dari HttpServletResponse Interface.

public Cookie[] getCookie( ) : Method dari HttpServletRequest Interface.

Berikut ini contoh penggunaan Cookies

File : Login.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <form action="Login.do" method="post">
             <table>
                 <tr><td>Name : </td><td><input type="text" name="username"></td></tr>
                 <tr><td>Password: </td><td> <input type="password" name="password"></td></tr>
             </table>
            <input type="submit" value="Submit">
        </form>
    </body>
</html>

File : Login.java ( Servlet )

package Action;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Kukuh Utama
 */
public class Login extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
       
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
                  
        
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        
        Cookie uCookie = new Cookie("username", username);
        uCookie.setComment("Ini Cookie username");
        uCookie.setMaxAge(300);
        
        Cookie pCookie = new Cookie("password", password);
        pCookie.setComment("Ini Cookie password");
        uCookie.setMaxAge(300);
        
        response.addCookie(uCookie);
        response.addCookie(pCookie);
        
        if(username != null && password != null){
          
           RequestDispatcher rd = request.getRequestDispatcher("Home.jsp");
           rd.forward(request, response);
        } else{
            
        }
        
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }
}

File : web.xml

....

 <servlet>
        <servlet-name>Login</servlet-name>
        <servlet-class>Action.Login</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Login</servlet-name>
        <url-pattern>/Login.do</url-pattern>
    </servlet-mapping>

....

File : Home.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<% Cookie  cookie[] = request.getCookies();%>
<h5>Hello, <% out.println(cookie[0].getValue()); %></h5>
<h4>Your Password is : <% out.println(cookie[1].getValue()); %></h4>
</body>
</html>


Halaman Login:

Login

Login

Halaman Home:

Home

Home

Semoga bermanfaat.

Referensi : Dari berbagai sumber.

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: