Httpurlconnection - How to use java.net.URLConnection to fire and handle HTTP requests. First a disclaimer beforehand: the posted code snippets are all basic examples. You'll need to handle trivial IOExceptions and RuntimeExceptions like NullPointerException, ArrayIndexOutOfBoundsException and consorts yourself. Preparing We first need to know at least the URL and the charset. The parameters are optional and depend on the functional requirements. String url = " charset = "UTF-8";String param1 = "value1";String param2 = "value2";// ...String query = String.format("param1=%s¶m2=%s", URLEncoder.encode(param1, charset), URLEncoder.encode(param2, charset)); The query parameters must be in name=value format and be concatenated by &.
You would normally also URL-encode the query parameters with the specified charset using URLEncoder#encode(). The String#format() is just for convenience. Firing a HTTP GET request with (optionally) query parameters It's a trivial task. URLConnection connection = new URL(url + "? " Actually firing the HTTP request User-Agent. Java - Examples of GoF Design Patterns. Factory method java.util.Collection#Iterator is a good example of a Factory Method. Depending on the concrete subclass of Collection you use, it will create an Iterator implementation. Because both the Factory superclass (Collection) and the Iterator created are interfaces, it is sometimes confused with AbstractFactory.
Most of the examples for AbstractFactory in the the accepted answer (BalusC) are examples of Factory, a simplified version of Factory Method, which is not part of the original GoF patterns. In Facory the Factory class hierarchy is collapsed and the factory uses other means to choose the product to be returned. Abstract Factory An abstract factory has multiple factory methods, each creating a different product. Java IAQ: Infrequently Answered Questions. By Peter Norvig Q: What is an Infrequently Answered Question? A question is infrequently answered either because few people know the answer or because it is about an obscure, subtle point (but a point that may be crucial to you). I thought I had invented the term, but it also shows up at the very informative About.com Urban Legends site.
There are lots of Java FAQs around, but this is the only Java IAQ. (There are a few Infrequently Asked Questions lists, including a satirical one on C.) Q:The code in a finally clause will never fail to execute, right? Well, hardly ever. Q:Within a method m in a class C, isn't this.getClass() always C? No. Q: I defined an equals method, but Hashtable ignores it. Equals methods are surprisingly hard to get right. Q: I tried to forward a method to super, but it occasionally doesn't work. This is the code in question, simplified for this example: You need to be careful when passing to super that you fully understand what the super method does. Absolutely not. Inversion of Control Containers and the Dependency Injection pattern.
In the Java community there's been a rush of lightweight containers that help to assemble components from different projects into a cohesive application. Underlying these containers is a common pattern to how they perform the wiring, a concept they refer under the very generic name of "Inversion of Control". In this article I dig into how this pattern works, under the more specific name of "Dependency Injection", and contrast it with the Service Locator alternative. The choice between them is less important than the principle of separating configuration from use. One of the entertaining things about the enterprise Java world is the huge amount of activity in building alternatives to the mainstream J2EE technologies, much of it happening in open source.
A lot of this is a reaction to the heavyweight complexity in the mainstream J2EE world, but much of it is also exploring alternatives and coming up with creative ideas. Components and Services A Naive Example Inversion of Control.