Monday, June 30, 2014

Round a double to 2 decimal places

I think this is easier:
double time = 200.3456;
DecimalFormat df = new DecimalFormat("#.##");      
time = Double.valueOf(df.format(time));

System.out.println(time); // 200.35
Note that this will actually do the rounding for you, not just formatting.

Friday, June 27, 2014

MongoDB GUI client (cross-platform or Linux)

Wait for Robomongo - crossplatform MongoDB GUI client. Right now they published only Windows version and Mac OS X version coming soon (as noted on the site). I'm sure Linux version will be also available soon.
Update: Mac OS X and Linux versions released.
enter image description here

Thursday, June 26, 2014

Analogy for REST vs. SOAP

The letter analogy
A nice analogy for REST vs. SOAP is mailing a letter: with SOAP, you're using an envelope; with REST, it's a postcard. Postcards are easier to handle (by the receiver), waste less paper (i.e., consume less bandwidth), and have a short content. (Of course, REST requests aren't really limited in length, esp. if they use POST rather than GET.)
But don't carry the analogy too far: unlike letters-vs.-postcards, REST is every bit as secure as SOAP. In particular, REST can be carried over secure sockets (using the HTTPS protocol), and content can be encrypted using any mechanism you see fit. Without encryption, REST and SOAP are both insecure; with proper encryption in place, both are equally secure.

http://rest.elkstein.org/2008/02/how-simple-is-rest.html

Tuesday, June 24, 2014

How do I get the file extension of a file in Java?

In this case, use FilenameUtils.getExtension from Apache Commons IO
Here is an example of use:
String ext = FilenameUtils.getExtension("/path/to/file/foo.txt");
http://stackoverflow.com/questions/3571223/how-do-i-get-the-file-extension-of-a-file-in-java

Tuesday, June 17, 2014

Accessing a value in Struts ActionForm from JSP

Try
<h1>Hello  <bean:write name="RegistrationForm" property="userid" />!</h1>
it doesn't matter what the internal name / coding of your formbean is. All what matters is how the getters and setters are named. Your getter is named getUserid() to the javabean property is userid.

Wednesday, June 11, 2014

What is a MIME type?

A MIME type is a label for a given type of data so software can know how to handle the data. It serves the same purpose on the Internet that file extensions do on Microsoft Windows.
So if a server says "This is text/html" the client can go "Ah, this is an HTML document, I can render that internally", while if the server says "This is application/pdf" the client can go "Ah, I need to launch the FoxIt PDF Reader plugin that the user has installed and that has registered itself as the application/pdf handler."

Are semantics and syntax the same?

Syntax is the grammar. It describes the way to construct a correct sentence. For example, this water is triangular is syntactically correct.
Semantics relates to the meaning. this water is triangular does not mean anything, though the grammar is ok.
Talking about the semantic web has become trendy recently. The idea is to enhance the markup (structural with HTML) with additional data so computer could make sense of the web pages more easily.

Monday, June 9, 2014

What is “runtime”?

As per Wikipedia: runtime library/run-time system.
In computer programming, a runtime library is a special program library used by a compiler, to implement functions built into a programming language, during the runtime (execution) of a computer program. This often includes functions for input and output, or for memory management.

A run-time system (also called runtime system or just runtime) is software designed to support the execution of computer programs written in some computer language. The run-time system contains implementations of basic low-level commands and may also implement higher-level commands and may support type checking, debugging, and even code generation and optimization. Some services of the run-time system are accessible to the programmer through an application programming interface, but other services (such as task scheduling and resource management) may be inaccessible.

Re: your edit, "runtime" and "runtime library" are two different names for the same thing.

How do I provide JVM arguments to VisualVM?

Should be able to modify the memory settings in %JDK_HOME%\lib\visualvm\etc\visualvm.conf
Xms and Xmx are in the default_options line.

Thursday, June 5, 2014

What are the roles of EJBs in Java EE?

As of Java EE 5, EJBs are really just ordinary Java objects with a few additional behaviours added, primarily:
  1. Dependencies are automatically injected into their fields
  2. Their methods are automatically exported to remote clients via RMI
  3. Security checks are automatically applied to invocations of their methods
  4. Transactions are automatically wrapped around invocations of their methods
There are a few other services that are specifically available to EJBs, such as asynchronous invocations, timers, and coupling to message queues.
If you think any of those things would be useful, then you should consider using EJBs. If you're learning about Java EE, then you definitely need to learn about EJBs, so that you can make an informed decision about whether to use them or not.
It's worth noting that in modern Java EE, there is a standard called Contexts and Dependency Injection (CDI) which can be used to obtain many of the benefits of EJBs, and may be preferable. You should definitely also learn about CDI.
Many web applications are built without any EJBs at all. Most web applications user either CDI or some earlier, non-standard, equivalent such as Spring or Guice.
Hibernate is an implementation of the Java Persistence Architecture (JPA) standard. JPA is about persisting objects to a database, and provides completely different services to EJB. You would use JPA for your entity objects (which capture the structure and fundamental behaviour of your data), and EJB or CDI for your service objects (which capture the specific business logic of your application).
Earlier versions of EJB did also provide persistence of EJBs, and so had some overlap with JPA, but that is only of historical interest now.

What does flushing the buffer mean?

Consider writing to a file. This is an expensive operation. If in your code you write one byte at a time, then each write of a byte is going to be very costly. So a common way to improve performance is to store the data that you are writing in a temporary buffer. Only when there is a lot of data is the buffer written to the file. By postponing the writes, and writing a large block in one go, performance is improved.
With this in mind, flushing the buffer is the act of transferring the data from the buffer to the file.
Does this clear the buffer my deleting everything in it or does it clear the buffer by outputing everything in it?
The latter.

Wednesday, June 4, 2014

Reasons to store users' data in LDAP instead of RDBMS

Interoperability, as has already been mentioned, is very much in LDAP's favour with some types of server software, although much of the software that integrate with LDAP require a specific schema so it's not necessarily as simple as just installing and configuring an LDAP service and off you go - you might need to add new elements in the schema for each app you want to interact with, and each application might have different limitations with regard to authentication (for example plain text password fields, password fields as MD5 hashes or SHA hashes with a salt, etc).
A good LDAP service requires a fair bit of configuration knowledge too, more so than creating a simple schema in something like like MySQL. SQL DB's are still a fairly interoperable option, LDAP support is not as uniquely dominant as it once was. Many applications (like Apache) and operating systems (like Linux's PAM) can authenticate against SQL DB's just as easily as LDAP servers as it's all handled by drivers that abstract the interface.
Where LDAP really shines is scalability. If you specifically want a place to hold user accounts for authentication and want to scale to multiple replicated servers and handle tends of thousands of authentication requests a second, LDAP is an excellent option. It's not that modern RDBMS's aren't good enough to do this, it's just that LDAP is (typically) even better because of the way it cascades replication through different tiers. You can do that with a few RDBMS replication systems, but in practice not all (particularly when it comes to doing it reliably with dozens of servers and really huge authentication databases, where queries tend to be mostly read operations so where one way replication is an acceptable model).
Really though, looking at an LDAP server is something to consider if you have a specific need to do so, like aspecific application you want to be able to interoperate with, or if you are building a highly scalable authentication system (e.g. for an ISP or for a super-scalable web application - where you plan on having more than a couple servers dedicated just to authentication, and where they may be spread across the country or even across the globe).
The point someone has already made about having an LDAP front end on an RDBMS is very good one. A few companies - including Oracle (who have a vested interest, obviously) - have products that do specifically that. If you don't want the overhead of managing an LDAP service, or if you just want to manage all your users in a DB you can create views/joins with, but think you might need an LDAP service later, than it's a perfectly valid option. OpenLDAP supports a shell back end which can take data from any source, including an RDBMS (e.g. I've used it with MySQL).
In summary, LDAP's great, but it's situation specific to interoperability and extreme scalability. If you have limited resources to manage and support one, it might not be worth the hassle of supporting, but if you are planning services like UNIX hosted POP/IMAP/SMTP or other third party software integration then it's certainly worth doing (and may even be your only option).
Oh and lastly be wary of what LDAP server you use if you do decide to implement one! They are not all created equal and the differences between them (in terms of performance and ease of management & configuration) can be quite stark.
OpenLDAP is a pretty safe bet, scales well and is fairly easy to use. Some applications work best / come with specific configuration files for a specific LDAP server (e.g. a lot of software on Solaris assumes you are using Sun ONE directory server) which you might not otherwise want to use (because it doesn't perform as well, or is a pig to configure, etc).

What is the difference between Composition and Association relationship?

COMPOSITION
Imagine a software firm that is composed of different Business Units (or departments) like Storage BU, Networking BU. Automobile BU. The life time of these Business Units is governed by the lifetime of the organization. In other words, these Business Units cannot exist independently without the firm. This is COMPOSITION. (ie the firm is COMPOSED OF business units)
ASSOCIATION
The software firm may have external caterers serving food to the employees. These caterers are NOT PART OF the firm. However, they are ASSOCIATED with the firm. The caterers can exist even if our software firm is closed down. They may serve another firm! Thus the lifetime of caterers is not governed by the lifetime of the software firm. This is typical ASSOCIATION
AGGREGATION
Consider a Car manufacturing unit. We can think of Car as a whole entity and Car Wheel as part of the Car. (at this point, it may look like composition..hold on) The wheel can be created weeks ahead of time, and it can sit in a warehouse before being placed on a car during assembly. In this example, the Wheel class's instance clearly lives independently of the Car class's instance. Thus, unlike composition, in aggregation, life cycles of the objects involved are not tightly coupled.

Tuesday, June 3, 2014

What exactly we mean by Precompiled SQL Statement

If a statement is used multiple times in a session, precompiling it provides better performance than sending it to the database and compiling it for each use. The more complex the statement, the greater the performance benefit.
If a statement is likely to be used only a few times, precompiling it may be inefficient because of the overhead involved in precompiling, saving, and later deallocating it in the database.
Precompiling a dynamic SQL statement for execution and saving it in memory uses time and resources. If a statement is not likely to be used multiple times during a session, the costs of doing a database prepare may outweigh its benefits. Another consideration is that once a dynamic SQL statement is prepared in the database, it is very similar to a stored procedure. In some cases, it may be preferable to create stored procedures and have them reside on the server, rather than defining prepared statements in the application.

http://www.coderanch.com/t/299852/JDBC/databases/Precompiled-SQL-Statement