introduction of Built-in objects
Built-in objects cannot be in <%! Used in%>.
Built-in objects |
Owning class |
PageContext |
Javax.servlet.jsp.PageContext |
Request |
Javax.servlet.http.HttpServletRequest |
Response |
Javax.servlet.http.HttpServletResponse |
Session |
Javax.servlet.http.HttpSession |
Config |
Javax.servlet.ServletConfig |
Application |
Javax.servlet.ServletContext |
Out |
Javax.servlet.jsp.JspWriter |
Page |
Java.lang.Object |
exception |
Java.lang.Throwable |
For easy documentation, be sure to remember the classes you belong to (there is no such class in the Java SE documentation and you need to download the Java EE document);
The built-in objects of the JSP are important in the JSP, which are created by the Web container, so users do not have to create them themselves.
The creation of these built-in objects can be clearly seen in the Servlet class generated by the JSP, but it should be noted that exception is not generally created, only if the JSP page is an error page, that is, only the JSP contains iserrorpage= "true" CHICAI create exception object;
Several of the main built-in objects are:
(1) Request:javax.servlet.http.HttpServletRequest, representing the customer request.
Specific usage: request.getparameter ("name") and so on.
(2) Response:javax.servlet.http.HttpServletResponse, indicating server response.
(3) PageContext:javax.servlet.jsp.pageContext, representing the JSP page.
(4) Session:javax.servlet.http.HttpSession, representing a session.
(5) Application:javax.servlet.servletContext, which indicates that all users share information.
(6) Out:javax.servlet.jsp.jspWriter, write page content.
(7) Page: Represents an instance of a page.
(8) Config:javax.servlet.servletConfig, representing configuration file information.
Two, 4 kinds of attribute range
(1) page range (PageContext): Valid on one page, jump is invalid.
(2) Request scope: Server jump valid, client jump invalid.
(3) Session scope: Jump valid, new open browser is invalid.
(4) Application range: All users are valid and the reboot server is invalid.
These four objects have 3 methods:
(1) void setattribute (String key,object o); Setting properties
(2) Object getattribute (String key); After you have made the transition downward.
(3) void RemoveAttribute (String key); Delete attribute
Note: After the getattribute must be transformed.
Example:
Pagecontext.setattribute ("name", "Xiazdong"); Can only be saved in a single page
String name = (string)Pagecontext.getattribute ("name"); You can't get it when you change a page
Note: Keep the property settings as small as possible while satisfying the functionality. This can improve performance; third, Request object
Note: request.getsession () can obtain the session object;
The Request object is information received from the client, and the client sends the header information, content, delivery method, cookie.
Common methods:
(1) request.getparameter (String name): Gets the value of the name element. Get Source: (1) address rewrite (2) Form submission
(2) enumeration ENU = Request.getparameternames (): Gets the name of all the parameters and iterates through the iterator.
Supplement: Enumeration usage:
enu.hasmoreelements ();
enu.nextelement ();
(3) string[] STRs = request.getparametervalues (String name): Multiple selections for obtaining a checkbox, for example.
(4) Enumeration ENU = Request.getheadernames (): Gets the name of all header information.
(5) String str = Request.getheader (string name): Gets the contents of the header information named name.
(6) String str = Request.getmethod (): Obtained is post or get.
(7) request.setcharacterencoding (String): The encoding of the unified request. For example, the server's JSP page set to GBK, and the browser is UTF-8, there will be encoding mismatch problem.
(8) Request.isuserinrole (String name); For user authentication roles
(9) Cookie[] C = request.getcookies (): Gets all cookies. Example 1: Display all header information
Enumeration ENU = Request.getheadernames ();
while (Enu.hasmoreelements ()) {
String name = (string) enu.nextelement ();
String value = Request.getheader (name);
Display name and value can
}
Example 2: Obtaining the client IP address
String IP = request.getremoteaddr ();
The request also has some less common methods:
(1) request.getremoteaddr (); Returns the IP address of the client;
(2) Request.getservletpath (); Returns the directory of the current file in the server;
(3) Request.getcontextpath (); Returns the home directory information;
Tomcat ways to add new users and new permissions:
(1) The tomcat/conf/tomcat-users.xml can be set, referencing the admin configuration, and then restarting the Tomcat server to complete the load of new users and permissions.
(2) Set up in the Web.xml, make the login page will jump out of the window to enter the user name, password. specifically see: http://blog.csdn.net/xiazdong/article/details/6894889
Iv. The difference between post and get
The most obvious difference is the difference between the address bar, when post is passed, the address bar does not change, and get passes, the address bar displays the information passed, so the information is limited.
v. Response Objects
Represents an object that responds to a client.
1. Common methods:
(1) Addcookie (cookie): Add cookie
(2) SetHeader (String name,string value): Sets header information.
(3) Sendredirect (): Redirect, similar to client jump.
(4) Getoutputstream (); Output stream
Common usage:
(1) Timed refresh: Response.setheader ("Refresh", "2"): 2 seconds refresh.
(2) Timed Jump: Response.setheader ("Refresh", "2; Url=hello.jsp "); 2 seconds to jump to hello.jsp. Note: This is a client-side jump;
(3) Client Jump: Response.sendredirect ("hello.jsp");
Note: The above (2) usage may fail because the user refreshes the page within 2 seconds, causing the refresh to fail;
Application:
Implementation of the user login after the "3 seconds after the jump, if no jump click here";
difference between server jump and client jump:
Server-side jump is immediately jump, that is, the page execution to jump statements after the execution of the following statements;
The client jump is done after the entire page is executed. 2. Manipulate cookies:
Javax.servlet.http.Cookie;
Saves the user's information as a cookie, stored on the client, and sends the cookie to the server at each request.
According to our Gongkong teacher said: A person has a piece of paper, this piece of paper is his cookie, when previously logged in page A, it will be recorded on the paper, if you enter page A again, people will know that you have entered the page A and know your identity.
Response.addcookie (cookie e); add cookies.
Cookies are located in Javax.servlet.http.cookie.
Common methods:
(1) Cookie C = new Cookie ("name", "value"): Create a fresh cookie.
(2) c.setmaxage (int seconds): set the maximum lifetime of a cookie, because cookies are stored in the browser, and if the browser is off, the cookie is lost.
(3) C.getname (): Get the name.
(4) C.getvalue (): Get the value.
Cookie[]cs = request. getcookies (); Get cookies
six, Session object
Session Common methods:
(1) String getId (): Gets the session ID. The session ID is similar to the jsessionid of the cookie, which is allocated automatically by the server;
(2) Session.setattribute ("", ""): Set properties.
(3) Session.getcreationtime (): Get creation time.
(4) Session.getlastaccessedtime (): Get last visit time.
(5) Session.isnew (): Whether this session is new, that is, to determine the session ID.
(6) Session.invalidate (): Set all the properties of the session to empty.
Login-Logout page:
(1) The use of Session.setattribute () registration.
(2) Use Session.getattribute () to determine whether you have registered.
(3) using session.invalidate (); Cancellation
If you want to save the session ID, you need to persist the operation, see: http://blog.csdn.net/xiazdong/article/details/6894945
Vii. Application Objects
Javax.servlet.ServletContext;
Common methods:
(1) Getrealpath (path): Get the real path.
(2) Getcontextpath (): Get the virtual path.
(3) Getinitparameter (String);
We can add initialization parameters to the Web.xml:
<context-param>
<param-name></param-name>
<param-value></param-value>
</context-param>
Application==this.getservletcontext ()
eight, config object
Javax.servlet.ServletConfig;
Start with the configuration file, in each Web application, there is a Web-inf folder, which is very secure and will not be seen by others, because it cannot be accessed directly unless it is accessed through a mapping relationship;
If you put a hello.jsp file in the Web-inf, if you can make it accessible.
The answer is in the Web-inf/web.xml settings can be, the contents are as follows:
<servlet>
<servlet-name>he</servlet-name>
<jsp-file>/WEB-INF/hello.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>he</servlet-name>
<url-pattern>hello</url-pattern>
</sevlet-mapping>
After restarting the server, enter Http://localhost:8080/test/hello in the Address bar.
Config Common method:
(1) getinitparameternames ();
(2) Getinitparameter (String name);
What is the initialization parameter?
Initialization parameters are configured in Web.xml in the form of:
<servlet>
<init-param>
<param-name>name</param-name>
<param-value>value</param-value>
</init-param> </servlet>
Nine, PageContext objects
Represents the JSP context, through which you can get request\response\session\<jsp:forward>, and so on.
(1) Pagecontext.forward (String);
(2) Pagecontext.include (String);
(3) pagecontext.getservletconfig ();
(4) Pagecontext.getservletcontext ();
(5) pagecontext.getrequest ();
(6) pagecontext.geresponse ();
(7) pagecontext.getsession ();
In other words, as long as there is a PageContext object, you can complete the functions of all built-in objects;
add: Pageconext set any range of properties (rarely used)
Use functions: Pageconext.setattribute ("name", "value", int SCOPE);
1. Set Page range
Pageconext.setattribute ("name", "Xiazdong", Pageconext.page_scope);
2. Set Request scope
Pageconext.setattribute ("name", "Xiazdong", Pageconext.request_scope);
3. Set Session scope
Pageconext.setattribute ("name", "Xiazdong", Pageconext.session_scope); 4. Set Application Range
Pageconext.setattribute ("name", "Xiazdong", Pageconext.application_scope);
Therefore, only need PageContext can complete the operation of all the built-in objects;
10, exception object
can only be used if iserrorpage= is "true." Common methods: (1) getMessage (); The
11, out object
Javax.servlet.jsp.JspWriter
is typically used to output character streams, or text streams, but if you need to pass information such as pictures, You need to pass through response;
public void _jspservice (final httpservletrequest request, Final httpservletresponse response)//Create request, response
Throws Java.io.IOException, servletexception {final PageContext pagecontext;
HttpSession session = NULL;
Final ServletContext Application;
Final ServletConfig config;
JspWriter out = null;
Final Object page = this;
JspWriter _jspx_out = null;
PageContext _jspx_page_context = null;
try {response.setcontenttype ("text/html;charset=utf-8");
PageContext = _jspxfactory.getpagecontext (this, request, response,///create PageContext null, True, 8192, true);
_jspx_page_context = PageContext; application = Pagecontext.getservletcontext (); Create application that is ServletContext config = Pagecontext.getservletconfig (); Create config session = Pagecontext.getsession (); Create Session out = Pagecontext.getout (); Create out _jspx_out = out;
Summary: If you want to output in a JSP page, use OUT.PRINTLN ();
add: Disable scripts and disable El
1. Disable Script
Configure the following template in Web.xml:
<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
< scripting-invalid>true</scripting-invalid>
</jsp-property-group>
</jsp-config>
You can disable scripting for all of the JSPs.
Effect: If Scriptlet is disabled, it appears: "Org.apache.jasper.JasperException";
2. Disabling El
Configure the following template in Web.xml:
<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
< el-ignored>true</el-ignored>
</jsp-property-group>
</jsp-config>
or iselignored= "true" in the JSP page instruction;
If two configurations contradict each other, the JSP page instruction is preferred;
Effect: If El is disabled, continue using El as normal text, that is, if ${requestscope.aaa} then output: "${requestscope.aaa}"
Whether to disable the El Summary table
<el-ignored> |
Iselignored |
Whether to ignore El expressions |
Not specified |
Not specified |
Do not ignore |
False |
Not specified |
Do not ignore |
true |
Not specified |
Ignore |
False |
False |
Do not ignore |
False |
True |
Ignore |
True |
False |
Do not ignore |