JSP Summary (i)
Preface: Originally, is intended to write only a JSP built-in object summary, but did not expect this guy's JSP summary is very good, I took it.
Reference Address: http://www.cnblogs.com/cyjch/archive/2012/03/28/2420798.html
Miscellaneous words (see below): (a) JSP definition: 1) java Server Page, Java EE component, essentially Servlet. 2) run on Web Container. Receive HTTP request, generate HTTP Response (default protocol is HTTP request and Response) 3) JSP allows us to detach the static HTML of the page and Dynamic part--the technology we need. 4) enables pages to be mixed with HTML code, Java code, and JSP tags, allowing access to components. (b) servlet defects (JSP appearance reasons): 1) write static pages must be deployed to see the effect, it is difficult to control the appearance of the page. 2) servlet is a mixed code of Java code and HTML static code from a technical point of view. 3) in terms of market competition, Microsoft has introduced ASP products. (iii) Improvements to JSP: 1) JSP is a tabbed text file (area servlet is a Java file) 2) JSP does not need to compile (in fact, the server monitors the JSP file changes, and then translates it into a Servlet code). The server compiles it and creates a servlet instance on the first request. Therefore, the first time to access the JSP page will be delayed after 3) JSP do not write configuration file 4) jsp to static code-based, Java code supplemented. Servlet instead. 5) is part of the Java EE Blueprint (Servlet, JSP, and EJB are the three main components of the Java EE) 6) JSP is essentially a servlet, But the servlet is not an alternative but a complementary relationship. 7) JSP is suitable for writing the dynamic page of the display layer, while the servlet is suitable for writing the control layer's Business Control (page forwarding). 8) JSP to the pure label direction, servlet to the pure code direction, they take serThe Vlet kernel (request-responsive mode of operation) is developed in two directions.
JSP basic syntax: first, JSP declaration (statement)
Used to define the properties and methods (member variables) of the class in the resulting class file. A class can be declared (that is, an inner class).
Because the servlet is working in a multithreaded environment, try not to declare member variables outside of the service method body.
<%!.....%>//declaration to add "!", which belongs to the class member, is loaded first, can be written in any position, and not the local variable of the script, must be called before writing.
Such as:
<%! String hello= "Hello, world!"; %> // variable declaration <%=hello%> / / variable call private int counter=0; Public int return // Declaration of a function // Call to function
Claim rules:
1) The variables and methods declared in the JSP correspond to the instance methods and instance variables in the servlet. These will be shared by all users who request the page at the same time;
2) before using a variable or method must be defined (not to say that the position of the declared variable in the page to be in the use of the variable in front, but that the variable does not declare can not be used);
3) The scope of the declared variable or method is the current page or the containing page;
4) The statements are separated by semicolons.
Second, JSP code snippet (scriptlet)
<% Java Code%>
is a piece of Java code that can be executed at the time of processing the request. can produce output, or it can be some process control statements.
The variable defined in the code snippet is a local variable in the service method.
1. _jspservice () in the local code:
<% System.out.println ("Hi,i like JSP.")%> // printed on the console, no <% on the page Out.println ("Hi,i like JSP."); %> // Print on the Web page <% Connection conn=drivermanager.getconnection (); Statement St=conn.createstatement ();
String SQL= "SELECT * from users"; ResultSet rs=st.executequery (sql); // ... %>
Q: Can I define a method in a JSP script?
Answer: No! Scripts are equivalent to methods and cannot be defined in methods
<%! Public void HelloWorld () {}%> // can be declared method publicvoid HelloWorld () {}%> // compile error; script cannot define method
2. Comparison:
int i=100;%> // member variable <% int i=101;%> // Local Variables in the _jspservice () method <%=i%> // in the same file, local variables are preferred
3. Script Applet rules:
1) The scripting language you use determines the rules of the script applet;
2) The statements are separated by semicolons;
3) You can use default objects, import-in classes, declaration-declared methods and objects, and objects declared in Usebean tag.
Iii. jsp expressions (expression)
<%=......%> // "=" Number
Calculating his value during the JSP request processing phase, the code generated by the expression is a snippet of code in the service method.
JSP handling of claims : 1, evaluating the value of an expression
2. Convert value to String
3, send the label with OUT.PRINTLN; output data to the current position of the page
<%= "Hello,jsp world!" %> //out.println ("hello,jsp World");
<%=name%> // <%! String name= "GiGi";%> out.println (name); <%=New java.util.Date ()%> //out.println (New Java.util.Date ());
Expression rules:
1) The scripting language you use determines the rules of the script applet;
2) The order of execution is from left to right;
3) A semicolon cannot be used in an expression.
Iv. JSP directive (direction)
Instructions are used to send information from the JSP to the container. Used to set global variables, declare classes, methods to implement and output content, and so on.
Directive is valid within the entire JSP file. It provides global information for the translation phase.
<%@......%> // "@" symbol
Directives include: page, include, taglib
1. Page directive
Import, Session, ErrorPage, IsThreadSafe
Page language, content type, character set, page encoding
<% @page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" Utf-8 "%>
Language:java unique value that represents the programming language used in the script
ContentType: Sets the type of content and the encoding of the static page (tells the browser what encoding to display)
Pageencoding: The encoding format of the page itself (the encoding format used when writing the page)
The above code is equivalent to the servlet: Response.setcontenttype ("text/html; CHARSET=GBK ");
(1)import: Importing other packages and classes where the JSP default import package is java.lang.*
Import // specific packages and classes Import= "java.sql.*"%> // Package All classes import// ligatures, Comma separated
(2) Session: Indicates whether the current JSP participates in the session (default is true; participating sessions)
To make the current page and session non-conversational by instruction: <% @page session= "false"%>
When session= "true", the session can be accessed directly by the built-in objects session, for example:
<%
Session.setattribute ("username", "Maxwell");
%><%=name%>
(3)ErrorPage:
iserrorpage:jsp how exceptions are handled in the page
For pages that have the potential to appear abnormally:
<% @page errorpage= "error.jsp"%>//exception will jump to the handling of the exception of the page;
Print the reason where it is possible to be abnormal: throw new Exception ("Database connection error");
For the page that handles the exception (ERROR.JSP):
<% @page iserrorpage= "true"%>, where the exception information is printed using <%=exception.getmessage ()%>
(4)isthreadsafe--This property is no longer in use (deprecated)
is the current JSP page thread-Safe Default--->true
<% @page isthreadsafe= "true"%>//common servlet that can handle user requests concurrently
<% @page isthreadsafe= "false"%>//equivalent to servlet implementation Singlethreadmodel
2. Include directives
The content of the target page is included in the current page, resulting in the output of the page overlay//equivalent to merging two pages; compile time is included
// can be inserted in any position
3. Taglib Instructions
Stay in the jstl to explain.
V. Comment 1 in the JSP. Java Format comments
The compiler ignores the content in such comments (the client's source code is not visible)
<%--JSP annotations; multiline--%><%// Java single-line comment%><%/**/%><% /** Java-specific annotations */%>
2. HTML style annotations
The compiler executes the code in such comments (the client source is visible)
// equivalent to Out.println ("<!--HTML-style annotations--")
The downside of this notation is that the load on the server increases when the page has too much information to comment on.
There are also annotations that need to be transmitted over the network to reduce efficiency; internal programmer's test data is generally not written in such comments to avoid disclosure.
JSP processing process:
The processing of JSP source files is divided into two stages:
1. JSP page conversion phase:
The page is compiled into a Java class, and all HTML tags and JSP tags are converted to create a servlet. At this point, the script and expression have not been executed;
2. Request Processing phase:
Occurs on the server, pointing a client request to a JSP page.
A Request object is created, parsed, and committed to the servlet corresponding to the compiled JSP.
When the servlet processes the request, it executes the processing script applet and expression previously defined in the JSP.
Disadvantages and guidelines for using scripting code 1. Disadvantages:
A. Overuse of scripting code using JSP pages is confusing and difficult to maintain;
B. Scripting code reduced JSP two main benefits: Software reuse and code separation
2. Guidelines:
Use only when the components are powerless or require limited scripting.
JSP Call JavaBean
By introducing javabean,jsp, it is better to separate the page presentation from the business logic.
The business logic is put into the Java bean in the background, reducing the script code in JSP, which is advantageous to the maintainability and reusability of the program.
(i) Defining Java Beans
Form:
class = "ClassName" sope= "scope field" >
id--declares the identifier of the Bean object for easy use elsewhere
Class--bean the type of the object, note that you want to use the fully qualified name
Scope--java the shared scope of the Bean object (page, request, session, application)
Page: Current range (minimum range, shortest life cycle)
Request: The same range of requests (Forward,include)
Session: The same conversation (30 minutes not used, will automatically end)
Application: Same application (maximum range, longest life cycle) ServletContext
For example:
class= "Com.tarena.vo.SuperGirl" scope= "Session"/>
Equivalent to:
<% Supergirl girl= (Supergirl) Session.getattribute ("Girl"); if (girl==null) { new// corresponding ID and class session.setattribute ("Girl" , girl); // value corresponding to scope }%>
You can get the bean's value with an expression:
<%=girl.getname ();%>
(b) Assigning values to JavaBean properties
Form:
<jsp:setproperty name= "JavaBean object Name" property= "JavaBean property name" Value= "property value"/>
Example:
<jsp:setproperty name= "Girl" property= "name" value= "Lily"/>
Equivalent to:
<% girl.setname ("Lily");%>
You can nest JSP expressions:
<jsp:setproperty name= "Girl" property= "name" Value= ' <%=request.getparameter ("name")%> '/>
The name of the property in the Java Bean is consistent with the name of the input field in the form, and you can use the wildcard character * to set the values for all fields at once.
<jsp:setproperty name= "" property= "*"/>
(iii) Get the attribute value of JavaBean
Form:
<jsp:getproperty name= "" property= ""/>
Name: Identifies the specific Bean object, which matches the ID value in the <jsp:useBean> standard action
Property: The identifier in the identity attribute.