Konfigurasi XML dan anotasi pada Servlet

Konfigurasi servlet dapat dilakukan melalui deployment descriptor (Web.xml) atau anotasi @WebServlet. IDE Eclipse yang saya gunakan (LUNA) secara default menggunakan anotasi untuk mengkonfigurasi sebuah servlet. Tujuan utama konfigurasi servlet adalah untuk memberitahu bagaimana servlet tersebut dapat diakses. Berikut contoh konfigurasi servlet melalui web.xml.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>SimpleWeb</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>    
  </welcome-file-list>
  <servlet>
  	<servlet-name>xmlServlet</servlet-name>
  	<servlet-class>com.zisal.XMLServlet</servlet-class>
  </servlet>
  <servlet-mapping>
  	<servlet-name>xmlServlet</servlet-name>
  	<url-pattern>/XMLServlet</url-pattern>
  </servlet-mapping>
  </web-app>

XMLServlet.java

package zisal.com.simpleweb;

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

/**
 * Servlet implementation class XMLServlet
 */
public class XMLServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public XMLServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("XML Servlet Called !!!");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}

output


XMLServlet diatas, dipanggil berdasarkan url pattern yang telah dideskripsikan di web.xml. Untuk menggantikan konfigurasi web.xml, konfigurasi diatas dapat diganti dengan

@WebServlet(description="xmlServlet", urlPatterns="/XMLServlet")

Dua tipe konfigurasi tersebut, bisa dipilih salah satunya tergantung dari preference developer.

Tagged: , , , , ,

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: