|
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 9132 users |
Added on |
10/25/2009 |
Views |
74750 |
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 |
|
Can I just abort processing a JSP?
|
View Answer
|
|
What is a better approach for enabling thread-safe servlets and JSPs? SingleThreadModel Interface or Synchronization?
|
View Answer
|
|
If you want a servlet to take the same action for both GET and POST request, what should you do?
|
View Answer
|
|
What is the servlet life cycle?
|
View Answer
|
|
Which code line must be set before any of the lines that use the PrintWriter?
|
View Answer
|
|
How HTTP Servlet handles client requests?
|
View Answer
|
|
What is the Servlet Interface?
|
View Answer
|
|
When a servlet accepts a call from a client, it receives two objects. What are they?
|
View Answer
|
|
What information that the ServletRequest interface allows the servlet access to?
|
View Answer
|
|
What information that the ServletResponse interface gives the servlet methods for replying to the client?
|
View Answer
|
|
What is the servlet?
|
View Answer
|
|
What are the uses of Servlets?
|
View Answer
|
|
What are the advantages using servlets than using CGI?
|
View Answer
|
|
What is the difference between servlets and applets?
|
View Answer
|
|
Difference between single thread and multi thread model serv
|
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
|