Skip to main content

JSP Interview Questions part - 2



JSP Interview Questions




1. How will you implement error page in JSP?
To implement an error-handling page in JSP, we first create a JSP with error page handling information. In most of the cases we gracefully handle error by giving a user-friendly message like “Sorry! There is system error. Please try again by refreshing page.”
In this error page, we show user-friendly message to user, but we also log important information like stack trace to our application log file. We have to add parameter isErrorPage=true in page directive of this page. This tells to JSP container that this is our error page.
<%@page isErrorPage=”true” %>
Now we can use this error page in other JSP where we want to handle error. In case of an error or exception, these JSP will direct it to errorPage.
           <% page errorPage=”ErrorPage.jsp” %>

2. How will you send XML data from a JSP?
In general, JSP is used to pass HTML data to web browser. If we want to send data in XML format, we can easily do it by setting contentType=”text/xml” in page directive.
E.g. <%@page contentType=”text/xml” %>

3. What happens when we request for a JSP page from web browser?
When a user calls JSP page from web browser, the request first comes to web server. Web server checks for .jsp extension of page and passes the request to JSP container like Tomcat.
The JSP container checks whether it has precompiled JSP class or not. If this is the first time this JSP is called, then JSP container will translate JSP into a servlet and compiles it. After compiling, JSP code if loaded in memory and JSP container will call jspInit() method and _jspService() methods.
The _jspService() method will create the output that will be sent by JSP container to client browser.

4. How will you implement Auto Refresh of page in JSP?
We can use setIntHeader() method to set the refresh frequency with which we want to auto-refresh a JSP page. We can send key “Refresh” with the time in seconds for auto refresh of the JSP page.
E.g. response.setIntHeader(“Refresh”,10)

5.What are the important status codes in HTTP?
Every HTTP request comes back with a status code from the server. The important status codes in HTTP are as follows:
1. 200: It means the request is successful.
2. 400: It means the request was bad.
3. 401: It means request was not authorized.
4. 404: It means the resource requested was not found.
5. 503: It means the service is not available.

6. What is the meaning of Accept attribute in HTTP header?
In HTTP header, Accept attribute is used to specify the MIME types that a HTTP client or browser can handle. MIME type is the identifier for specifying the type of file/data that we are planning to pass over the internet.

7. What is the difference between Expression and Scriptlet in JSP?
We use Expression in a JSP to return a value and display it at a specific location. It is generally used for dynamically print information like- time, counter etc in a HTML code. Scriptlet is for writing Java code in a JSP. We can define variable, methods etc in a Scriptlet. A Scriptlet can handle much more complex code and can be also reused.

8. How will you delete a Cookie in JSP?
We can use following options to delete a Cookie in JSP:
setMaxAge(): we can set the maximum age of a cookie. After this time period, Cookie will expire and will be deleted.
Header: We can also set the expiry time in header of response. Respone.setHeader(). This will also expire the cookie after specified time period.

9. How will you use a Cookie in JSP?
We can use a Cookie in JSP by performing following steps: First we create a Cookie object. We set the name and value of the cookie to be created. We set the expiry time of the Cookie by setting the maximum age. We can use setMaxAge() method for this.
Finally, we can send the cookie in a HTTP Response by sending it in HTTP header. In this way cookie goes to client browser and gets stored there till the maximum age is not achieved. Once a Cookie is set in the client browser, we can call getCookies() method to get the list of all the cookies set in Client. We iterate through the list of all the cookies and get the value of the cookie that was set in earlier request.
In this way we can use Cookie to set some information at client side and retrieve its value.

10. What is the main difference between a Session and Cookie in JSP?
A Session is always stored at the Server side. In JSP, session is a built-in object in JSP container. A Cookie is always stored at the client side.
We can use both the methods for Session   tracking . But Cookie method needs  permission from  user for storing  cookie  at  the client  location.

11. How will you prevent creation of session in JSP?
We can simply set the session attribute as false in page directive to prevent creation of session object.
E.g. <% @page session=”false” %>

12. What is an output comment in JSP?
We can write output in JSP in such a way that it becomes a comment in HTML code. This comment will not be visible in the web browser. But when we view page source to see HTML, we can see output comment.
An HTML comment is of following format:
<!-- comment -->
If we output comment in above format, it will be visible to client.

13. How will you prevent caching of HTML output by web browser in JSP?
We can use set the header in response object for Cache-Control to specify no caching.
Sample code is as follows:
response.setHeader(“Cache-Control”, “no-store”);

response.setDateHeader(“Expires”,”0”);


14. How will you redirect request to another page in browser in JSP code?
We can use sendRedirect() method in JSP to redirect the request to another location or page. In this case the request will not come back to server. It will redirect in the browser itself.
Sample code is as follows:
<% response.sendRedirect(URL); %>

