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

This error comes when you call include() or forward() method after calling  the getOutputStream() from ServletResponse object and writing into it.  This error is similar to java.lang.IllegalStateException: getWriter() has already been called for this response error, which we have seen in the earlier article.

This is the exception:
org.apache.jasper.JasperException: java.lang.IllegalStateException: getOutputStream() has already been called for this response

and here is the root cause :

6 Difference between forward() and sendRedirect() in Servlet - Java

Servlet in JEE platform provides two methods forward() and sendRedirect() to route an HTTP request to another Servlet for processing. Though, both are used for forwarding HTTP requests for further processing there are many differences between forward() and sendRedirect() method e.g. forward is performed internally by Servlet, but a redirection is a two-step process, where Servlet instruct the web browser (client) to go and fetch another URL, which is different from the original. That's why forward() is also known as a server-side redirect and sendRedirect() is known as the client-side redirect. Becuase of their usefulness, the difference between forward() and sendRedirect is also a frequently asked Servlet interview question. Let's see a couple of more differences to answer this question better.

Top 21 Java Inheritance Interview Questions and Answers

Last week I wrote about some good Java OOP concept questions and In this article, I am going to share some frequently asked Inheritance based Java Interview questions and answers. Inheritance is one of the most important Object oriented concepts along with Abstraction, Encapsulation, and Polymorphism. Since most popular languages are object oriented e.g. Java and C++, you will always find a couple of questions from OOP concepts, particularly from Polymorphism and Inheritance. It's expected from a Java developers to know about these OOP concepts and have an understanding of when to use them e.g. many times Composition is the better choice than Inheritance because of flexibility it offers but when it comes to leverage polymorphism of type, you have to use Inheritance. In Java inheritance is supported by language using extends and implements keyword.

How to get ArrayList from Stream in Java 8 - Collectors Example, Tutorial

You can use Collectors.toList(), toSet() and toMap() to get all elements of Stream into any Collection e.g. List, Set or Map, but if you want to get a particular collection e.g. ArrayList, then you need to use Collectors.toCollection(ArrayList::new) method. This method first creates an ArrayList using method reference and then adds all elements of Stream into the ArrayList. It's very useful if you have a long list of String and you want to create a smaller list containing only String starting with the letter "b" e.g. "Bluehost". All you need to do is first get the stream from List by calling stream() method, then call the filter() method to create a new Stream of filtered values and finally call the Collectors.toCollection(ArrayList::new) to collect those elements into an ArrayList. Let's see a couple of examples to understand the concept better.

How to connect Eclipse to Oracle database - Step by Step Guide

Though I prefer Toad or Oracle SQL Developer tool to connect Oracle database, sometimes it's useful to directly connect Eclipse to Oracle using JDBC using its Data Source Explorer view. This means you can view data, run SQL queries to the Oracle database right from your Eclipse window. This will save a lot of time wasted during switching between Toad and Eclipse or Oracle SQL Developer and Eclipse. Eclipse also allows you to view Execution plan in both text and Graphical mode, which you can use to troubleshoot performance of your SQL queries. In this article, I'll tell you steps to connect Eclipse to Oracle database. Since Eclipse uses Java to connect to Oracle database, it asks for JDBC driver information. Since connecting using thin Oracle JDBC driver is much easier, as you just need to drop a JAR file in the classpath, we will be using JDBC thin driver to connect Oracle from Eclipse. Depending upon your Oracle version e.g. Oracle 10g or Oracle 11g you need to download Oracle JDBC thin driver e.g. ojdbc14.jar file. Just download this JAR file and configure it on Eclipse IDE. Enter username, password, and sid and you are ready to execute SQL query to Oracle database from Eclipse.

How to convert java.util.Date to java.sql.Timestamp in Java - JDBC Example

You can convert java.util.Date to java.sql.Timestamp by first taking the long millisecond value using the getTime() method of Date class and then pass that value to the constructor of Timestamp object. Yes, it's as simple as that. For better code reusability and maintenance, you can create a DateUtils or MappingUtils class to keep these kinds of utility or mapping functions. Now, the questions comes, why do you need to convert java.util.Date to java.sql.Timestamp? Well, If you are storing date values to database using JDBC, you need to convert a java.util.Date to its equivalent java.sql.Timestamp value. Even though both of them represent date + time value and can be stored in DATETIME SQL type in Microsoft SQl Server database or equivalent in other databases like Oracle or MySQL, there is no method in JDBC API which takes the java.util.Date object. Instead, you have got three separate methods to set DATE, TIME, and TIMESTAMP in the java.sql package.

Difference between FileReader vs FileInputStream in Java?

Even though both FileReader and FileInputStream are used to read data from a file in Java, they are quite a different. The main difference between the FileReader and FileInputStream is that one read data from character stream while other read data from a byte stream. The FileReader automatically converts the raw bytes into character by using platform's default character encoding. This means you should use this class if you are reading from a text file which has same character encoding as the default one. If you happen to read a text file encoded in different character encoding then you should use InputStreamReader with specified character encoding. An InputStreamReader is a bridge between byte stream and character stream and can take a FileInputStream as a source. Though, it's worth remembering that it caches the character encoding which means you cannot change the encoding scheme programmatically.

How to convert Date to LocalDateTime in Java 8 - Example Tutorial

The LocalDateTime class has introduced in Java 8 to represents both date and time value. It's local, so date and time is always in your local time zone. Since java.util.Date has been widely used everywhere in many Java applications, you will often find yourself converting java.util.Date to LocalDate, LocalTime and LocalDateTime classes of java.time package. Earlier I have shown you how to convert Date to LocalDate and today, I am going to teach you how to convert Date to LocalDateTime in Java 8. The approach is same. Since the equivalent class of java.util.Date in new Date and Time API is java.time.Instant, we first convert Date to Instance and then create LocalDateTime instance from that Instant using System's default timezone.

How to use flatMap() in Java 8 - Stream Example Tutorial

In order to understand the flatMap() method, you first need to understand the map() function of Java 8. The map() function is declared in  the class and uses to transform one Stream into another e.g. a stream of integer numbers into another stream of ints where each element is the square of the corresponding element in source stream. In the map() function, a function is applied to each element of Stream and return values are inserted into a new Stream. The key point to note here is that the function used by map() operation returns a single value. Now, if the map operation uses a function which instead of returning a single value returns a Stream of values e.g. prime factors of the number then you have a Stream of Stream of integers. The flatMap() method is used to flatten that stream into a Stream of integers. For example, suppose, you have a list of numbers e.g. [21, 23, 42] and we use getPrimeFactors() method along with the map() operation to transform this stream. The result would be [[3,7],[23],[2,3,7]]. If you want to flat this stream of a stream into a stream of values, you can use the flatMap() which will return [3,7,2,3,2,3,7].