Top 150+ Java Servlet Interview Questions and Answers - 1

Question: 1

What are Page directives?

Page directive is used to define page attributes the JSP file. Below is a sample of the same:

<%@ page language = “Java” import= “java.rmi.*,java.util.*” session = “true” buffer=”12kb” autoFlush=”true” errorPage=”error.jsp” %>

To summarize some of the important page attributes:

import: Comma separated list of packages or classes, just like import statements in usual Java code.

session: Specifies whether this page can use HTTP session.

buffer: If a buffer size is specified (such as “50kb”) then output is buffered with a buffer size not less than that value.

isThreadSafe: Defines the level of thread safety implemented in the page. If set “true” the JSP engine may send multiple client requests to the page at the same time. If “false” then the JSP engine queues up client requests sent to the page for processing, and processes them one request at a time, in the order they were received. This is the same as implementing the javax.serlvet.SingleThreadModel interface in a servlet.

errorPage: Defines a URL to another JSP page, which is invoked if an unchecked runtime exception is thrown. The page implementation catches the instance of the Throwable object and passes it to the error page processing.

Question: 2

What are the different ways we can maintain state between requests?

Following are the different ways of maintaining states between stateless requests:

URL rewriting

Cookies

Hidden fields

Sessions

Question: 3

What is Session Hijacking?

If your application is not very secure, then it is possible to get the access of system after acquiring or generating the authentication information.

Session hijacking refers to act of taking control of a user session after successfully obtaining or generating an authentication session ID. It involves an attacker using captured, brute forced or reverse engineered session IDs to get a control of a legitimate user’s Web application session while that session is still in progress.

Question: 4

How do you delete a Cookie within a JSP?

Cookie mycook = new Cookie (“name” , “value”) ;

response.addCookie (mycook) ;

Cookie killmycook = new Cookie (“mycook” , “value”) ;

killmycook.setMaxAge(0) ;

killmycook.setpath (“ / ”) ;

killmycook.addCookie (killmycook) ;

Question: 5

What is meant by web application?

A web application is a collection of servlets and contents installed under a specific subset of the server’s URL namespace such as / catalog and possibly installed via a .war file.

Related Questions