Introduction: Java 2 Platform, Enterprise Edition (J2EE), offers a rich set of technologies for developing enterprise applications, with Servlets being one of the foundational components. In this blog post, we'll delve into Servlets, exploring their role in J2EE and addressing the top 10 interview questions that frequently arise, complete with detailed answers and example code. What is a Servlet, and how does it differ from traditional CGI programs? Servlets are Java classes that extend the functionality of servers in hosting web applications accessed through a request-response model. They execute within the server's address space, offering improved performance and scalability over traditional CGI programs, which spawn a new process for each request. What are the key features of Servlets? Servlets boast several key features, including: Platform Independence: Servlets are written in Java and are thus platform-independent. Performance: Servlets execute within the server's add...
To find the second-highest salary from a list of employees using Java 8 streams, you can follow these steps: Create a list of employees with their salaries. Use Java 8 streams to sort the employees by salary in descending order. Skip the first element (which is the employee with the highest salary). Get the first element of the remaining stream (which is the employee with the second-highest salary). Example code: java import java.util.ArrayList; import java.util.List; class Employee { private String name; private double salary; public Employee (String name, double salary) { this .name = name; this .salary = salary; } public double getSalary () { return salary; } } public class SecondHighestSalary { public static void main (String[] args) { List<Employee> employees = new ArrayList <>(); employees.add( new Employee ( "John" , 60000.0 )); employees.add( new Employe...