JSP and jsp tutorials
JSP
I. What is jsp?
JSP, short for Java Server Pages, is a dynamic web page development technology. It uses JSP labels to insert Java code into HTML webpages. The tag is usually ended with "%" starting with "%.
JSP is a Java servlet used to implement the user interface of Java web applications. Web developers compile JSP by combining HTML code, XHTML code, XML elements, and embedded JSP operations and commands. Ii. operating principles of jsp:
When each JSP page is accessed for the first time, the JSP Engine translates it into a Servlet Source program, and then compiles the Servlet Source program into a Servlet class file. then, the WEB Container (Servlet Engine) loads and interprets and executes the Servlet program translated from the JSP page in the same way as calling a common Servlet program.
Iii. jsp implicit Variables
Objects that can be used without declaration. a JSP page contains nine hidden objects. the code written with <%> is located here. you can use the request, response, pageContext, session application, config, out, and page hidden objects. (an implicit object called exception can also be used)
①. Request: an object of HttpServletRequest .*
②. Response: an object of HttpServletResponse (almost no method of response is called on the JSP page .)
③. PageContext: The page context, which is an object of PageContext. you can obtain the other 8 hidden objects from this object. you can also obtain other information on the current page. (use it when learning custom tags) ④. session: indicates a session between the browser and the server. It is an object of HttpSession.
⑤. Application: indicates the current WEB application. It is a ServletContext object .*
⑥. Config: The ServletConfig object of the Servlet corresponding to the current JSP (almost not used). To access the initialization parameters of the current JSP configuration,
7. out: JspWriter object. Calling out. println () can print the string directly to the browser.
References. page: point to the reference of the Servlet Object corresponding to the current JSP, but it is of the Object type and can only call methods of the Object class (almost not used)
Forbidden. exception: this parameter can be used only when isErrorPage = "true" of the page command is declared.
Iv. jsp commands
JSP commands (directive) are designed for the JSP Engine. They do not directly generate any visible output, but only tell the engine how to process the rest of the JSP page.
In JSP 2.0, three commands, page, include, and taglib, are defined to define the attributes of JSP pages, no matter where the page command appears on the JSP page, it serves the entire JSP page. To keep the program readable and follow good programming habits, it is best to place the page instruction at the beginning of the entire JSP page.
①. Import attribute: Specify the class to be imported by the Servlet corresponding to the current JSP page. <% @ page import = "java. lang. *" %>
②. Session attribute: The value is true or false, which specifies whether the session hidden variables on the current page are available. It can also be said that the HttpSession object must be generated when accessing the current page. creating a session on the server consumes a lot of resources, so it is also an optimization to not generate a session in a proper place. <% @ Page session = "false" %>
③. ErrorPage and isErrorPage:
> ErrorPage specifies the actual response page when an error occurs on the current page.
Where/indicates the root directory of the current WEB application. <% @ page errorPage = "/error. jsp" %>
> In response to error. jsp, the JSP engine uses the request forwarding method.
> IsErrorPage specifies whether the current page is an error handling page. It indicates whether the current page can use exception to hide the variable. note that if you specify isErrorPage = "true" and use the exception method, you are generally not advised to directly access this page.> how can customers not directly access a page? For the Tomcat server, files in the WEB-INF cannot be accessed by directly entering the address in the browser. But it is okay to forward requests!
> You can also configure the error page in the web. xml file:
<Error-page> <! -- Error code specified: 404 no resource specified, 500 internal error. --> <error-code> 404 </error-code> <! -- Specify the location of the response page --> <location>/WEB-INF/error. jsp </location> </error-page> <! -- Specify the exception type --> <exception-type> java. lang. arithmeticException </exception-type> <location>/WEB-INF/error. jsp </location> </error-page>
④. ContentType: Specify the response type of the current JSP page. the actual call is response. setContentType ("text/html; charset = UTF-8"); generally, for JSP pages, the value is text/html; charset = UTF-8. charset specifies the character encoding of the returned page. usually the value is UTF-8 <% @ page contentType = "application/vnd. ms-excel "%> data generated on the current page excel
⑤. PageEncoding: Specifies the character encoding of the current JSP page. Normally, this value is consistent with the charset in contentType.
6. isELIgnored: Specifies whether the current JSP page can use the EL expression. The value is usually false.