Web Development with JavaServer Pages

Скачать в pdf «Web Development with JavaServer Pages»

4.3.4 Exception handling


JSP uses Java’s exception-handling mechanism, which helps keep code readable by letting developers focus on the tasks they’re trying to solve, instead of on handling errors manually. A key principle of Java’s exception support is that it lets errors propagate up to the code that’s appropriate to handle them. For instance, a library function might be able to deal with some errors, but it should pass any errors it can’t handle up to its caller.


A JSP page works similarly: if a JSP page wants to handle certain types of unexpected events, it certainly can do so. But a JSP page can also ignore certain errors and let them be handled by code that gets built into the servlets that the JSP container generates. Specifically, JSP allows you to specify an error page that handles unexpected errors that occur during its processing. Suppose you write a JSP page that connects to a database, but the database is down. You don’t need to catch this unexpected event by writing code in your JSP page. Instead, you can use the errorPage mechanism to let the environment catch the error for you, thus giving you less to worry about for each new JSP page you write.


How JSP works

4.3.5 Implicit objects


The Servlet API specifies a Java mapping to functionality that is useful for web applications. For example, through the HttpServletRequest and HttpServletRe-sponse interfaces introduced in chapter
2, Java web applications can easily access requests and configure responses.


Since JSP inherits functionality from the servlet API, JSP applications can take advantage of the convenient Java mappings provided by the servlet environment. JSP takes things a step further, too, by giving simple names to commonly used objects. These names can be accessed from within scripting elements to give Java code within JSP pages easy access to its environment. For instance, in a JSP page operating over HTTP, the name request is given to the HttpServletRequest object, and request parameters can be accessed simply through calls to request.getParameter(). This explains the convenient syntax we demonstrated in chapter 2.

Скачать в pdf «Web Development with JavaServer Pages»