Difference between jsp:include and jsp:forward action- Example

JSP provides standard actions to do things without using Java inside the scriptlet. Two of such standard actions, which help JSP to interact with other server resources e.g. another JSP, Servlet or HTML files are, include and forward actions. The <jsp:forward> action enables you to forward an HTTP request to a static HTML file, a servlet, or another JSP. It has an attribute called page, which accepts the URL of another resource as shown below:

<jsp:forward page="URL" /> 

How to sort a LinkedList in Java? Example Tutorial

Since LinkedList implements the java.util.List interface, you can sort the LinkedList by using Collections.sort() method, just like you sort an ArrayList. Since LinkedList class implements the linked list data structure which doesn't provide random access based upon the index, sorting is quite expensive. In order to access any element, you need to first traverse through that element which is O(n) operator. This method uses an efficient strategy to handle this scenario. It first copies the contents of LinkedList to an array, sorts the array and copies it back. So it's as efficient as sorting an ArrayList. By default Collections.sort() arrange elements of linked list into their natural order of sorting but it also accepts a Comparator, which can be used to sort elements in custom order. Java 8 also introduced a new sort() method on the java.util.List interface itself, which means you no longer need Collections.sort() to sort a LinkedList, you can do directly by calling the LinkedList.sort() method in Java 8. See Java SE 8 for Really Impatient to learn more about new features of Java 8. In this article, I'll show you a couple of examples of sorting LinkedList in Java.

java.lang.IllegalStateException: getWriter() has already been called for this response

This error comes when a Servlet calls the getOutputStream() method on response object for writing something after calling the include() method. Suppose a Servlet calls the include() method to load the response of a JSP. Since JSP has already written the response on it hence again opening OutputStream on response object is illegal, you get the java.lang.IllegalStateException: getWriter() has already been called for this response error. This error also comes when you try to include response of another Servlet and than tries to write something on output stream again. In short, your Servlet should never write anything on response object after calling the include() method. 

15 Technical Core Java Interview Questions Answers for Experienced Developers

When the experience of a Java Programmer grows in the years e.g. when it goes from beginner years ( 2 to 4) to more experience or sort of senior level ( 5 to 7 years), Core Java Interview Questions also change a bit. Of course, basics like data structure, algorithms, and object-oriented programming remains the same, but types of questions will become more advanced and their answers will definitely need to be more detailed and accurate. I often receive queries about core Java questions asked to a senior developer of 5 to 6-year experience, or, sometimes, I am going for an interview of senior Java developer, what kind of questions I should expect. This sometimes puzzles me, that once you become senior, you automatically start taking part in the interview, and you should have an idea of what to expect on Interviews, but at the same time, I can understand that having an idea of questions before going on Interview, helps preparation. Of course, you are not going to get a question like the one you have faced on 2 to 3 years level Java Interviews, but It also depends on different rounds of Interviews.

How to get Current Date and Time in Java with Example

You can get today's date or current date and time in Java by creating a new Date() object. A java.uti.Date object represents the current date and time instance in Java. Now, if you want to date in DDMMYYYY format or any other date format, you can easily convert the Date object to String by using SimpleDateFormat class. Since Date instance contains both date and time, if you only need a date then you can further convert the java.util.Date to java.sql.Date. Similarly, if you need just time then you can convert the java.util.Date to java.sql.Time, they represent the date and time part of java.util.Date object in Java. You can also use the java.util.Calendar class to get the today's date and time. Further, if you are using Java 8 then you can use new Date and Time API classes like LocalDate, LocalTime, and LocalDateTime to get the current date, time, and date-time in Java.

Can You Declare Constructor inside Servlet Class?

Yes, Servlet can have Constructor, it's perfectly legal but it's not the right way to initialize your Servlet. You should use the init() method provided by the Servlet interface to initialize the Servlet. If you remember, Servlet's are special in a sense that they are instantiated by the container and managed by the container. A servlet container like Tomcat creates a pool of multiple Servlets to serve multiple clients at the same time. They instantiate Servlet by calling the default no-argument constructor and suppose you have declared another constructor which takes a parameter e.g. HelloServlet(String name) than Java compiler will not add the default no-argument constructor and Servlet container will not able to initialize the Servlet. That's why it's important not to provide a constructor in Servlet, but if you do, make sure you also add a default constructor there for Servlet container.

Difference between HashSet and TreeSet in Java

Difference between HashSet and TreeSet in Java
There are several differences between an HashSet and a TreeSet is similar to what we discussed as a difference between TreeMap and HashMap. Anyway, Set and Map are two completely different interfaces so we will revisit those differences here. Probably the most important difference between HashSet and TreeSet is the performance. HashSet is faster than TreeSet which means if you need performance use HashSet but HashSet doesn't provide any kind of ordering so if you need ordering then you need to switch to TreeSet which provides sorting of keys. Sorting can be natural order defined by a Comparable interface or any particular order defined by Comparator interface in Java.

How to Increase Heap Size of Java Program running in Eclipse

It is possible to increase the heap size allocated by the JVM by using command line options Here we have 3 options

-Xms<size>        to set the initial Java heap size
-Xmx<size>        to set the maximum Java heap size
-Xss<size>          to set the java thread stack size

$ java -Xms1G -Xmx2G MainClass

In the above line, we have set the minimum heap size to 1GB and maximum heap size to 2GB.

How to Convert Vector to Array in Java? 2 Examples

In the last article, we have learned how to convert a LinkedList to array in Java and in today's tutorial you will learn how to convert a Vector to array in Java. There is a similarity between Vector and LinkedList, both implements the List interface. Even though Vector is a legacy class and exists in Java from JDK 1.1 long before Java Collection framework was introduced, it was later retrofitted to implement the List interface. You can use the same toArray() method to convert a Vector to an array in Java. Similar to LinkedList, you can convert Vector of any object to an array of the corresponding object, the only restriction is that you cannot convert a vector of Integers to an array of ints or Vector of Float objects to an array of floats, which applies to our linked list solution as well.

What is Inheritance in Java with example - Object Oriented Programming Tutorial

What is Inheritance in Java
Inheritance in Java or OOPS (Object oriented programming) is a feature which allows coding reusability.  In other words, Inheritance  self-implies inheriting or we can say acquiring something from others. Along with Abstraction, Encapsulation and Polymorphism, Inheritance forms the backbone of Object oriented programming and Java.  In Java, we use the term inheritance when one object acquires some property from other objects. In Java, inheritance is defined in terms of superclass and subclass. it is normally used when some object wants to use existing feature of some class and also want to provide some special feature, so we can say inheritance has given the advantage of reusability.

How to loop over TreeSet in Java with example

In our earlier articles, we have learned how to loop over ArrayList in Java and you can use the same logic to loop over a TreeSet. You can use both, enhanced for loop and Iterator to traverse over TreeSet in Java. Though, worth noting is that Iterator returned by the iterator() method of TreeSet returns elements in the sorted order which is imposed by the Comparator you have provided to TreeSet at the time of instantiation. By default, if you don't provide any custom Comparator than TreeSet sorts elements in their natural order e.g. String elements are sorted in alphabetic order and Integer elements are sorted in numeric order. When you iterate over a TreeSet the iterator follows this order.