• +91 9971497814
  • info@interviewmaterial.com

JSP Interview Questions Answers

Question 1 : Can we use the constructor,  instead of init(), to initialize servlet?

Answer 1 : Yes , of course you can use the constructor instead of init(). There’s nothing to stop you. But you shouldn’t. The original reason for init() was that ancient versions of Java couldn’t dynamically invoke constructors with arguments, so there was no way to give the constructur a ServletConfig. That no longer applies, but servlet containers still will only call your no-arg constructor. So you won’t have access to a ServletConfig or ServletContext.

Question 2 : How does JSP handle runtime exceptions?

Answer 2 : 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.

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

Answer 3 : 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 4 : How can a servlet refresh automatically if  some new data has entered the database?

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

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

Answer 5 : 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 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 : How do you connect to the database from JSP?

Answer 7 : 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 8 : Is there a way to reference the "this" variable within a JSP page?

Answer 8 : 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 9 : How can I declare methods within my JSP page?

Answer 9 : 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 10 : How can I implement a thread-safe JSP page?

Answer 10 : 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.

JSP Contributors

krishan

Share your email for latest updates

Name:
Email:

Our partners