#Servlets #JSP #JSTL #Interview Questions
Interview Questions: Servlets, JSP, and JSTL
Table of Contents
- What is a «servlet»?
- What advantages do servlets have over CGI (Common Gateway Interface)?
- What is the structure of a web project?
- What is a «servlet container»?
- Why are application servers needed if there are servlet containers?
- How does a servlet container manage the servlet lifecycle?
- What is a «deployment descriptor»?
- What actions are required to create servlets?
-
When is it necessary to override the
service()
method? - Is there a point in defining a constructor for a servlet? How should data be initialized?
-
Why is it necessary to override only the
init()
method without arguments? - What are the most common tasks performed in the servlet container?
- What can you tell me about servlet filters?
- Why are various listeners used in servlets?
- When should servlet filters be used, and when should listeners be used?
- How can a servlet be executed concurrently with the application startup?
- How to handle exceptions thrown by another servlet?
-
What is
ServletConfig
? -
What is
ServletContext
? -
What are the differences between
ServletContext
andServletConfig
? -
What is the purpose of the
ServletResponse
interface? -
What is the purpose of the
ServletRequest
interface? -
What is a
Request Dispatcher
? - How can one servlet call another servlet?
-
What is the difference between
sendRedirect()
andforward()
? - What are servlet attributes used for, and how do they work?
- How can deadlock occur in a servlet?
- How to obtain the actual location of a servlet on the server?
- How to retrieve information about the server from a servlet?
- How to get a client’s IP address on the server?
- What wrapper classes for servlets do you know?
-
What are the differences between
GenericServlet
andHttpServlet
? -
Why is the
HttpServlet
class declared as abstract? -
What main methods are present in the
HttpServlet
class? - Should you worry about multithreading safety when working with servlets?
- Which HTTP method is not idempotent?
- What methods are available for sending data from the client to the server?
-
What is the difference between
GET
andPOST
methods? -
What is the difference between
PrintWriter
andServletOutputStream
? -
Can
PrintWriter
andServletOutputStream
be used in the same servlet? -
Tell me about the
SingleThreadModel
interface. - What does URL encoding mean? How do it in Java?
- What different session management methods in servlets do you know?
- What are cookies?
- What methods for working with cookies are provided in servlets?
- What is URL Rewriting?
-
What are the purposes and differences between the methods
encodeURL()
andencodeRedirectURL()
? - What is a «session»?
- How to notify an object in a session that the session is invalid or has ended?
- What effective way exists to ensure that all servlets are available only to users with valid sessions?
- How can we ensure transport layer security for our web application?
- How to organize database connection and logging in a servlet?
- What are the main features in the Servlet 3 specification?
- What types of authentication are available to a servlet?
- What is Java Server Pages (JSP)?
- What is the purpose of JSP?
- Describe how JSP pages are processed, from request to response.
- Tell us about the stages (phases) of the JSP lifecycle.
- Discuss the methods of the JSP lifecycle.
- Which JSP lifecycle methods can be overridden?
- How can direct access to a JSP page from the browser be prevented?
- What is the difference between dynamic and static content in JSP?
- How to comment code in JSP?
- What are the main types of JSP tags?
- What can you tell me about JSP actions (Action tag and JSP Action Elements)?
- How to extend the functionality of JSP?
- What do you know about writing custom JSP tags?
- Provide an example of using custom tags.
- How to create line breaks in HTML using JSP?
-
Why is it unnecessary to configure standard JSP tags in
web.xml
? - How to handle errors on JSP pages?
- How is error handling performed with JSTL?
- How is JSP configured in the deployment descriptor?
- Is it possible to use JavaScript on a JSP page?
- Is a session object always created on a JSP page, and can its creation be disabled?
-
What is the difference between
JSPWriter
and servletPrintWriter
? - Describe general practical principles of working with JSP.
…
What is a «servlet»?
A servlet is an interface whose implementation extends the capabilities of a server. Servlets interact with clients via the request-response mechanism. While servlets can cater to any request, they are primarily used to extend web servers.
The majority of classes and interfaces necessary for creating servlets are contained within the javax.servlet
and javax.servlet.http
packages.
The primary methods of a servlet include:
public void init(ServletConfig config) throws ServletException
, which is invoked immediately after the servlet is loaded into memory;public ServletConfig getServletConfig()
, which returns a reference to the object that grants access to the servlet’s configuration information;public String getServletInfo()
, returning a string with information about the servlet, such as its author and version;public void service(ServletRequest request, ServletResponse response) throws ServletException, java.io.IOException
, called to process each request;public void destroy()
, executed before the servlet is unloaded from memory.
The current specification – Servlet 3.1 is outlined in JSR-340 and was adopted in 2013.
What advantages do servlets have over CGI (Common Gateway Interface)?
- Servlets offer improved request processing performance and more efficient memory use thanks to multithreading (a new thread is created for each request, which is faster than creating a new object for each request as is the case in CGI).
- Servlets are platform-independent. Therefore, a web application developed using servlets can run on any servlet container that complies with the standard and on any operating system.
- The use of servlets enhances program reliability since the servlet container manages the lifecycle of servlets (thus taking care of potential memory leaks), security, and garbage collection.
- Servlets are relatively easy to learn and maintain, allowing developers to concentrate solely on the application’s business logic rather than the inner workings of web technologies.
What is the structure of a web project?
The typical structure of a web project can include the following directories:
src/main/java
: Application/library source filessrc/main/resources
: Resource files for the application/librarysrc/main/filters
: Servlet filter filessrc/main/webapp
: Web application source filessrc/test/java
: Test source filessrc/test/resources
: Resource files for testssrc/test/filters
: Tests for servlet filterssrc/it
: Integration testssrc/assembly
: Assembly descriptionsrc/site
: SiteLICENSE.txt
: Project licenseNOTICE.txt
: Notes and definitions of dependency librariesREADME.txt
: Project description
What is a «servlet container»?
A servlet container is a program that serves as a server managing the system support for servlets and handles their lifecycle according to the rules defined in the specifications. It can function as a standalone web server, serve as a page provider for another web server, or integrate with a Java EE application server.
The servlet container facilitates data exchange between the servlet and clients, taking on responsibilities like creating a runtime environment for executing the servlet, and identifying and authorizing clients, as well as organizing sessions for each of them.
Notable servlet container implementations include:
- Apache Tomcat
- Jetty
- JBoss
- WildFly
- GlassFish
- IBM WebSphere
- Oracle WebLogic
Conclusion: Key Concepts and Best Practices for Servlets, JSP, and JSTL
Servlets and JSP (JavaServer Pages) are fundamental technologies within Java EE, used to create dynamic web applications. Understanding how servlets work, how to manage their lifecycle, and how JSP integrates with servlets can significantly enhance web application development. Utilizing JSTL for managing XML data, internationalization, and conditionals can further streamline coding practices, making applications more robust and easier to maintain.
When preparing for interviews, focus on the lifecycle methods of servlets and JSP, the differences between servlets and JSP, and the advantages of using JSTL. SQL and database connectivity, error handling, and servlet filters are also vital areas to cover. A deep understanding of these concepts will not only help in interviews but will also be beneficial in practical applications of Java EE technologies.
By mastering servlets, JSP, and JSTL, developers can build powerful and efficient web applications that meet modern user demands.