Saturday, July 7, 2007

HTTP Status Codes

In a web app, whenever there is a communication between the browser (i.e client) and (web) server, there is a back-and-forth transfer of data. The client sends data to Server for processing. Data is then processed at the server side, and a response is sent back to client, indicating the status of the sent request. The response sent by Server to browser, typically, has a status line, some response headers, blank line, and the document, e.g.

HTTP/1.1 200 OK
Content-Type: text/plain

This is an example of status codes.

Here, the first line indicates the HTTP Version (HTTP/1.1), a status code (200) and a short description of status code (ok). Second line consists of the various headers sent by Server. At mimimal, it has a content type. The data after the blank line indicates the document itself, though not all responses may have a document. If a document is present, the content type indicates its MIME type.

Given below is a list of the commonly used status codes. The status codes are all three-digit numbers that are grouped by the first digit into 5 groups.

1xx: Informational

100 Continue
Continue with partial request.

101 Switching Protocols
Server will comply with Upgrade header and change to different protocol.


2xx: Successful

200 OK
Means that the server did whatever the client wanted it to, and all is well.

Others
The rest of the 2xx status codes are mainly meant for script processing and are
not often used.


3xx: Redirection
Means that the resource is somewhere else and that the client should try again at
a new address.

301 Moved permanently
The resource the client requested is somewhere else, and the client should go there
to get it. Any links or other references to this resource should be updated.

302 Moved temporarily
This means the same as the 301 response, but links should now not be updated, since
the resource may be moved again in the future.

304 Not modified
This response can be returned if the client used the if-modified-since header field
and the resource has not been modified since the given time. Simply means that the
cached version should be displayed for the user.


4xx: Client error
Means that the client screwed up somehow, usually by asking for something it should
not have asked for.

400 Bad request
The request sent by the client didn't have the correct syntax.

401 Unauthorized
Client tried to access password-protected page without proper authorization. Response should include a WWW-Authenticate header that the browser would use to pop up a username/password dialog box, which then comes back via the Authorization header.

403 Forbidden
The client is not allowed to access the resource and authorization will not help.
Often the result of bad file or directory permissions on the server.

404 Not found
Seen this one before? :) It means that the server has not heard of the resource
and has no further clues as to what the client should do about it. In simpler
words: dead link.

405 Method Not Allowed
The request method not allowed for this particular resource.


5xx: Server error
This means that the server screwed up or that it couldn't do as the client requested.

500 Internal server error
Something went wrong inside the server. It is often the result of Servlets or CGI
programs or (Any Other Server Side Technology) that crash or return improperly
formatted headers.

501 Not implemented
The request method is not supported by the server.

503 Service unavailable
This sometimes happens if the server is too heavily loaded and cannot service the
request. Usually, the solution is for the client to wait a while and try again.
A servlet might return this header if some thread or database connection pool is
currently full. Server can supply a Retry-After header.

505 HTTP Version Not Supported
Server doesn't support version of HTTP indicated in request line.


Just keep in mind that some of the status codes may be available only in HTTP/1.1 version though some of the browsers may still support only HTTP/1.0 version.
The general method of setting status codes is simply to call response.setStatus(int), there are two common cases where a shortcut method in HttpServletResponse is provided. The sendError method generates a 404 response along with a short message formatted inside an HTML document. The advantage of sendError over setStatus is that, with sendError, the server automatically generates an error page showing the error message. And the sendRedirect method generates a 302 response along with a Location header indicating the URL of the new document.


Advertisement
Fund Literacy, Care for the Environment, and get a Fair Price on the Books you Want.
http://www.betterworld.com/

Sunday, June 24, 2007

Jar, War and Ear files

Jar, War and Ear files are essentially the archived files. There are no structural differences among the three types, though these are intended for different purposes.

Jar: Jar files (Java Archive file : .jar extension) stores of Java classes, resources, auxiliary files, etc. Java source files are compiled and then distributed in jar format.

War files (Web Archive File : .war extension) are intended to contain complete Web applications. A Web Application is a collection of files, classes, resources, .jar files that can be packaged and accessed as one servlet context. Usually, sample web applications are distributed using war format.

Ear files (Enterprise Archive file : .ear extension) are intended to contain complete Enterprise applications. In this context, an enterprise application is defined as a collection of .jar files, resources, classes, and multiple Web applications.

All three types of files can be generated using Ant.

Advertisement

Thursday, June 21, 2007

Optical Illusion

Aoccdrnig to rscheearch at Cmabrigde Uinervtisy, it deosn't mttaer in waht oredr the ltteers in a wrod are, the olny iprmoatnt tihng is taht the frist and lsat ltteer be at the rghit pclae. The rset can be a toatl mses and you can sitll raed it wouthit a porbelm. Tihs is bcuseae the huamn mnid deos not raed ervey lteter by istlef, but the wrod as a wlohe.

Find this book at Biblio.com

Saturday, June 16, 2007

River Crossing in Manali

Here is a video of my recent trip to Manali. Manali is a popular north Indian tourist spot.
This video was shot at Manali Club and features me doing a river crossing.



Thursday, June 7, 2007

Top Ten Reasons AJAX is Here to Stay

Andre Charland at developer.com has compiled a list of reasons that make AJAX a hot property in tech-crazy world.

Top Ten Reasons for AJAX

10. XAML, XUL, XForms...Not Yet.
9. Server Technology Agnostic
8. Web 2.0
7. Adoption Is Strong with Industry Leaders
6. Plays Nicely with Flex and Flash
5. Low Incremental Cost
4. Benefits of Regular Web Applications
3. Cross Browser and Cross Platform
2. Usability and User Experience Are King
1. Open Standards Based


Top Ten Reasons AJAX is Here to Stay

Thursday, May 31, 2007

[Humor] Copy n Paste

A well known motivational speaker gathering the entire crowd's attention, said "The best years of my life were spent in the arms of a woman who wasn't my wife"
The crowd was shocked...
He followed up by saying, "That woman was my mother"
The crowd burst into laughter and he gave his speech, which was well received.
About a week later, one of the top managers of an organization who had attended the speech decided to use that joke at his house. He tried to rehearse the joke in his head. It was a bit foggy to him.
He said loudly, "The greatest years of my life were spent in the arms of a woman who was not my wife"
Naturally, his wife was shell shocked, murmuring.
After standing there for almost 10 seconds trying to recall the second half of the joke, the manager finally blurted out "... and I can't remember who she was"
As expected, he got the thrashing of his life time...

Moral of the story:
Don't copy if you can't paste J J J

[Received in an email]

Sunday, May 20, 2007

Show'em Who's Boss

Received this seriously funny pic in an email forward :)





How true it is!

Monday, May 14, 2007

If you just focus on fixing someone...

Rajesh Setty of Life Beyond Code has this inspiring entry about 'fixing' people.

Your spouse wants to fix you.

You want to fix your spouse.

Your parents want to fix you.

You want to fix your children.

Your teachers want to fix you.

You want to fix your teachers.

Your boss wants to fix you.

You want to fix your boss.

Your colleagues want to fix you.

You want to.. well you get the point.

Wanting to fix someone whether right or not has one problem. In your quest to fix that someone, you forget to leverage the strengths of the that person for the benefit of both.

Next time, before you attempt to "fix" someone, why not focus on what that person brings to the table and see how best both of you can benefit from it?


http://blog.lifebeyondcode.com/blog/_archives/2007/5/10/2941581.html

Sunday, May 6, 2007

Java 6 Feature : Working with Derby Database

The Java 5 and Java 6 releases have introduced some significant changes. While Java 5 introduces new features (e.g. Generics, Variable arguments, Enhanced for loop, Boxing/unboxing, Type-safe enumerations, Static import, Metadata), Java 6 installation includes a lightweight database known as Derby.
Derby is actually an Apache Database project. Derby is a transactional, relational database and provides a small footprint on disk. When you install Java 6, core libraries, example programs, and a sample database gets automatically installed.
Derby has a command-line tool called ij. This tool provides a way to connect to and manipulate Derby databases.

To connect to Derby is quite easy. You need to have following jar files in the classpath to enable you to access Derby:
D:\tools\jdk1.6.0_01\db\lib\derby.jar
D:\tools\jdk1.6.0_01\db\lib\derbytools.jar

The derby.jar has JDBC drivers, while derbytools.jar contains the ij tool.

Once you have configured the classpath for Derby, open the command prompt, and start ij tool:
C:\Documents and Settings\Administrator>java org.apache.derby.tools.ij
ij version 10.2
ij>

You are now connected to Derby db.
Connect to the supplied example database (toursdb):
ij> connect 'jdbc:derby:Absolute_Path_to_the_connecting_database';
ij>

You can create a new database from the ij tool.
connect 'jdbc:derby:DB4MyApplication;create=true';

A semicolon (;) at the end of each command is also required.
You can get a list of Derby commands by issuing help command.
ij> help;

Supported commands include:

PROTOCOL 'JDBC protocol' [ AS ident ];
-- sets a default or named protocol
DRIVER 'class for driver'; -- loads the named class
CONNECT 'url for database' [ PROTOCOL namedProtocol ] [ AS connectionName ];
-- connects to database URL
-- and may assign identifier
SET CONNECTION connectionName; -- switches to the specified connection
SHOW CONNECTIONS; -- lists all connections
AUTOCOMMIT [ ON OFF ]; -- sets autocommit mode for the connection
DISCONNECT [ CURRENT connectionName ALL ];
-- drop current, named, or all connections;
-- the default is CURRENT

SHOW SCHEMAS; -- lists all schemas in the current database
SHOW [ TABLES VIEWS PROCEDURES SYNONYMS ] { IN schema };
-- lists tables, views, procedures or synonyms
SHOW INDEXES { IN schema FROM table };
-- lists indexes in a schema, or for a table
DESCRIBE name; -- lists columns in the named table

COMMIT; -- commits the current transaction
ROLLBACK; -- rolls back the current transaction

PREPARE name AS 'SQL-J text'; -- prepares the SQL-J text
EXECUTE { name 'SQL-J text' } [ USING { name 'SQL-J text' } ] ;
-- executes the statement with parameter
-- values from the USING result set row
REMOVE name; -- removes the named previously prepared statement

RUN 'filename'; -- run commands from the named file

ELAPSEDTIME [ ON OFF ]; -- sets elapsed time mode for ij
MAXIMUMDISPLAYWIDTH integerValue;
-- sets the maximum display width for
-- each column to integerValue

ASYNC name 'SQL-J text'; -- run the command in another thread
WAIT FOR name; -- wait for result of ASYNC'd command

GET [SCROLL INSENSITIVE] CURSOR name AS 'SQL-J query';
-- gets a cursor (JDBC result set) on the query
-- SCROLL cursors are only available
-- in JDBC 2.0 and higher.
-- (Cursor scroll type is ignored in JDBC 1.X.)
NEXT name; -- gets the next row from the named cursor
FIRST name; -- gets the first row from the named scroll cursor
LAST name; -- gets the last row from the named scroll cursor
PREVIOUS name; -- gets the previous row from the named scroll cursor
ABSOLUTE integer name; -- positions the named scroll cursor at the absolute row number
-- (A negative number denotes position from the last row.)
RELATIVE integer name; -- positions the named scroll cursor relative to the current row
-- (integer is number of rows)
AFTER LAST name; -- positions the named scroll cursor after the last row
BEFORE FIRST name; -- positions the named scroll cursor before the first row
GETCURRENTROWNUMBER name; -- returns the row number for the current position of the named scroll cursor
-- (0 is returned when the cursor is not positioned on a row.)
CLOSE name; -- closes the named cursor
LOCALIZEDDISPLAY [ ON OFF ];
-- controls locale sensitive data representation

EXIT; -- exits ij
HELP; -- shows this message

Any unrecognized commands are treated as potential SQL-J commands and executed directly.

So, now you have a lightweight DB installed alongwith the JDK. You can use this database for Proof-of-Concepts (POC), sample applications, demos among other things. To connect to Derby from Java, use the supplied driver
org.apache.derby.jdbc.EmbeddedDriver
. Connect to DB as shown earlier.

Saturday, May 5, 2007

Developing a Spring Framework MVC application step-by-step

Recently, I started reading on Spring. I was looking for an example application code, and then, I came across this article on springframework.org. The article is quite old (first written in July 2003, and then revised in April 2005), but quite relavent to topic. This is a hand-holding type of tutorial and develops an application from scratch, covering various Spring components, and even talks to HSqlDB, with JUnit test cases. Ant is used for deployment. It takes not more than 2-3 hours to develop the complete application.

Application is covered in 4 steps:
  • Basic Application and Environment Setup
  • Developing and Configuring the Application
  • Adding Unit Tests and a Form to the Application
  • Implementing Database Persistence

The tutorial assumes a Tomcat installation, but then u can develop it for any servlet engine or application server (e.g. i did it for JBoss).

it is a good exercise if u r new to Spring, or want to have a first hand look at Spring.

http://www.springframework.org/docs/MVC-step-by-step/Spring-MVC-step-by-step.html

Tuesday, May 1, 2007

Java 5 Feature: Static Imports

Static imports are another convenience feature added to version 1.5 that extends the way imports work in Java. For example, consider the code fragment shown below that calls the static ceil() method on the java.lang.Math class


// x is a number of type double such as 98.765
double y = Math.ceil(x);


With static imports in 1.5, you can ask the Java compiler to import only a class's static portions, as shown, for example, in the rewritten code fragment below:


// Import declaration at the top of the class along with the other imports
import static java.lang.Math.ceil;

// And then somewhere in the code...

// x is a number of type double such as 5.345
double y = ceil(x);

In the above fragment, I used the new static import feature to import the static method ceil() from the Math class. Now when I call the method, I don't have to qualify it with Math. If I wanted to use multiple static methods from the Math class, I could import them individually or all at once as shown below:

// Import all static methods from Math
import static java.lang.Math.*;


This also applies to any constants declared within Math, such as E and PI. With the above declaration, I can use these constants as if they were declared locally within my class (or superclass).

Use it when you require frequent access to static members from one or two classes. If you overuse the static import feature, it can make your program unreadable and unmaintainable, polluting its namespace with all the static members you import. Readers of your code (including you, a few months after you wrote it) will not know which class a static member comes from. Importing all of the static members from a class can be particularly harmful to readability; if you need only one or two members, import them individually. Used appropriately, static import can make your program more readable, by removing the boilerplate of repetition of class names.

Total Pageviews

Popular Posts