Can I Invoke A Jsp Error Page From A Servlet
Contents |
Mobile Media API Other Certification Server-Side Java Web Services & Cloud Downloads Learn Forums Newsletter Close Home / FAQ how to call servlet from jsp page / Server-Side Development / Servlets Can I invoke a JSP error page
How To Call Servlet From Jsp Page Example
from a servlet? Created May 3, 2012 Tweet Govind Seshadri Yes, you can invoke the JSP error
How To Call Servlet From Jsp Page Without Using Url Mapping
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
How To Call A Servlet From Jsp On Page Load
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 how to call servlet from jsp without form 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(); } } } Post a comment Email Article Print Article Share Articles Digg del.icio.us Newsvine Facebook Google LinkedIn MySpace Reddit Slashdot StumbleUpon Technorati Twitter Windows Live YahooBuzz FriendFeed Please enable Javascript in your browser, before you post the comment! Now Javascript is disabled. 0 Comments (click to add your comment) Comment and Contribute Your name/nickname Your email WebSite Subject (Maximum characters: 1200). You have 1200 characters left. Most Popular jGuru Stories Editor's Picks Most Popular The Java Game Development Tutorial Files and Directories in Java Load Testing your Applications with Apache JMeter Unit Testing Java Programs Using SOAP with Java Repeating Annotations in Java 8 About | Sitemap | Contact
Management Accounting Operating System Operating System Android iOS Microsoft Coding how to call servlet from jsp using javascript Programming J2EE Languages Database Database Data Warehousing Oracle how to call servlet from jsp form action Applications Applications Clarify CRM Oracle Apps Peoplesoft Siebel Software Testing Testing Testing Tools how to call servlet from jsp onload Miscelleanous Helpdesk Mainframe Networking SAP R/3 Web Interview Coaching Career Counseling Job Interview Interview eBooks Accounting eBook C eBook Citrix eBook Job http://www.jguru.com/faq/view.jsp?EID=1347 Interview eBook Career Advice Career Change Jobs Help Projects Requests Resume Help Training Advice Engineering Chemical Engineering Civil Engineering Electrical Engineering Electronics Engineering Micro Processor Mechanical Engineering By Company Ask Question Home Interview QuestionsJ2EEServlets Can I invoke a JSP error page from a servlet? http://www.geekinterview.com/question_details/2410 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 obtainedThe 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) throwsServletException, 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();}}} Interview Candidate
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of http://stackoverflow.com/questions/26838036/how-to-redirect-to-error-page-in-servlet this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask https://docs.oracle.com/cd/B10501_01/java.920/a96657/basics.htm Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up how to How to redirect to error page in servlet? up vote 2 down vote favorite I am writing the servlet , in case of exception I am redirecting to my customized error page for that i have done like this. In web.xml
JSP "starter sample" for data access. The following topics are included: Application Root and Doc Root Functionality Overview of JSP Applications and Sessions JSP-Servlet Interaction JSP Resource Management JSP Runtime Error Processing JSP Starter Sample for Data Access Notes: JSP configuration, including specifics for a JServ environment, are covered in "Getting Started in a JServ Environment". JSP pages will run with any standard browser supporting HTTP 1.0 or higher. The JDK or other Java environment in the end-user's Web browser is irrelevant, because all the Java code in a JSP page is executed in the Web server or data server. Application Root and Doc Root Functionality This section provides an overview of application roots and doc roots, distinguishing between servlet 2.2 functionality and servlet 2.0 functionality. Application Roots in Servlet 2.2 Environments As mentioned earlier, the servlet 2.2 specification provides for each application to have its own servlet context. Each servlet context is associated with a directory path in the server file system, which is the base path for modules of the application. This is the application root. Each application has its own application root. This is similar to how a Web server uses a doc root as the root location for HTML pages and other files belonging to a Web application. For an application in a servlet 2.2 environment, there is a one-to-one mapping between the application root (for servlets and JSP pages) and the doc root (for static files, such as HTML files)--they are essentially the same thing. Note that a servlet URL has the following general form: http://host[:port]/contextpath/servletpath When a servlet context is created, a mapping is specified between the application root and the context path portion of a URL. For example, consider an application with the application root /home/dir/mybankappdir, which is mapped to the context path mybank. Further assume the application includes a servlet whose servlet path is loginservlet. This servlet can be invoked as follows: http://host[:port]/mybank/loginservlet (The application root directory name itself is not visible to the end-user.) To continue this example for an HTML page in this application, the following URL points to the file /home/dir/mybankappdir/dir1/abc.html: http://host[:port]/mybank/dir1/abc.html F