Home > Java and Web > Attribute Pada Servlet

Attribute Pada Servlet

Attribute pada servlet memungkinkan data untuk disimpan dan digunakan antar servlet. Ada tiga scope untuk attribute yaitu

  • Request scope ( ServletRequest )
  • Session scope  ( HttpSession )
  • Context scope ( ServletContext )

Method yang digunakan yang digunakan untuk memanipulasi attribute sebagai berikut :

  • public void setAttribute(String name, Object object )
  • public Object getAttribute(String name)
  • public Enumeration getAttributeNames()
  • public void removeAttribue(String name)

Berikut ini contoh penggunaan Attribute untuk request dan session scope.

Nama file : index.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 method="post" action="AttributeEx">
              <table>
                     <tr><td>Name</td> <td><input type="text" name="name"></td>  </tr>
                     <tr><td>Mobile</td> <td><input type="text" name="mobile"></td> </tr>
                     <tr><td>Address</td> <td><input type="text" name="address"></td> </tr>
                     <tr><td> <input type="submit" value="Submit"> </td> </tr>
              </table>
        </form>
    </body>
</html>

Nama file : AttributeEx.java ( servlet )

package Action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class AttributeEx extends HttpServlet {
    
      protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String name=null;
        Enumeration attributes = request.getAttributeNames();
        try {
           
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet ExServlet</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h5>Request Attribute</h5>");
            out.println("<table>");
            out.println("<tr>");
            out.println("<td>Name Attribute </td>");
            out.println("<td>Value Attribute </td>");
            out.println("</tr>");
                while(attributes.hasMoreElements()){
                name = (String) attributes.nextElement();
            out.println("<tr>");
            out.println("<td>"+name+"</td>");
            out.println("<td>"+request.getAttribute(name)+"</td>");
            out.println("</tr>");
            }
            out.println("</table>");
            out.println("<a href=\"ForwardServlet\">To Session Attribute </a>");
            out.println("</body>");
            out.println("</html>");
          } finally {            
              out.close();
          }
    }
      
      
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
       String name ="";
       String address ="";
       int mobile = 0;
       
       name = request.getParameter("name");
       address = request.getParameter("address");
       mobile = Integer.parseInt(request.getParameter("mobile"));
       
       request.setAttribute("name", name);
       request.setAttribute("address", address);
       request.setAttribute("mobile", mobile);
       
       HttpSession mySession = request.getSession();
       
       mySession.setAttribute("name", name);
       mySession.setAttribute("address", address);
       mySession.setAttribute("mobile", mobile);
       System.out.println("Wonder Here");
       processRequest(request, response);
    }

}

Nama file : ForwardServlet.java ( servlet )

package Action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

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

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        System.out.println("do Get");
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String name = null;
        HttpSession mySession = request.getSession();
        Enumeration attributes = mySession.getAttributeNames();
       
            out.println("<html>");
            out.println("<head>");
            out.println("<title></title>");            
            out.println("</head>");
            out.println("<body>");  
            out.println("<h5>Session Attribute</h5>");
            out.println("<table>");
            out.println("<tr>");
            out.println("<td>Name Attribute </td>");
            out.println("<td>Value Attribute </td>");
            out.println("</tr>");
            while(attributes.hasMoreElements()){
                name = (String) attributes.nextElement();
                if( name.equalsIgnoreCase("name") || name.equalsIgnoreCase("address") || name.equalsIgnoreCase("mobile")){
                out.println("<tr>");
                out.println("<td>"+name+"</td>");
                out.println("<td>"+mySession.getAttribute(name)+"</td>");
                out.println("</tr>");
                }
            }
            out.println("</table>");
            out.println("</body>");
            out.println("</html>");
      
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        System.out.println("do Post");
        
    }
}

File : web.xml

.....

    <servlet>
        <servlet-name>AttributeEx</servlet-name>
        <servlet-class>Action.AttributeEx</servlet-class>
    </servlet>
    <servlet>
        <servlet-name>ForwardServlet</servlet-name>
        <servlet-class>Action.ForwardServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>AttributeEx</servlet-name>
        <url-pattern>/AttributeEx</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>ForwardServlet</servlet-name>
        <url-pattern>/ForwardServlet</url-pattern>
    </servlet-mapping>

.....

Halaman Index :

Index

Index

Halaman  Request Attribute Scope :

Attribute Request Scope

Attribute Request Scope

Halaman  Session Attribute Scope

Attribute Session Scope

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: