Dont Compute With Wrapper

27 March 2014 2 comments

As a Java Developer, i got these interview questions a lot… really. What is the difference between int and Integer, which one is better ?

The answer for the difference, can be answered easily, int is a primitive data type and Integer is wrapper object for int, and so on bla bla bla google it.

But, for the better one the answer would be  depends on the purpose of it. For this example, i will put simple demo to show how slow is the Wrapper when we use it for the mathematics computation.

import java.util.Date;
public class Tester {
	public void runInteger() {
		long startTime = new Date().getTime();
		Integer ctr = 0;
		for (int i = 0; i < Integer.MAX_VALUE; i++) {
			ctr += i;
		}
		long endtime = new Date().getTime();
		System.out
				.println("Time for Integer: " + (endtime - startTime) + " ms");

		System.out.println();
	}

	public void runint() {
		long startTime = new Date().getTime();

		int i = 0;
		int ctr = 0;
		for (i = 0; i < Integer.MAX_VALUE; i++) {
			ctr += i;
		}

		long endtime = new Date().getTime();

		System.out.println("Time for int: " + (endtime - startTime) + " ms");

		System.out.println();
	}

	public static void main(String[] args) {
		new Tester().runInteger();
		new Tester().runint();
		new Tester().runInteger();
	}
}

and the output was:

Time for Integer: 7652 ms

Time for int: 5 ms

Time for Integer: 7541 ms

We can see the interval difference between primitive and wrapper on this example is quite huge ~ 7500ms .  But, unfortunately, many developers today didnt and dont aware of this,  because of the auto boxing-unboxing features since Java 1.5, the process is hidden on the background.

Categories: Java

Variable Declaration : Member, Outside or Inside Loop

21 March 2014 1 comment

Well, my colleague and I, were having this discussion before. He was a bit confused with best practice (in performance matter) to declare the variable, whether it’s as a member, inside loop or outside loop.

public class InsideLoop {
	public void test() {

		for (int i = 0; i < 100000; i++) {
			int calc = (int) (i * Math.pow(i, i * 2));
			System.out.println(i);
		}

	}

	public static void main(String []args) {
		long startMem = Runtime.getRuntime().freeMemory();
		long startTime = System.currentTimeMillis();
		new InsideLoop().test();
		long endTime = System.currentTimeMillis();
		long endMem = Runtime.getRuntime().freeMemory();
		System.out.println("Inside Loop Memory taken: " + (startMem - endMem)+ " bytes");
		System.out.println("Inside Loop Time taken: "+ (endTime-startTime) +" ms");
	}

}

 

public class OutsideLoop {
	public void test() {
		int calc = 0;
		for (int i = 0; i < 100000; i++) {
			calc = (int) (i * Math.pow(i, i * 2));

		}

	}

	public static void main(String[] args) {
		long startMem = Runtime.getRuntime().freeMemory();
		long startTime = System.currentTimeMillis();
		new OutsideLoop().test();
		long endTime = System.currentTimeMillis();
		long endMem = Runtime.getRuntime().freeMemory();
		System.out.println("Outside Loop Memory taken: " + (startMem - endMem)+ " bytes");
		System.out.println("Outside Loop Time taken: " + (endTime - startTime) + " ms");
	}

}

 

public class Member {
	int calc;

	public void test() {

		for (int i = 0; i < 100000; i++) {
			calc = (int) (i * Math.pow(i, i * 2));
			System.out.println(i);
		}

	}

	public static void main(String[] args) {
		long startMem = Runtime.getRuntime().freeMemory();
		long startTime = System.currentTimeMillis();
		new Member().test();
		long endTime = System.currentTimeMillis();
		long endMem = Runtime.getRuntime().freeMemory();
		System.out.println("Member Memory taken: " + (startMem - endMem)+ " bytes");
		System.out.println("Member Time taken: " + (endTime - startTime) + " ms");
	}

}

And the results are:

Inside Loop Memory taken: 16044520 bytes
Inside Loop Time taken: 626 ms

Outside Loop Memory taken: 16044264 bytes
Outside Loop Time taken: 638 ms

Member Memory taken: 16044392 bytes
Member Time taken: 610 ms

As we can see, there is no significant difference with where did you place the variable – at least for this test cases. Thanks for the magic of JIT feature that Java has on it, so the optimization can be done at the runtime of the application and made this achieved.

Of course, this cant be taken generally, especially for non-primitive data type, String, Thread, etc. And depends of what operations that you do for that Object.

Categories: Java

Case Study: Factory Design Pattern

17 December 2013 11 comments

I had a job to check our project code quality. And have to report it back to my team leader for any obstacle that i found in the project.  I found a lot of leaks and i think would be good to be discussed on the blog. Not to mock the author, but to learn and improve ourselves together.

Like this code, this is the part that i found in our code.

 public ContactInfoBean(final Reseller resellerInfo) {

        switch(resellerInfo.getType()) {

            case PROGRAM_CONTACT:

                readExecutiveInfo(resellerInfo);

                break;

            case FILE_CONTACT:

                readOperationalInfo(resellerInfo);

                break;

            default:

                break;

        }

    }

The code works fine, and do its job pretty well. But some problem will appear by using this code-style. This class will grow tailing the biz changes, as usual, the bigger one class, the “merrier” to maintain it is. And most likely this class, will be having more than one purpose, can be called low-cohesion.

Better OOP Approach

Well the better approach for the case above would be using the Factory Design Pattern.  We can let the factory of  READER to generate every single instance according to their type. It would be easier to grow the instance type, since we just need to create a new class and do a little modification in the Factory class. The caller class, wont grow and will stand still at its current shape.


public interface InfoReader {

	public void readInfo();

}

public class ExecutiveReader implements InfoReader {

	public void readInfo() {

		// override

	}

}

public class OperationalReader implements InfoReader {

	public void readInfo() {

		// override

	}

}

And The Factory

public enum ReaderType {
PROGRAM_CONTACT, FILE_CONTACT
}

 

public class InfoReaderFactory {

	public static InfoReader getInstance(ReaderType type) {

		InfoReader instance = null;

		switch (type) {

		case PROGRAM_CONTACT:

			instance = new ExecutiveReader();

			break;

		case FILE_CONTACT:

			instance = new OperationalReader();

			break;

		default:

			throw new IllegalArgumentException("Unknown Reseller");

		}

		return instance;

	}

}

And now The Caller


InfoReader reader = InfoReaderFactory.getInstance(resellerInfo.getType());

reader.readInfo();

The Benefits

With the Factory Design Pattern to handle this case, we can achieve some benefits,

  • Specifying a class for one task, means, easier to maintain since one class is for one purpose only (modularity/High Cohesion). i.e: Operational Reader is only to read data for Operational only, no other purpose. Just in case, one day in the future we need another Reader (say: NonOperationalReader). We just need create a new Class that extends (or implements) the InfoReader class and then we can override our own readInfo() function. This Caller class will have no impact. We just need to do some modification in the Factory code (and the Enum of course.
public enum ReaderType {
PROGRAM_CONTACT, FILE_CONTACT, NEW_READER
}

 

public class InfoReaderFactory {

	public static InfoReader getInstance(RaederType type) {

		InfoReader instance = null;

		switch (type) {

		case PROGRAM_CONTACT:

			instance = new ExecutiveReader();

			break;

		case FILE_CONTACT:

			instance = new OperationalReader();

			break;

		case NEW_READER:

			instance = new NonOperationalReader();

			break;

		default:

			throw new IllegalArgumentException("Unknown Reseller");

		}

		return instance;

	}

}

  • Higher Reusability of Parent’s Component (Inheritance): Since we have parent class (InfoReader), we can put common functions and thingies inside this InfoReader class, and later all of the derivative classes (ExecutiveReader and OperationalReader) can reuse the common components from InfoReader . Avoid code redundancy and can minimize coding time. Eventhough this one depends on how you do the code and cant be guaranteed :D

But, It’s Run Perfectly, Should We Change It?

Obviously the answer is big NO. This is only the case study and for your further experience and knowledge. OOP is good, do it anywhere it’s applicable. But the most important thing is, if it’s running, dont change it. It would be ridiculous if you ruin the entire working code just to pursue some OOP approach. Dont be naive also, no one can achieve the perfect code. The most important is we know what is the better approach.

This blog has some modifications and improvements caused by some comments in here. Personally, thanks for great input so i can keep the quality of this post.

Categories: Java, Miscellaneous Tags: , , ,

Calling private methods publicly ?

We Java developers, known 4 access modifiers in Java: private, protected, public, and package. Well, except for the private, the last three, can be called from outside of the class by inheritance, same package or from the instance.

Now, the common question, can private be called publicly (from outside class)? well the answer is NO and YES. No when you use ‘usual’ way to access it, and YES when you ‘hack’ into it using the Reflection API provided by Java itself.

Well okay, now just write the code that we will hack into. I called it as “TheVictim

package com.namex.hack;

public class TheVictim {
	private void hackTest() {
		System.out.println("hackTest called");
	}

	private static void hackTestStatic() {
		System.out.println("hackTestStatic called");
	}

}

Now after that, just follow my code and try to run it. I guarantee that if you followed it right, you will get TheVictim to call both of the hackTest and hackTestStatic. And you can see the output on your screen.

package com.namex.hack;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

public class HackTest {
	public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {

		Class c = TheVictim.class;

		Method[] ms = c.getDeclaredMethods();

		for (Method each : ms) {
			String methodName = each.getName();
			each.setAccessible(true); // this is the key
			if (Modifier.isPrivate(each.getModifiers())) {
				
				if (Modifier.isStatic(each.getModifiers())) {
					// static doesnt require the instance to call it.
					each.invoke(TheVictim.class, new Object[] {});
				} else {
					each.invoke(new TheVictim(), new Object[] {});
				}
			}
		}

	}
}


Output example:

hackTestStatic called
hackTest called

Okay, this tutorial has met its purpose. Now you know the Reflection API of java is very powerful feature of programming language. And it’s all up to you to modify or even extend it for your own purpose. Have fun with Java :)

Categories: Java Tags: ,

Task Driven vs Event Driven on Struts 2 Action

I had a job to design and develop a new framework for my own development team. We made the decision that we will adopt the Struts 2 as my MVC framework.

Yes i see the benefit from Struts 2 very well, easy to implement and has so many features. Until, my team member said “Hey, i hate Struts very much,  Because i have to put many exception-handling and/or debugger inside many events/methods. Can we use another framework? or just use the servlet mixed with the scriptlet?”

Well i know his concern, it would be easy to read and maintain the code with their exception-handling when your code was still small, but along with the time, you will have your own trouble to maintain the code to stay in a good shape, believe  me, i have had this kind of experience before.

So, i think and try to find a better solution and lucky for me, i found the much better solution with the Task-Driven approach. Maybe for some of you this approach will look like a tricky, but no problem as long as my team could gain a benefit from it and keep the code stay in healthy shape :).

Well, as a brief introduction for Struts 2. This Action code,perhaps, is the common way that almost every single Struts 2 developer will do. As you can see, you can have many methods that indicates as an “EVENT”

As you can see, i have the events/methods execute(), save(), fetch(), and delete().

and you can see on the above code, that you have to do the exception-handling four times, one each method.  For me — and team — one is more than enough, easy to read and maintain the code, especially when exception occur.

Now, my approach is to convert the familiarity of the event-driven into the task-driven.  So, i just have to add a new parameter that i gave name task, as a flagging parameter. In here i will have only 4 tasks to subtitute the 4 methods, “execute“, “save“, “fetch” and “delete“.

Implementing the code is easy, just put one extra attribute on the Action code, i named it task.

And on the JSP, only add a minor change, and i will give you the comparison between the event-driven and task-driven.

As you can see the difference between those 2 slices of code. I dont know how about you, but i love the task-driven Struts more than the event-driven. Maybe it’s relative, the answer could be different for another developer, but the most important for me is now my developers are happier to use Struts.

Categories: Java

Tremendous Moment of NamingException

17 March 2012 Leave a comment

These several months, has been tremendous year for me and my NamingException blog. Last year, around October 2011, ive been asked to be contributor of Java Code Geeks, and not only that. According to my private email with JCG founder, it turns out i’m the only and the first Indonesian Contributor at that time.

And now, in March 2012, ive been asked by Curator of Dzone.com for them to repost my blog content at their JavaLobby lounge and got an offer to join their Most Valuable Blogger (MVB) program. Of course, i accepted that offer.

Too bad, i havent updated my NamingException yet for several months, not because i dont want to, and im not too busy too, but honestly i have no idea about new and quality topic. I dont want my blog only act as “reposter” site, just to fulfill my post-target, for me, quantity is important but quality is still at the top.

If you have any idea to be discussed about, please let me know and help me to make this NamingException better and better in quality. I expect no commercial reason in here, all of my post are always and always will free and open-source. Never did, never do and never will about money. Let share about our skill and experience, from community, by community and to community. Let the benefit come to us by itself :)

Categories: Miscellaneous

Implementing URL Shortener with Servlet

10 October 2011 12 comments

I bet that most of you already tried Url Shortener Service to make your long link shorter and easier to write and remember. And perhaps you use more than one service, you name it ow.ly, goo.gl, bit.ly and still tons of services like that available freely on the cloud. But, how it works? here i will share its implementation with Servlet. It’s easy to implement, believe me.

First of all, prepare several tools below:

  • MySql Database Server. I use version 5.1.49 Community Edition, you can download newest MySql from http://www.mysql.com/downloads/mysql/. Or you can choose another DBMS implementation, like Oracle, Sql Server, etc.
  • MySql Connector/J, as our JDBC driver for MySql. You can download it from http://www.mysql.com/downloads/connector/j/. Please use suitable JDBC Driver if you choose another DBMS.
  • Servlet Container, here i use Apache Tomcat 6.0.32, you can download ithttp://tomcat.apache.org/. Of course, you can replace it with your own most favourite Web Server; Jetty, Glassfish, JBoss, etc.
  • servlet-api.jar, this is jar of Servlet API. You will need it to create a simple Http Servlet. You can download it from http://www.java2s.com/Code/Jar/STUVWXYZ/Downloadservletapijar.htm just in case you dont have it.
  • Your favourite Java IDE. In this tutorial I’m using Eclipse Helios 3.6, feel free to use similar IDE.

After you have all of these things above, we can start it. In order to make this post shorter, i assume that you have experience in creating new Web Project and adding required jars (servlet-api.jar and Mysql Connector/J ) to the classpath.

Scenario .

Before we started to the code, maybe i have to describe the business scenario first. My scenario is i want to make a simple program that take input of my long url and return a shorter link that formed like http://myserver/{id} , where {id} is my identifier to find the original url and turn my browser to open it.

Create Database

Run this Sql Script on your DBMS engine to create a table that stores our data. Dont forget, the id column has to be auto-increment. This is our primary key and we need it as a short url. You need to modify the Sql if you use other DBMS.

CREATE DATABASE /*!32312 IF NOT EXISTS*/`url_shortener` /*!40100 DEFAULT CHARACTER SET latin1 */;

CREATE TABLE `url_data` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `long_url` text NOT NULL,
  PRIMARY KEY (`id`)
);


Creating Servlet to Insert Long Url.

Now, create a new Java Class that extends javax.servlet.http.HttpServlet object and type below code.

package com.namex.shortener;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Insert extends HttpServlet {

    /**
     * Both POST and GET method are allowed to insert new record
     *
     */

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response)
	    throws ServletException, IOException {

	doGet(request, response);
    }

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

	String longUrl = request.getParameter("longUrl");
	request.getSession().setAttribute("a", "a");
	String a = response.encodeURL(request.getRequestURI());
	request.getSession().setAttribute("a", "a");
	System.out.println("->"+request.getRequestURI());
	System.out.println(a);
	System.out.println("shortening " + longUrl);
	String serverName = request.getServerName();
	int port = request.getServerPort();
	String contextPath = request.getContextPath();
	String shortUrl = null;
	try {
	    shortUrl = new Logic().getShort(serverName, port, contextPath,
		    longUrl);
	} catch (Exception e) {

	    e.printStackTrace();
	}
	System.out.println("short url: " + shortUrl);
	request.getSession().setAttribute("shortUrl", shortUrl);
	response.sendRedirect("index.jsp");
    }
}

In my case, i want my program to handle both of POST and GET method, so i need to implement both of doGet and doPost method. Just in case you only want to handle GET method and not the POST, so you only need to override the doGet method.

See the new Logic().getShort(serverName, port, contextPath, longUrl); line. That is our Logic class that handle the whole business-logic problem. So we can see our servlet neater. Below is complete Logic source code, just type the whole thing, including not used yet function. And dont forget to adjust the getConnection according to your DBMS IP Address and credential.

package com.namex.shortener;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Logic {
    public Connection getConnection() throws IllegalAccessException,
	    ClassNotFoundException, SQLException {
	//adjust it
	String url = "jdbc:mysql://localhost/url_shortener";
	Class.forName("com.mysql.jdbc.Driver");
	Connection conn = DriverManager.getConnection(url, "root", "root");
	return conn;
    }

    public String getId(String longUrl) throws Exception {
	Connection conn = null;
	ResultSet rs = null;
	Statement st = null;

	String query = "SELECT id FROM url_data WHERE long_url='"
		+ longUrl.trim() + "'";
	String id = null;
	try {
	    try {
		conn = getConnection();
		st = conn.createStatement();
		rs = st.executeQuery(query);
		if (rs.next()) {
		    id = rs.getString("id");
		}
	    } finally {

		if (rs != null) {
		    rs.close();
		}
		if (st != null) {
		    st.close();
		}
		if (conn != null) {
		    conn.close();
		}
	    }
	} catch (Exception e) {
	    throw e;
	}
	return id;
    }

    public String getShort(String serverName, int port, String contextPath,
	    String longUrl) throws Exception {

	Connection conn = null;

	Statement st = null;
	String id = getId(longUrl);// check if URL has been shorten already
	if (id != null) {
	    // if id is not null, this link has been shorten already.
	    // nothing to do

	} else {
	    // at this point id is null, make it shorter
	    String sqlInsert = "INSERT INTO url_data(long_url) VALUES('"
		    + longUrl.trim() + "')";
	    try {
		conn = getConnection();
		st = conn.createStatement();
		st.execute(sqlInsert);
	    } finally {
		if (st != null) {
		    st.close();
		}
		if (conn != null) {
		    conn.close();
		}
	    }
	    // after we insert the record, we obtain the ID as identifier of our
	    // new short link
	    id = getId(longUrl);

	}
	return "http://" + serverName + ":" + port + contextPath + "/" + id;
    }

    public String getLongUrl(String urlId) throws Exception {
	if (urlId.startsWith("/")) {
	    urlId = urlId.replace("/", "");
	}
	String query = "SELECT long_url FROM url_data where id=" + urlId;
	String longUrl = null;
	Connection conn = null;
	ResultSet rs = null;
	Statement st = null;

	try {
	    conn = getConnection();
	    st = conn.createStatement();
	    rs = st.executeQuery(query);
	    if (rs.next()) {
		longUrl = rs.getString("long_url");
	    }
	} finally {

	    if (rs != null) {
		rs.close();
	    }
	    if (st != null) {
		st.close();
	    }
	    if (conn != null) {
		conn.close();
	    }
	}

	return longUrl;
    }

}

Adjusting web.xml for Insert Servlet.

To let the Server identify your new created servlet, adjust your web.xml like this.

<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	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>URLShortener</display-name>

	<servlet>
		<servlet-name>Insert</servlet-name>
		<servlet-class>com.namex.shortener.Insert</servlet-class>
		<load-on-startup>-1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>Insert</servlet-name>
		<url-pattern>/insert</url-pattern>
	</servlet-mapping>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>