15. What is the difference between sendRedirect and forward in a JSP?
Both forward and sendRedirect are mechanisms of sending a client to another page. The main difference between these two are as follows:
In forward, the processing takes place at server side. In case of sendRedirect() the processing takes place the client side.
In forward, the request is transferred to another resource within same server. In case of sendRedirect the request can be transferred to resource on some other server.
In forward only one request call is consumed. In case of sendRedirect two request response calls are created and consumed.
The forward is declared in RequestDispatcher interface. Where as sendRedirect is declared in HttpServletResponse object.

16. What is the use of config implicit object in JSP?
In JSP, config object is of type ServletConfig. This object is created by Servlet Container for each JSP page. It is used for setting initialization parameters for a specific JSP page.

17. What is the difference between init-param and context-param?
We can specify both init-param and context-param in web.xml file. We use init-param to specify the parameters that are specific to a servlet or jsp. This information is confined to the scope of that JSP. We use context-param to specify the parameters for overall application scope. This information does not change easily. It can be used by all the JSP/Servlet in that Container.

18. What is the purpose of RequestDispatcher?
We use RequestDispatcher interface to forward requests to other resources like HTML, JSP etc. It can also be used to include the content of another page in a JSP. It has two methods: forward and include. We have to first get the RequestDispatcher object from the container and then we can call include or forward method on this object.

19. How can be read data from a Form in a JSP?
There is a built-in request object in a JSP that provides methods to read Form data. Some of the methods are as follows::
getParameterNames(): This method returns the list of all the parameters in the Form.
getParameter(): We call this method to get the value of  parameter set in the Form. It returns null if the parameter is not found.
getParameterValues(): If a Parameter is mentioned multiple times in a Form, we use request.getParameterValues() method to get all the values. This method returns an array of String values.
getParameterMap(): This method returns the map of all the Parameters in Form.

20. What is a filter in JSP?
We can define filters in JSP to intercept requests from a client or to change response from a server. Filter is a Java class that is defined in the deployment descriptor of  web.xml of an application. The JSP container reads filter from web.xml and applies a filter as per the URL pattern associated with the filter. JSP Engine loads all the filters in when we start the server.

21. How can you upload a large file in JSP?
To upload a file by JSP we can use <input type=”file”> in the Form data being passed from HTML. If the file is very large in size, we can set  enctype=multipart/formdata. We have to use POST method in the Form to send a file. Once the request is received, we can implement the logic to read mulitpart data in doPost() method of JSP. There are methods in JSP framework to read large files via this method.

22. In which scenario, Container initializes multiple JSP/Servlet objects?

To initialize multiple JSP objects, we have to specify same Servlet object multiple times in web.xml. This indicates to JSP container to initialize separate JSP/Servlet object for each element. Each of the Servlet instance will have its own ServletConfig object and parameters.





Basic Java       OOPs        Static       Inheritance  







Comments

Popular posts from this blog

Microservices Interview Questions

Microservices Interview Questions 1. What is a Microservice in Java? A Microservice is a small and autonomous piece of code that does one thing very well. It is focused on doing well one specific task in a big system. It is also an autonomous entity that can be designed, developed and deployed independently. Generally, it is implemented as a REST service on HTTP protocol, with technology-agnostic APIs. Ideally, it does not share database with any other service. 2. What are the benefits of Microservices architecture? Microservices provide many benefits. Some of the key benefits are: 1.      Scaling : Since there are multiple Microservices instead of one monolith, it is easier to scale up the service that is being used more. Eg. Let say, you have a Product Lookup service and Product Buy service. The frequency of Product Lookup is much higher than Product Buy service. In this case, you can just scale up the Product Lookup service to run on powerful hardware with multipl

DOCKER Interview questions

DOCKER 1. What is Docker? Docker is Open Source software. It provides the automation of Linux application deployment in a software container. We can do operating system level virtualization on Linux with Docker. Docker can package software in a complete file system that contains software code, runtime environment, system tools, & libraries that are required to install and run the software on a server. 2. What is the difference between Docker image and Docker container? Docker container is simply an instance of Docker image. A Docker image is an immutable file, which is a snapshot of container. We create an image with build command. When we use run command, an Image will produce a container. In programming language, an Image is a Class and a Container is an instance of the class. 3. How will you remove an image from Docker? We can use docker rmi command to delete an image from our local system. Exact command is: % docker rmi <Image Id> If we want to fin

Cloud Computing Interview Questions

Cloud Computing 1. What are the benefits of Cloud Computing? There are ten main benefits of Cloud Computing: Flexibility : The businesses that have fluctuating bandwidth demands need the flexibility of Cloud Computing. If you need high bandwidth, you can scale up your cloud capacity. When you do not need high bandwidth, you can just scale down. There is no need to be tied into an inflexible fixed capacity infrastructure. Disaster Recovery : Cloud Computing provides robust backup and recovery solutions that are hosted in cloud. Due to this there is no need to spend extra resources on homegrown disaster recovery. It also saves time in setting up disaster recovery. Automatic Software Updates : Most of the Cloud providers give automatic software updates. This reduces the extra task of installing new software version and always catching up with the latest software installs. Low Capital Expenditure : In Cloud computing the model is Pay as you Go. This means there is very