Servlet three elements:
1. must inherit from httpservlet
2. doget () or dopost () must be implemented ()
3. servlet must be configured in Web. xml
<Servlet>
<Servlet-Name> </servlet-Name>
<Servlet-class> </servlet-class>
</Servlet>
<Servlet-mapping>
<Servlet-Name> </servlet-Name>
<URL-pattern> </url-pattern>
</Servelt-mapping>
Httpserveltrrequest: request object
Getparameter (): Get the value of the form Element
Getattribute (): Get the attribute value in the request range
Setattribute (): Set the attribute value in the reqeust range
Setcharacterencoding (): sets the character encoding.
Httpserletresponse: Object
Sendredirect (): external jump
Getwriter (): Get the output stream object
Setcontenttype ("text/html; charset = UTF-8"): sets the content format and encoding.
Four session tracking methods:
1. Session
Httpsession session = request. getsession ();
Session. setattribute ("name", "zhangsan ");
Session. setattribute ("PWD", "AAA ");
String name = (string) Session. getattribute ("name ");
2. COOKIE:
// Create a cookie
Cookie = new cookie ("name", "zhangsan ");
// Set the cookie timeout time
Cookie. setmaxage (24*60*60*60 );
// Send the cookie to the client
Response. addcookie (cookie );
// Obtain the cookie sent by the client
Cookie [] cookies = request. getcookies ();
For (INT I = 0; I <cookies. length; I ++ ){
Cookie temp = Cookies [I];
String key = temp. getname ();
String value = temp. getvalue ();
}
3. Hide form fields
<Input type = "hidden" name = "name" value = "zhangsan"/>
Request. getparameter ("name ");
4. URL rewriting
Question mark parameter passing
Loginservlet? Username = zhangsan & PWDs = 123
String name = request. getparameter ("username ");
String Pwd = request. getpareameter ("PWD ");
Internal jump:
Loginservlet
Request. getrequestdispatcher ("index. jsp"). Forward (request, resposne );
External jump:
Response. sendredirect ("index. jsp ");
Internal redirection is a request and a response
External redirects are two requests and two responses.
Servletcontext: servlet context object
It is a public region that can be shared by all clients.
Setattribute (): Put data into the public area
Getattribute (): Get data from the Public Region
II:
Iii. Three standard scopes: request, session, and servletcontext
Common Features: setattribute () and getattribute ()
Difference: the range is different. Request <session <servletcontext
Iv. Four session tracking methods
5. Five objects on the server
Request, response, Servlet, session, servletcontext
JSP: Java Server Page
Page composition: 7 elements
1. Static content: HTML
2. Command: Page, include, taglib:
<% @ Directive name attribute 1 = "attribute value 1" attribute 2 = "attribute value 2" %>
3. expression: <% = expression %>
4. scriptlet <% JavaCode%>
5. Declaration: <%! %>: Variables and Methods
6. Action: <JSP: Action name attribute = "property value"> </jsp: Action Name>
7. Notes:
Invisible to the client: <% -- %>
What the client can see: <! -->
JSP execution process:
1. Translation: JSP ---> Servlet
2. Compile: servlet ---->. Class
3. Execute:. Class
The response speed for the first access to JSP is slow, and the response speed for subsequent requests is fast.
Script:
Expression: <% = %>
Scriptlet: <%>
Declaration: <%! %>
Command:
Page: language, import, errorpage, iserrorpage
Include: File
Taglib: URI: Specify the path prefix of the tag library descriptor: Specify the tag prefix.
Implicit object:
Category:
1. Input and Output objects: Request (httpservletrequest ),
Response (httpservletresponse ),
Out (jspwriter), The out in the servlet is printwriter
2. Scope communication objects: pagecontext, request,
Session (httpsession ),
Application (servletcontext)
3. servlet object: Page (this), config
4. Error object: exception
JavaBean:
A standard JavaBean has three conditions.
1. Common classes
2. Common constructor with no parameters
3. Has the Set () and get () Methods
4. Private attributes
standard actions in JSP:
1. usebean: Create an instance of JavaBean
2. setproperty: assign a value to the JavaBean attribute
value and Param cannot be used simultaneously
lazy method:
note that, the name of the form element must be exactly the same as that of the JavaBean attribute value
3. getproperty: Get the property value of jvabean
4. forward: Internal jump, equivalent to request. getrequestdispatcher (). forward (request, response);
5. include: Contains
Expression Language:
El: Expression Language
syntax format: $ {expression}
Expression = Operator + operand
OPERATOR: compared with Java, an empty is added, and an assignment operator
$ {empty "" }:true
$ {empty null }:true
is missing:
--> constant: Boolean (true/false), integer, floating point, string (''or ""), null
--> variable:
1. attribute (page, request, session, application) in four standard ranges
2. search order within the specified range: page --> request ---> session ---> application
3. how to obtain the variable value: vertex operator ., []
<%
request. setattribute ("name", "Lisi");
%>
$ {requestscope. name}
or
$ {requestscope ["name"]}
--> implicit object
1. pagecontext: You can access request, session, servletcontext
2. related to the scope: pagination, requestscope, sessionscope, and applicationscope
3. related to input: Param, paramvalues
4. others: Header, Cookie, headervalues,
El expressions:
1. It can be used in static text
2. Use with custom tags
3. Use with JavaBean
<JSP: userbean id = "Stu" class = "com. westaccp. Test. Student" Scope = "session"/>
<JSP: setproperty name = "Stu" property = "stuname" value = "hello"/>
$ {Stu. stuname}
Custom Tag:
1. Tag ProcessingProgramImplementation
---> Implementation: inherited from bodytagsupport or tagsupport
Generally, dostarttag (), doendtag (), and doafterbody () are overwritten ()
---> Description: description in the tag library descriptor file (. TLD)
<Taglib>
<Tlib-version> 1.0 </tlib-version>
<JSP-version> 2.0 </JSP-version>
<Short-Name> simpletag </short-Name>
showbody
COM. westaccp. test. showbodytag
empty/jsp
color
---> usage: <% @ taglib uri = "WEB-INF/mytag. TLD "prefix =" my "%>
2. tag file
---> Implementation and description
in.
set subject content in the Tag file: <% @ body-content = "empty/scriptless" %>
set attributes: <% @ attribute name = "name" required = "true" rtexprvalue = "true" %>
subject content:
<%
string body = (string) session. getattribute ("thebody");
%>
---> use
WEB-INF/tags/sayhello. tag
<% @ taglib tagdir = "/WEB-INF/tags/" prefix = "you" %>
standard tag Library:
1. core tag library
--> General:
set:
out:
remove:
--> condition:
If: .....
choose:
...
...
...
.....
...
--> iteration:
foreach:
fotokens:
JAVA, C #; SQL | C
2. i18n and formatting tag library
--> setlocale: Set the local region
--> Bundle: Set the resource package
--> setbundle: Set the resource package
--> message: output Message
3. SQL tag library
--> setdatasource: sets the data source for database connection
--> query: Query
--> Update: add, delete, change
--> transaction: Transaction
--> param: parameter
4. XML tag library
Filter:
Lifecycle:
1. instance China:
2. Initialization: Init ()
3. filter: dofilter ()
4. Destroy: Destroy ()
5. unavailable
Configuration:
<Filter>
<Filter-Name> </filter-Name>
<Filter-class> </filter-class>
</Filter>
<Filter-mapping>
<Filter-Name> </filter-Name>
<URL-pattern> </url-pattern>
</Filter-mapping>
Several important interfaces:
1. filter: Init (), dofilter (), destroy ()
2. filterchain: dofilter (request, response)
3. filterconfig: getfiltername (), getinitparameter (),
Filter chain: ---> 1 ---> 2 ---> 3 ---> Servlet Request
<---- 1 <--- 2 <--- 3 <--- response
MVC design pattern
1. modeli: JSP + JavaBean
2. modelii: JSP + servlet + JavaBean
JSP --- View
Servlet --- Control
JavaBean --- Model
MVC:
M -- model: Model: access the background database
V -- View: Display
C -- Control: Controller: Control Program Flow
relationship between modelii and MVC:
MVC is a design model, and modelii is a specific implementation of MVC.