Wrote this code some time back for a programming interview, sharing it for everyone's use.
Its a 64 character encoded URL shortener code written in java. The code does not use any database backend but can be easily modified to support that.
Update: replacing the code text with Github Gist for better formatting support
Thanks for sharing the code.. Will try with storing the urls in database. Hopefully should work.
ReplyDeleteThansk for your help and very greatful to getting this good solution...
ReplyDeleteHi,
ReplyDeleteSolution looks good,but the shorten url does not redirects to the long one.Is that something i am missing.
thx...for the code
ReplyDeleteThe loop at the end seems to be incomplete...could you please complete that
ReplyDeletegood solutions..nice share :)
ReplyDeleteA url ending with a slash is not the same as a url without. Often it is, but it depends on the server. Just like the server decides if the path is case-sensitive or not (linux/windows). Whereas the host name isn't, and the protocol neither (equalsIgnoreCase("http://")).
ReplyDeleteThansk for your help and very greatful
ReplyDeletepackage com.firstgame;
ReplyDeleteimport java.awt.Color;
import java.awt.Graphics;= 5;
public int movementResetSpeed = 1;
public int movementSpeed = 1;
public int movementFrame = 1;
public int noseHeight = 10;
public int noseWidth = 10;
public boolean objectDefine = false;
public boolean falling = false;
public boolean running = true;
public boolean left = false;
public boolean right = false;
public boolean leftNose = true;
public boolean rightNose = true;
public Thread game;
public Ac (Ad f) {
setBackground(Color.blue);
defineObjects();
game = new Thread(this);
game.start();
f.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == keyLeft) {
left = true;
rightNose = false;
leftNose = true;
}
if(e.getKeyCode() == keyRight) {
right = true;
leftNose = false;
rightNose = true;
}
}
public void keyReleased(KeyEvent e) {
if(e.getKeyCode() == keyLeft) {
left = false;
}
if(e.getKeyCode() == keyRight) {
right = false;
}
}
});
}
void defineObjects() {
character = new Rectangle ((Am.width/2) - (characterwidth/2), (Am.height/2) - (characterheight/2), characterwidth, characterheight);
floor = new Rectangle(0, 300, 600, 100);
objectDefine = true;
System.out.println("Shapes DefineObjects is running");
repaint();
}
public void paint(Graphics g) {
super.paint(g);
if (objectDefine) {
System.out.println("Graphics is running");
g.setColor(Color.CYAN);
g.fillRect(character.x, character.y, character.width, character.height);
g.setColor(Color.green);
g.fillRect(floor.x, floor.y, floor.width, floor.height);
if(leftNose) {
g.setColor(Color.yellow);
g.fillRect(character.x - noseWidth, character.y + (character.height/4), noseWidth, noseHeight);
} else if(rightNose) {
g.setColor(Color.yellow);
g.fillRect(character.x + character.width, character.y + (character.height/4), noseWidth, noseHeight);
}
}
}
public void run() {
while (running) {
//character feet
Point pt1 = new Point(character.x, character.y + character.height);
Point pt2 = new Point(character.x +character.width, character.y +character.height);
//Falling
if(fallingFrame >= fallingSpeed) {
if (floor.contains(pt1) || floor.contains(pt2)) {
falling = false;
} else {
character.y += 1;
falling = true;
}
if (falling) {
character.y += 1;
}
fallingFrame = 0;
}else {
fallingFrame += 1;
}
//movement speed check
if(falling) {
movementSpeed = movementFallingSpeed;
} else {
movementSpeed = movementResetSpeed;
}
//Movement
if(movementFrame >= movementSpeed) {
if(right) {
character.x += 1;
}
if(left) {
character.x -= 1;
}
movementFrame = -1;
} else {
movementFrame += 1;
}
fpsSetter();
repaint();
System.out.println("Run Method is running");
}
}
public void fpsSetter() {
try{
game.sleep(fps/1000);
}
catch(Exception e) {
e.printStackTrace();
System.out.println("fpsSetter is running");
}
}
}
Help required,
ReplyDeleteGetting 404 while open tiny url.
Logs as follows :-
URL:http://localhost:8084/mobilox_website/home.do
Tiny: www.mydevendra.com/voqLRH
Expanded: localhost:8084/mobilox_website/home.do
I have a question on the generateKey() method. At some point you will run out of keys right?
ReplyDeletecant redirect this tinyurl
ReplyDeleteThank you for sharing such a Nice information about Java Code for Url Shortener.
ReplyDeleteCool and I have a nifty proposal: How Long Renovate House home remodeling estimates
ReplyDelete