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 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 <% Java code %>
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
<Jsp: useBean id = "stu" class = "com. westaccp. test. Student" scope = "page/session/application/request"/>
2. setProperty: assign values to attributes of JavaBean.
<Jsp: setProperty name = "stu" property = "stuName" value = "zhangsan"/>
<Jsp: setProperty name = "stu" property = "stuName" param = "txtName"/>
Value and param cannot be used at the same time
Lazy method: <jsp: setProperty name = "stu" property = "*"/>
Note that the name of the form element must be the same as the value of the JavaBean attribute.
Exactly
3. getProperty: Get the property value of JvaBean
<Jsp: getProperty name = "stu" property = "stuName"/>
4. forward: Internal jump, equivalent to request. getRequestDispatcher (). forward (request, response );
<Jsp: forward page = "index. jsp"/>
5. include: Contains
<Jsp: include page = "header. jsp" flush = "true"/>
Expression Language:
EL: Expression Language
Syntax format: $ {expression}
Expression = Operator + operand
Operator: Compared with Java, an empty is added and an assignment operator is missing.
$ {Empty ""}: true
$ {Empty null}: true
Operands:
--> Constant: Boolean (true/false), integer, floating point, string (can be '', or" "), Null
--> Variable:
1. It refers to the attributes (page, request, session, application) placed in four standard scopes)
2. search order within the specified range: page --> request ---> session ---> application
3. How to obtain the variable value: the vertex operator. Also use []
<%
Request. setAttribute ("name", "lisi ");
%>
$ {RequestScope. name}
Or
$ {RequestScope ["name"]}
--> Implicit object
1. pageContext: through which you can access request, session, and servletContext
2. The following range is related to pagination, requestScope, sessionScope, and applicationScope.
3. Input-related: param, paramValues
4. Other: 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 handler implementation
---> 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>
<Tag>
<Name> showbody </name>
<Tag-class> com. westaccp. test. ShowBodyTag </tag-class>
<Body-content> empty/jsp </body-content>
<Attribute>
<Name> color </name>
</Attribute>
</Tag>
</Taglib>
---> Usage: <% @ taglib uri = "WEB-INF/mytag. tld" prefix = "my" %>
<My: showbody/>
2. Tag file
---> Implementation and description
Implemented in the. tag file
Set subject content: <% @ body-content = "empty/scriptless" %>
Set attributes: <% @ attribute name = "name" required = "true" rtexprvalue = "true" %>
Subject content: <jsp: doBody scope = "session" var = "theBody"/>
<%
String body = (String) session. getAttribute ("theBody ");
%>
---> Use
WEB-INF/tags/sayhello. tag
<% @ Taglib tagdir = "/WEB-INF/tags/" prefix = "you" %>
<You: sayhello/>
Standard tag Library:
1. Core tag Library
--> General:
Set: <c: set var = "" value = "" scope = ""/>
Out: <c: out value = ""/>
Remove: <c: remove var = "" scope = ""/>
--> Condition:
If: <c: if test = "">... </c: if>
Choose: <c: choose>
<C: when test = "">... </c: when>
<C: when test = "">... </c: when>
<C: when test = "">... </c: when>
.....
<C: otherwise>... </otherwise>
</C: choose>
--> Iteration:
ForEach: <forEach var = "" items = "" varStatus = "" begin = "" end = "">
FoTokens: <foTodens var = "" items = "" delim = ",; |"> </foTodens>
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: Set the data source to obtain the connection to the database.
--> Query: query execution
--> Update: Execute add, delete, and modify
--> 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: 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.