HTML

3 Difference between Web Server vs Application vs Servlet Containers in Java JEE

In the Java EE, or J2EE or JEE world, there is a lot of confusion between terminology, which is quite evident that now we have three words (J2EE, Java EE, and JEE) to describe the same technology platform. You will see Java developers using the terms like Web Server, Application Server, and Web containers interchangeably but they are not exactly same. The main difference between a Web server and an application server is that web server is meant to serve static pages e.g. HTML and CSS, while Application Server is responsible for generating dynamic content by executing server-side code e.g. JSP, Servlet or EJB. One of the most popular web servers is Apache HTTPD which is often used in conjunction with Tomcat to host many Java web application. Now, tomcat is not exactly an application server, it's more of a servlet engine or web container or also known as servlet containers because it provides the runtime environment for Servlet and JSP but doesn't provide the services like EJB and distributed transaction which are a key feature of the application server in Java JEE world.

How to Add Elements of two Arrays in Java - Example

One of the common programming exercise on various Java course is addition and multiplication of two arrays. How do you add two integer arrays in Java? Can you add two String array? how about other data types etc? These are some of the interesting questions because Java doesn't support operator overloading. You cannot use the plus operator to add two arrays in Java e.g. if you have two int arrays  a1 and a2, doing a3 = a1 + a2 will give compile time error. The only way to add two arrays in Java is to iterate over them and add individual elements and store them into a new array. This is also not easy because the array can be of different length, so you need to make some rules and apply them to your method e.g. you can throw IllegalArgumentException if you get two arrays which are not of the same type and their length is different.

How to count a number of words in given String in Java?

Can you write a method in Java that accepts a String argument and returns a number of words in it? A word is a sequence of one or more non-space characters i.e. any character other than '' (empty String). This should be your method signature:

public int count(String word);

This method should return 1 if the input is "Java" and return 3 if the input is "Java, C++, Python". Similarly a call to wordCount("    ") should return 0.  This is one of the several String algorithmic questions you can expect in a programming job interview.  This is used to test the coding skills of the candidate and that's why it's very important to prepare for these questions anytime you go for an interview.

Unsupported major.minor version 52.0 in Java + Eclipse + Linux [Solution]

The "unsupported major.minor version 52.0" error started to come after Java SE 8 release and the root cause of this error is trying to run a Java application compiled with JDK 8 into a JRE lower than Java SE 8 e.g. JRE 7 or JRE 6. This is very common because a developer has updated their compiler or IDE to Java SE 8 but many times their runtime is not upgraded to Java 8. If you remember, in Java you can run a class file compiled with a lower version say Java 6 to a higher version say JRE 8 because Java is backward compatible but vice-versa is not allowed. This make sense because Java SE 8 has features like lambda expressions, method reference, functional interface and new Date and Time API, which lower version JRE has no information. Depending upon where you getting this error e.g. Eclipse, NetBeans, IntelliJ IDEA or Android Studio, the solution could be different. All these IDEs has different settings for JRE but the bottom line is same, you need to configure these IDE to use JRE 8 to run the Java program compiled using Java 8.

4 ways of Session management in Servlet Java

Session tracking or Session management is an important feature of modern web applications which allows the server to remember it's clients. By keeping a session for each user, Server can serve the client better. It also helps in safety, security and personalization and must for certain kind of web applications e.g. e-commerce sites like Amazon or e-bay which stores item selected by the user for purchase in a shopping cart, even after the user is logged out. Since HTTP is a stateless protocol, there are no ways to know that two HTTP requests are related to each other i.e. they are coming from the same client or they are part of the same process. Session tracking is a mechanism that Servlets and Java Web application use to maintain state about a series of request from the same user across some period of time. By keeping a session, an e-commerce site can maintain add to card facility and also keep tracks of how you interact with the application. Since HTTP doesn't provide a default way to track Session, there are some non-standard ways to manage Sessions in Servlet JSP based application. Let's have a close look on them.

How to print Floyd's triangle in Java - Example Tutorial

In the last article, I have taught you how to print Pascal's triangle and in today's article I'll teach you how to print Floyd's triangle in Java program. Floyd's triangle is easier to print than Pascal's triangle because you don't need to take care of formatting the numbers as Floyd's triangle is a right angle triangle. It is named after American computer scientist Robert Floyd, who has also contributed Floyd–Warshall algorithm, which efficiently finds all shortest paths in a graph and Floyd's cycle-finding algorithm for detecting cycles in a sequence. If you remember, we use this algorithm to find the cycles in linked list. Coming back to Floyd's triangle, it is a right angle triangle which consists natural numbers starting from 1 in the first row. It then goes on with two numbers in second row, 3 numbers in 3rd row and so on. Along with other pattern based exercises and Pascal's triangle, Floyd's triangle is also a good programming exercise and often used in programming and training courses to teach how to program to beginners. It's one of the easier program but help you to build code sense and how to use basic programming constructs e.g. loop, operators and functions.

How to print Pascal Triangle in Java - Example Tutorial

Printing patterns with stars or numbers and triangles are some of the common programming exercises. Earlier we have seen how to print pyramid patterns with stars and today you will learn how to print Pascal's triangle in Java. Sometimes this problem is also asked as "write a program to print Pascal triangle without using array" or by just using for loop. Pascal’s triangle is a set of numbers arranged in the form of a triangle, similar to Floyd's triangle but their shape is different. Each number in the Pascal triangle row is the sum of the left number and a right number of the previous row. If a number is missing in the above row,  it is assumed to be 0. The first row starts with number 1, that's why you will see that the first two rows of the Pascal triangle just contain 1.