Re: Conditional Branch from Servlet to URL - Continued
On 1:59 PM, Donald Jolley wrote: JSPs should be used for output with all processing (and, hopefully, anything that can fail) having already been completed by the time the JSP runs. That's a REALLY good point. The JSP can be thought of as simply bieing a view with all (or most) of the heavy lifting done elsewhere. Within the limited context of handling errors, I think that your point would be compelling. However, I can think of many other uses one could make of a capability to forward from a JSP. I'm sure that there are even more uses that haven't occurred to me. It would just be a real nice capability to have available. Accordingly, I would like to play around with it a bit. I've learned a lot from what I have done so far; and, if nothing else I'm sure the exercise wiill provide a great learning experience. I'm sure that I'll be running into difficulty and posting. However, I think that the issue that I have raised in this thread has been solved. If (more like when) I have other problems I will post in a new thread. Thanks to all for all of the great help that I received in this thread. ... doug Hi, Donald- You might also want to consider using the isErrorPage=”true|false” and possibly the errorPage=”error_url” attributes of the JSP page directive for error handling. However, I would still agree with Chris's recommendation that all processing that might generate an error be completed before any output is generated. -Terence Bandoian - To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org
Re: Conditional Branch from Servlet to URL - Continued
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 Donald, Glad you got things working. On 8/25/2011 8:57 PM, Donald Jolley wrote: Having gotten this far, my next question is this: Is there any way that I can get this to work within the context of a jsp? Yes, but I would recommend against it. JSPs should be used for output with all processing (and, hopefully, anything that can fail) having already been completed by the time the JSP runs. Sure, JSP has tag libraries for accessing databases and all kinds of crazy stuff, but that's not good architecture IMHO. I have a catch block to handle the exception. What I'd like to do is to jump (on the occurrence of an exception) from the jsp that has the method-throwing exception to a different error-handling jsp. Is there a way that I can do that? There are try/catch taglibs available, but again I'd recommend against using them. public class Test2 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType(text/html); PrintWriter out = response.getWriter(); out.println(html); out.println(headtitleTest 2/title/head); out.println(body); out.println(pHello, world!/p); if (request.getParameter(jump) != null ) { request.getRequestDispatcher(request.getParameter(jump)).forward(request, response); } out.println(/body); out.println(/html); } } This looks like using RequestDispatcher.include() might be more appropriate. When you use forward(), you should let the other servlet generate the /entire/ response. - -chris -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.10 (MingW32) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iEYEARECAAYFAk5XvEcACgkQ9CaO5/Lv0PC7XwCgtddXawbUy3Jeb4gsoOykSsgi 0wsAoLoQz02NaV3Br75UGo2eXNrkTZZH =XN0a -END PGP SIGNATURE- - To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org
Re: Conditional Branch from Servlet to URL - Continued
JSPs should be used for output with all processing (and, hopefully, anything that can fail) having already been completed by the time the JSP runs. That's a REALLY good point. The JSP can be thought of as simply bieing a view with all (or most) of the heavy lifting done elsewhere. Within the limited context of handling errors, I think that your point would be compelling. However, I can think of many other uses one could make of a capability to forward from a JSP. I'm sure that there are even more uses that haven't occurred to me. It would just be a real nice capability to have available. Accordingly, I would like to play around with it a bit. I've learned a lot from what I have done so far; and, if nothing else I'm sure the exercise wiill provide a great learning experience. I'm sure that I'll be running into difficulty and posting. However, I think that the issue that I have raised in this thread has been solved. If (more like when) I have other problems I will post in a new thread. Thanks to all for all of the great help that I received in this thread. ... doug
Re: Conditional Branch from Servlet to URL - Continued
Chris et al, thanks ever so much for all the great help. I finally got things working. This has been quite an educational experience for me. I am appending to this message my complete *WORKING* servlet. Having gotten this far, my next question is this: Is there any way that I can get this to work within the context of a jsp? Say for example that I call a method from within a JSP and that method could through an exception. I have a catch block to handle the exception. What I'd like to do is to jump (on the occurrence of an exception) from the jsp that has the method-throwing exception to a different error-handling jsp. Is there a way that I can do that? Again, thanks to all. ... doug /* WORKING SERVLET */ import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class Test2 extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType(text/html); PrintWriter out = response.getWriter(); out.println(html); out.println(headtitleTest 2/title/head); out.println(body); out.println(pHello, world!/p); if (request.getParameter(jump) != null ) { request.getRequestDispatcher(request.getParameter(jump)).forward(request, response); } out.println(/body); out.println(/html); } }
Re: Conditional Branch from Servlet to URL
On 24/08/2011 03:46, Leo Donahue - PLANDEVX wrote: From: Donald Jolley [jolleyt...@gmail.com] Subject: Conditional Branch from Servlet to URL I'm not at all surprised about the request and response symbols as they appear to be undefined. I really expected that getRequestDispatcher would have been found in javax.servlet.* which is imported. ... doug * Shouldn't you import javax.servlet.http ? If your request and response objects are undefined - wish we could see how you declared them, how can RequestDispatcher perform the forward? @Donald Usually in a Servlet (your code *is* inside a Servlet isn't it?) you'd see those two variables defined in the method parameters: protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ... { p signature.asc Description: OpenPGP digital signature
Re: Conditional Branch from Servlet to URL - Continued
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 Donald, On 8/24/2011 2:23 PM, Donald Jolley wrote: how can RequestDispatcher perform the forward I'm not sure. My objective is to institute a way of conditionally jumping from a servlet to a specified page. This does work. You have to do something like the following: public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { // ... do something interesting if(/* whatever decision you have to make */) { request.getRequestDispatcher(/target/url) .forward(request, response); return; } // do whatever the default operation of this servlet is } That last return may or may not be required, but if you expected that the forwarded request would produce a complete response, then it's the proper thing to do. Note that the forward() call is synchronous and /actually invokes the other servlet/ and /then returns to your code/. This isn't like telling the container that, after your method ends, you want the other servlet to take over. Your servlet is calling the other one, indirectly through the RequestDispatcher. This surprises some people sometimes :) If your servlet won't compile, then you need to figure that out. Look at the entire example -- it should probably include some import statements at the top of the file. There's no requirement that the method arguments be named request and response though they often are, by convention (I've also seen req/rsp, rq/rp, hsrRequest/hsrResponse and some others, but most people just stick with request/response). If things aren't actually working once you get your code compiled and running, let us know and maybe we can help. But the request dispatcher definitely works. - -chris -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.10 (MingW32) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iEYEARECAAYFAk5VSJsACgkQ9CaO5/Lv0PAPDACfbsLy6wgXO5AuCwroa+srG3cp 1u4AoLx+9kPekqFBQRfSLdPIF6Bivj7m =yQLH -END PGP SIGNATURE- - To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org
RE: Conditional Branch from Servlet to URL
From: Donald Jolley [jolleyt...@gmail.com] Subject: Conditional Branch from Servlet to URL I'm not at all surprised about the request and response symbols as they appear to be undefined. I really expected that getRequestDispatcher would have been found in javax.servlet.* which is imported. ... doug * Shouldn't you import javax.servlet.http ? If your request and response objects are undefined - wish we could see how you declared them, how can RequestDispatcher perform the forward? - To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org