Learning notes _ java web-JSP Basics

Source: Internet
Author: User

JSP Java Server Pages
Dynamic Web page Technical Standard for java pages on the server side
Common JSP labels
Set JSP page properties <% @ page %>
<% @ Page import = "" %>
Declare global variables <%! %>
Write java code <%>
Implementation output <% out. pringln (); %>
<% = %>
Add annotation HTML Annotation
JSP comment <% -- %>
JSP code comments <% // %>
<%/**/%>
JSP Execution Process
Translation phase: first compile the file into a java class (servlet)
Compilation phase: compile java into a class
Execution phase: dynamically generate a display page and display it to the user


Character encoding Problems
The default ios-8859-1 in jsp does not support Chinese
Commonly supported Chinese Methods GB2312, GBK, UTF-8
Sets the encoding method for requests and responses.
Set post request
Request. setCharacterEncoding ("UTF-8 ");
Set response
Response. setCharacterEncoding ("UTF-8 ");
Or, page <% @ page contentType = "text/html; charset = ytf-8" %>
Set get request
Governance
If the garbled characters are obtained during s
S = new String (s. getBytes ("iso-8859-1"), "UTF-8 ")
In server. xml
Modify RedirtPort = "" URIEncoding = "UTF-8">
Forwarding and redirection
Forwarding
Request. getRequestDispatcher ("url"). forward (request, response );
Redirection
Response. sendRedirect ("url ");
Compare forwarding redirection
URL change no Yes
Re-request is not
Whether the request is modern or not
Target URL arbitrary Web


JSP built-in Object request and response
Built-in object that can be used without declaration
Difference between get and post submission methods
Get post
Whether the parameter appears in the URL
Length limit?
Poor security
Whether the URL can be propagated


Request object: request
Common Methods
GetParameter (String name );
GetParameterValues (String name );
SetAttribute (String name, Object object)
GetAttribute (String name );
Note: 1. null value judgment
2. Type of the retrieved object to be converted
GetRequestDispatcher ("url"). forward (request, response); forward
SetCharacterEncoding ("UTF-8"); change the character encoding method

Session Object
Session Principle
Each session has an id. The session is stored on the server side and passed the id
Find the session to achieve interaction
Session clearing
1. Actively clear and call the following methods
Session. invalidate (); session invalid
Session. removeAttrbute (String name); session key is the name attribute.
2. automatically clear the session expiration time
Set the session expiration time
Call method: setManInaciveInterval (int intervale (unit: seconds ))
Configure XML: 30 Unit minutes ()
Common Methods
SetAttribute (String name, Object object)
GetAttribute (String name );
Note: 1. null value judgment
2. Type of the retrieved object to be converted
GetId () Get sessionID
Response. sendRedirect ("url"); redirection

Global Object: application
Application shares usage data throughout the project
Common Methods
SetAttribute (String name, Object object)
GetAttribute (String name );
Note: 1. null value judgment
2. Type of the retrieved object to be converted


Cookie
Common Methods
Cookie cookie = new Cookie (String name, Object value );
SetMaxAge (int expiry); set the validity period (in seconds)
Response object: void addCookie (Cookie cookie); add data
Request object: Cookie [] getCookies (); get data
Usage
// ==================== Request page ============================= ==================
Cookie cookie = new Cookie ("user", usernmae );
Cookie. setMaxAge (60*60 );
Response. addCookie (cookie );
// ===================== Response page ================================ ==================
Cookie [] cookies = request. getCookies ();
String;
For (cookie: cookies ){
If (cookie. getname (). equals ("user ")){
A = cookis. getValue ();
}
}

































Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.