JSP script Element
Jsp declaration Syntax: <%! %>
The jsp declaration is only valid on the current page, and the jsp built-in object cannot be used in the jsp declaration. The variables or methods declared in it are instance methods or instance variables.
Java code block syntax in Jsp: <%>
The java code block embedded in jsp is called Scriptlet. By default, it is the code block in the service () method of the Servlet class corresponding to jsp. The Scriptlet position is determined by the method in the page command. The method value can be service (), doGet (), doPost (), and so on. The variables declared in it are local variables.
Expression syntax in Jsp: <% = %> is used to output the expression or variable value on the page.
JSP built-in object
Object Type
Request javax. servlet. HttpServletRequest
Response javax. servlet. HttpServletResponse
PageContext javax. servlet. jsp. PageContext
Application javax. servlet. ServletContext
Out javax. servlet. jsp. JspWriter
Config javax. servlet. ServletConfig
Page java. lang. Object (similar to the this keyword in java)
Session javax. servlet. HttpSession
Exception java. lang. Exception
JSP exception handling:
Jsp and Java may also throw an exception at runtime. You can add the following code on the exception page:
<% @ Page errorPage = "errorpage. jsp" %>
The current page jumps to errorpage. jsp for exception handling.
On the errorpage. jsp page, add the following statement to declare that this page is an exception handling page.
<% @ Page isErrorPage = "true" %>
On the exception handling page, you can directly access the built-in exception object to obtain the exception information.
<% Exception. printStackTrace (new PrintWriter (out); %>
JSP request forwarding:
Request. getRequestDispatcher ("success. jsp"). forward (request, response );
Features: the address bar does not change. We call it request forwarding. The same request is initiated and data is not lost.
Response. sendRedirect ("success. jsp ");
Feature: the address bar is changed, which is called redirection. Two requests are initiated, causing data loss.
Chinese garbled code solution:
Post submission method: request. setCharacterEncoding ("UTF-8 ");
Get submit (url passing parameters) method: String str = new String (request. getParameter ("uName"). getBytes ("iso-8859-1"), "UTF-8 ");
JSP pre-Compilation: The jsp_precomplie parameter can be set to true or false. If the request is true, the Servlet container only needs to pre-compile the JSP file requested by the client, converts a JSP file to a Servlet class, but does not run the Servlet;
1. http: // localhost: 8080/helloapp/hello. jsp? Jsp_precomplie = true
2. Find the hello_jsp.class file under the helloapp of the work directory under the Tomcat directory.
Copy it to the subdirectory corresponding to the WEB-INF/classes
3. Configure the hello_jsp class in the web. xml file.
<Servlet>
<Servlet-name> hi </servlet-name>
<Servlet-class> org. apache. jsp. hello_jsp </servlet-name>
</Servlet>
<Servlet-mapping>
<Servlet-name> hi </servlet-name>
<Url-pattern>/hello </url-pattern>
</Servlet-mapping>
From the column gdn_wolf