|
INTERVIEW QUESTIONS
J2EE
SERVLET
DETAILS
Question: Can I invoke a JSP error page from a servlet?
Answer: Yes, you can invoke the JSP error page and pass the exception object to it from within a servlet. The trick is to create a request dispatcher for the JSP error page, and pass the exception object as a javax.servlet.jsp.jspException request attribute. However, note that you can do this from only within controller servlets. If your servlet opens an OutputStream or PrintWriter, the JSP engine will throw the following translation error: java.lang.IllegalStateException: Cannot forward as OutputStream or Writer has already been obtained The following code snippet demonstrates the invocation of a JSP error page from within a controller servlet: protected void sendErrorRedirect(HttpServletRequest request, HttpServletResponse response, String errorPageURL, Throwable e) throws ServletException, IOException { request.setAttribute ("javax.servlet.jsp.jspException", e); getServletConfig().getServletContext(). getRequestDispatcher(errorPageURL).forward(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) { try { // do something } catch (Exception ex) { try { sendErrorRedirect(request,response,"/jsp/MyErrorPage.jsp",ex); } catch (Exception e) { e.printStackTrace(); } } }
|
|
|
Category |
Servlet Interview Questions & Answers -
Exam Mode /
Learning Mode
|
Rating |
(0.3) By 7742 users |
Added on |
7/17/2011 |
Views |
71423 |
Rate it! |
|
|
Question:
Can I invoke a JSP error page from a servlet?
Answer:
Yes, you can invoke the JSP error page and pass the exception object to it from within a servlet. The trick is to create a request dispatcher for the JSP error page, and pass the exception object as a javax.servlet.jsp.jspException request attribute. However, note that you can do this from only within controller servlets. If your servlet opens an OutputStream or PrintWriter, the JSP engine will throw the following translation error: java.lang.IllegalStateException: Cannot forward as OutputStream or Writer has already been obtained The following code snippet demonstrates the invocation of a JSP error page from within a controller servlet: protected void sendErrorRedirect(HttpServletRequest request, HttpServletResponse response, String errorPageURL, Throwable e) throws ServletException, IOException { request.setAttribute ("javax.servlet.jsp.jspException", e); getServletConfig().getServletContext(). getRequestDispatcher(errorPageURL).forward(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) { try { // do something } catch (Exception ex) { try { sendErrorRedirect(request,response,"/jsp/MyErrorPage.jsp",ex); } catch (Exception e) { e.printStackTrace(); } } } Source: CoolInterview.com
If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-
- There should not be any Spelling Mistakes.
- There should not be any Gramatical Errors.
- Answers must not contain any bad words.
- Answers should not be the repeat of same answer, already approved.
- Answer should be complete in itself.
|
|
Related Questions |
View Answer |
|
Difference between single thread and multi thread model servlet
|
View Answer
|
|
Can we call destroy() method on servlets from service method?
|
View Answer
|
|
What is the use of ServletConfig and ServletContext..?
|
View Answer
|
|
What is the difference between servlets and applets?
|
View Answer
|
|
What is the difference between ServetConfig and ServletContext..?
|
View Answer
|
|
What are the uses of Servlets?
|
View Answer
|
|
How Servlet Applet communication achieved?
|
View Answer
|
|
What is the difference between JSP and SERVLETS
|
View Answer
|
|
What is servlet context and what it takes actually as parameters?
|
View Answer
|
|
Do we have a constructor in servlet ?Can we explicitly provide a constructor in servlet program?
Can we call "destroy" method from "init" method()? If so what will happen?
|
View Answer
|
|
How to communicate between two servlets?
|
View Answer
|
|
What is difference between sendRedirect() and forward()..? Which one is faster then other and which works on server?
|
View Answer
|
|
When we increase the buffer size in our project using page directive attribute 'buffer' what changes
|
View Answer
|
|
operations performed on vector are either posing a value into the vector (or) retriving a value from
|
View Answer
|
|
What are the differences between GET and POST service methods?
|
View Answer
|
|
How to get one Servlet's Context Information in another Servlet?
|
View Answer
|
|
Why is that we deploy servlets in a webserver.What exactly is a webserver?
|
View Answer
|
|
How to know whether we have to use jsp or servlet in our project?
|
View Answer
|
|
How to integare struts and hibernate
|
View Answer
|
|
After converting jsp into servlet which methods are present i.e all 6 methods(jspinit(),_jspService(),jspDestroy(),init(),service(),destroy()) or only 3 methods i.e either jsp life cycle methods or servlet lifecycle methods
|
View Answer
|
Please Note: We keep on updating better answers to this site. In case you are looking for Jobs, Pls Click Here Vyoms.com - Best Freshers & Experienced Jobs Website.
View All Servlet Interview Questions & Answers - Exam Mode /
Learning Mode
|