You see the <servlet></servlet> tag, between that tag is our servlet class and we give it a name Insert. So whenever we need to do configuration to this servlet, we use the name Insert. Dont forget to give negative integer to <load-on-startup></load-on-startup> because we wont need it to be loaded when the server is up. We only need the servlet loaded when it called explicitly from our program. You can see more detail about <servlet></servlet> tag here http://download.oracle.com/docs/cd/E13222_01/wls/docs81/webapp/web_xml.html#1039287

The <servlet-mapping></servlet-mapping> identify when the servlet is called by the program. In our case, the Insert servlet will be called when someone make request to /{context-path}/Insert.

Create User Interface.

Now it’s time to provide some user interface, i make a simple one with only 1 text-field and a simple scriptlet to view our recently shorted link. You can beautify it if you want. Put it on top of WEB-INF folder as index.jsp, because we want it to be the first page of our program.

<%@ 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></title>
</head>
<body>
	<form action="/url/insert" method="GET">
		Long URL: <input type="text" name="longUrl" size="100" /> <input
			type="submit" value="Get Short !" />

	</form>
	</p>
	<%
	    if (session.getAttribute("shortUrl") != null) {
	%>
	Hi, your short url is:
	<br />
	<br />
	<%=session.getAttribute("shortUrl")%>

	<%
	    }
	%>
</body>
</html>

url short 001

Testing Shortening

After we did all the steps above, now we can try it in action. Compile and make a WAR of your recently create application and deploy it on Tomcat. And open the program using your browser. Input the long URL, press the button and you can see the short URL generated below.

url short 002

the result would be

url short 003

IF, remember the BIG IF everything runs perfectly, our task isnt done yet. We still have to make a redirector that transforming the short url to the original url.

Creating Servlet to Transform Short Url.

Similar proses like here. Just type the Retrieve servlet, and be aware of line new Logic().getLongUrl(urlId);. We have had created this before and if you did what i told you before to code the Logic class completely, with all of its functions, the Retrieve servlet code should be perfect.

package com.namex.shortener;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Retrieve extends HttpServlet {

    private static final long serialVersionUID = 1293961717469276130L;

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

	String urlId = request.getServletPath();

	String longUrl = null;
	if (urlId != null && !"".equals(urlId)) {
	    try {
		longUrl = new Logic().getLongUrl(urlId);
	    } catch (Exception e) {
		// handling exception here
		e.printStackTrace();
	    }
	}

	if (longUrl == null) {
	    // if long url not found, send to index.jsp
	    System.out.println("long url not found, back to index.jsp");
	    response.sendRedirect("index.jsp");
	} else {
	    //if long url found, so redirect the browser
	    System.out.println("redirecting to "+longUrl );
	    response.sendRedirect(longUrl);
	}
    }

}

Adjusting web.xml for Retrieve Servlet.

Dont forget we have to adjust the web.xml setting to let the Tomcat identify our servlet. The whole web.xml would be like this.

<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	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>URLShortener</display-name>

	<servlet>
		<servlet-name>Insert</servlet-name>
		<servlet-class>com.namex.shortener.Insert</servlet-class>
		<load-on-startup>-1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>Insert</servlet-name>
		<url-pattern>/insert</url-pattern>
	</servlet-mapping>

	<servlet>
		<servlet-name>Retrieve</servlet-name>
		<servlet-class>com.namex.shortener.Retrieve</servlet-class>
		<load-on-startup>-1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>Retrieve</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>

Test the Whole Shortener.

This is our last part, now you can test the program completely. Run your Tomcat and open our program. Insert the long url and open the short url in your browser, see if it return your page or not.

Now, our Url Shortening service has been created. There are still many ways to implement it using various techniques, we can use SOAP, REST or even EJB to handle the shortening and retrieving url. Now it’s your turn to improve it and implement it for your own need.

Categories: Java
Follow

Get every new post delivered to your Inbox.

Join 726 other followers