I. Declare the JSP error page
(1) The errorpage attribute of page ctictive is used to specify a URL. This URL can handle exceptions thrown by JSP.
(2) The URL is relative to the path of the Web application.
(3) The declared error page can be another JSP, Servlet, or HTML file.
Let's take a look at the error indicator declaration error page.
<%@page errorPage="badLogin.jsp"%>
Ii. built-in exception object
(1) The JSP using the iserrorpage attribute has an additional built-in exception.
Exception object example badlogin. jsp
<%@ page isErrorPage="true"%>Error message=<%=exception.getMessage()%><%PrintWriter writer=new PrintWriter(out);%>Stack=<% exception.printStackTrace(writer);%>
(2) error pages can be shared between JSP
3. Define the web application error page
(1) You can use the <error-page> element to specify a specific page to handle HTTP errors.
(2) if these errors are not internally processed by servlets or JSPs, these pages will be called.
Example of error page declaration web. xml file:
<error-page> <error-code>404</error-code> <location>/handle404.jsp</location></error-page><error-page> <exception-tpye>java.io.IOException </exception-tpye> <location>/handleIOException.jsp</locatio></error-page>
4. Forward and include
(1) servlets and JSPs can work collaboratively through forward and include
(2) servlet fowarding
Servlets can use only the forward method of the requestdispatcher object to permanently pass control to other servlets
Example of forwarding between servlets:
Public class servlet1 extends httpservlet {public void Service (httpservletrequest request, httpservletresponse response ){... servletcontext SC = getservletcontext (); requestdispatcher RD = SC. getrequestdispatcher ("servlet2"); Rd. forword (request, response); return; // end this servlet }...}
(3) servlet including
Servlets can use the include method of the requestdispatcher object to temporarily pass control to other servlets
Example of including between servlets:
Public class servlet1 extends httpservlet {public void Service (httpservletrequest request, httpservletresponse response ){... servletcontext SC = getservletcontext (); requestdispatcher RD = SC. getrequestdispatcher ("servlet2"); Rd. include (request, response); // continue to process this servlet }...}
(4) JSP forwarding
When JSP forward action is used, control the transfer to the target page
Example of using the forward action:
<% if(shippingAddressValid()){%><jsp:forward page="billing.jsp"/><% else %><jsp:forward page="address.jsp"/><%}%>
(5) JSP including
When the JSP include action is used, the JSP is executed and the JSP output is inserted into the jsp of the called JSP.
JSP contains another JSP based on user registration:
<% String registered=request.getParameter("regUser");if(registered.equals("true")){%><jsp:include page="welcome.jsp"/><%}else{%><jsp:include page="sorry.jsp"/><%}%>...
5. Use the tag Library
(1) custom tags:
- Expand JSP Functions
- Division of labor based on generated content and code writing
- Tag Handler
- Use the tag library descriptor that defines the tag library description file for declaration and Configuration
- Use the taglib command to load
<%@ taglib uri="counter" prefix="countLib"%>
(2) create tags
Obtain some JSP tags of the number of clicks from the file:
package staplerz.tagext.counter;import java.io.*;import javax.servlet.jsp.*;import javax.servlet.jsp.tagext.*;public class Display extends TagSupport{public int doStartTag()throws Jspexception{File countFile=new File("count.tmp");int count=Count.getCount(countFile);JspWriter out=pageContext.getout();try{out.print(count);}catch(IOException ioe){//Error handing}return(SKIP_BODY);}}
This section is a supplement to JSP content.