• +91 9971497814
  • info@interviewmaterial.com

JSP Interview Questions Answers

Question 1 : How do I use comments within a JSP page?

Answer 1 : You can use "JSP-style" comments to selectively block out code while debugging or simply to comment your scriptlets. JSP comments are not visible at the client. For example: --%> You can also use HTML-style comments anywhere within your JSP page. These comments are visible at the client. For example: Of course, you can also use comments supported by your JSP scripting language within your scriptlets.

Question 2 : How can I implement a thread-safe JSP page?

Answer 2 : You can make your JSPs thread-safe by having them implement the SingleThreadModel interface. This is done by adding the directive <%@ page isThreadSafe="false" % > within your JSP page.

Question 3 : How can I declare methods within my JSP page?

Answer 3 : You can declare methods for use within your JSP page as declarations. The methods can then be invoked within any other methods you declare, or within JSP scriptlets and expressions. Do note that you do not have direct access to any of the JSP implicit objects like request, response, session and so forth from within JSP methods. However, you should be able to pass any of the implicit JSP variables as parameters to the methods you declare. For example: Another Example: file1.jsp: file2.jsp <%test(out);% >

Question 4 : Is there a way to reference the "this" variable within a JSP page?

Answer 4 : Yes, there is. Under JSP 1.0, the page implicit object is equivalent to "this", and returns a reference to the Servlet generated by the JSP page.

Question 5 : How do you connect to the database from JSP?

Answer 5 : A Connection to a database can be established from a jsp page by writing the code to establish a connection using a jsp scriptlets. Further then you can use the resultset object "res" to read data in the following way.

Question 6 : How do I perform browser redirection from a JSP page?

Answer 6 : You can use the response implicit object to redirect the browser to a different resource, as: response.sendRedirect("http://exammaterial.com"); You can also physically alter the Location HTTP header attribute, as shown below: You can also use the: Also note that you can only use this before any output has been sent to the client. I beleve this is the case with the response.sendRedirect() method as well. If you want to pass any paramateres then you can pass using >

Question 7 : Can a JSP page process HTML FORM data?

Answer 7 : Yes. However, unlike Servlet, you are not required to implement HTTP-protocol specific methods like doGet() or doPost() within your JSP page. You can obtain the data for the FORM input elements via the request implicit object within a scriptlet or expression as.

Question 8 : How can a servlet refresh automatically if  some new data has entered the database?

Answer 8 : You can use a client-side Refresh or Server Push.

Question 9 : How can my application get to know when a HttpSession is removed?

Answer 9 : Define a Class HttpSessionNotifier which implements HttpSessionBindingListener and implement the functionality what you need in valueUnbound() method. Create an instance of that class and put that instance in HttpSession.

Question 10 : How does JSP handle runtime exceptions?

Answer 10 : Using errorPage attribute of page directive and also we need to specify isErrorPage=true if the current page is intended to URL redirecting of a JSP.

JSP Contributors


Share your email for latest updates


Our partners