INFO: Request for /FirstProject/SessionServlet; Count=1
Request
web.xml :
<listener>
<listener-name>MyRequestListener</listener-name>
<listener-class>chapt21.MyRequestListener</listener-class>
</listener>
Filter , . , . , , ( ) , . ,
, JSP-.
, :
, ;
request-response;
;
;
;
.
Filter, FilterChain FilterConfig javax.servlet. Filter.
void doFilter(ServletRequest req, ServletResponse res, FilterChain chain),
, . , / FilterChain. , .
, void init(FilterConfig config), FilterConfig. destroy() , .
init(), doFilter() , . destroy().
doFilter() , FilterChain, . FilterChain , /. chain.doFilter() .
request JSP- demofilter.jsp testName.
<!-- # 7: : demofilter.jsp -->
<%@ page language="java" contentType="text/html;
charset=ISO-8859-5" pageEncoding="ISO-8859-5"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
|
|
<html><head><title>Demo Filter</title></head>
<body>
<c:out value="Info from filter: ${info}"/> <br>
<P> </P>
</body></html>
, info .
Filter :
/* # 8: : MyFilter.java */
package chapt21;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class MyFilter implements Filter {
private FilterConfig filterConfig;
public void init(final FilterConfig filterConfig) {
this. filterConfig = filterConfig;
}
public void doFilter(final ServletRequest request,
final ServletResponse response, FilterChain chain)
throws java.io.IOException,
javax.servlet.ServletException {
System. out. println(" ");
String value = "Simple Filter";
request.setAttribute("info", value);
chain.doFilter(request, response);
System. out. println("info = " + value);
System. out. println(" a");
}
public void destroy() {
System. out. println(" a");
}
}
, <web-app> web.xml :
<filter>
<filter-name>simplefilter</filter-name>
<filter-class>chapt21.MyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>simplefilter</filter-name>
<url-pattern>/demofilter.jsp</url-pattern>
</filter-mapping>
. . PrintWriter, . , UTF-8.
// # 9: , : SetCharFilter.java
package chapt21;
import java.io.IOException;
import javax.servlet.*;
public class SetCharFilter implements Filter {
private FilterConfig filterConfig = null;
public void init(FilterConfig config)
throws ServletException {
this. filterConfig = config;
}
public void doFilter(ServletRequest request,
ServletResponse response, FilterChain next)
throws IOException, ServletException {
//
String encoding = request.getCharacterEncoding();
System. out. println(encoding);
// UTF-8,
if (!"UTF-8".equalsIgnorCase(encoding))
response.setCharacterEncoding("UTF-8");
next.doFilter(request, response);
}
public void destroy() {
}
}
web.xml:
<filter>
<filter-name>setCharFilter</filter-name>
<filter-class>chapt21. SetCharFilter</filter-class>
|
|
</filter>
<filter-mapping>
<filter-name>setCharFilter</filter-name>
<url-pattern>/DemoCharServlet</url-pattern>
</filter-mapping>
, DemoCharServlet .
/* # 10: :
DemoCharServlet.java*/
package chapt21;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class DemoCharServlet extends HttpServlet {
public void init() throws ServletException {
}
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.print(" !");
}
public void destroy() {
super. destroy();
}
}
:
!
21
. , .
1. , , , . cookie.
2. . , HTML-, cookie, .
3. , , . ? cookie.
4. cookie , .
5. . cookie, .
6. . cookie, . :
) ;
) .
7. , , , , . cookie , . : .
8. cookie , , .
9. - . cookie.
10. . , , cookie.
11. cookie.
12. .
cookie.
13. HTML-.
cookie ( ), .
14. HTML-. cookie. , . : <BR>.
15. HTML- . , HTML , cookie. , . : <BR>.
16. . cookie.
|
|
17. cookie.
18. (*.by, *.ru ..), , cookie.
19. ()
cookie.
20. , .
B
4 . cookie
.
21
21.1.
?
1) , ;
2) invalidate() ;
3) remove() ;
4) delete() ;
5) finalize() .
21.2.
?
1) setAttribute(String name, Object value);
2) removeAttribute();
3) deleteAttribute();
4) setValue(String name, Object value);
5) getAttributeNames();
6) getInactiveTime().
21.3.
- - HttpServletRequest req?
1) HttpSession session = req.getSession();
2) HttpSession session = req.createHttpSession();
3) Session session = req.createSession();
4) Session session = req.getSession();
5) HttpSession session = req.getHttpSession();
6) HttpSession session = req.createSession();
7) HttpSession session = req.getSession(true).
21.4.
Cookie ?
1) Cookie ;
2) Cookie.setName(String name);
3) setValue(String value);
4) cookie 4096 ;
5) cookie;
6) cookie Cookie.setMaxAge(int day).
21.5.
Cookie ?
1) Cookie c1 = new Cookie ();
2) Cookie c2 = new Cookie ("cookie2");
3) Cookie c3 = new Cookie ("$cookie3", "value3");
4) Cookie c4 = new Cookie ("cookie 4", "value4");
5) Cookie c5 = new Cookie ("cookie5", "value5");
6) Cookie c6 = new Cookie ("6cookie", "value6");
7) Cookie c7 = new Cookie ("c7,8", "value7").
21.6.
cookie - HttpServletResponse resp?
1) resp.setCookie(Cookie cookie);
2) resp.addCookie(Cookie cookie);
3) resp.createCookie(Cookie cookie);
4) resp.putCookie(Cookie cookie);
5) resp.setCookies(Cookie cookie).
JSP 1.1 . Web-, , Java. , . Java- JSP Java-,
. JSP- .
|
|
, , ( .tld), , XML- .
Java, javax.servlet.jsp.tagext.Tag. , TagSupport BodyTagSupport ( ). Tag , . javax.servlet.jsp , , java.io .
doStartTag(), , , .
, ./ # 1: : GetInfoTag.java */
package test.mytag;
import javax.servlet.jsp.tagext.TagSupport;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import java.io.IOException;
// - (. #7 )
import test.my.MySet;
public class GetInfoTag extends TagSupport {
public int doStartTag() throws JspException {
// ,
int size = new Integer(new MySet().getSize());
String str = "Size =<B>(" + size + ")</B>";
try {
JspWriter out = pageContext.getOut();
out.write(str);
} catch (IOException e) {
throw new JspException(e.getMessage());
}
return SKIP_BODY;
}
}
, doStartTag() SKIP_BODY, .
, write() JspWriter, str. pageContext PageContext , TagSupport, , JSP. getOut() JspWriter, . PageContext :
getRequest() ;
getResponse() ;
getServletContext() ;
getServletConfig() ;
getSession() ;
ErrorData getErrorData() .
:
forward(String relativeUrlPath) action-;
include() ServletRequest ServletResponse, .
XML-. XML .
.tld <taglib>, <tag>.
, JSP, , .
, : test.mytag.GetInfoTag. XML- XSD, <taglib>.
, <taglib>, :
tlib-version ;
short-name . JSP-;
uri , . , , web.xml;
info .
|
|
<taglib> <tag>. tag <tag> </tag> :
name , taglib;
tag-class - ;
info ;
body-content empty, . , JSP-, jsp, , ,
tagdependent.
mytaglib.tld, JSP 2.0 :
<?xml version="1.0" encoding="ISO-8859-1"?>
<taglib
xmlns="http://java.sun.com/JSP/TagLibraryDescriptor"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/ web-jsptaglibrary_2_0.xsd"
version="2.0"> <!-- -->
<tlib-version>1.0</tlib-version>
<short-name> mytag </short-name>
<uri> /WEB-INF/mytaglib.tld </uri>
<tag>
<name> getinfo </name>
<!-- -->
<tag-class> test.mytag.GetInfoTag </tag-class>
<body-content> empty </body-content>
</tag>
</taglib>
JSP 2.1 taglib :
<taglib version="2.1"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
web-jsptaglibrary_2_1.xsd">
URI mytaglib.tld :
1. web.xml,
<welcome-file-list\>:
<jsp-config>
<taglib>
<taglib-uri>/WEB-INF/mytaglib.tld</taglib-uri>
<taglib-location>/WEB-INF/mytaglib.tld
</taglib-location>
</taglib>
</jsp-config>
2. URI - (.tld) /WEB-INF . web.xml . , URI. , TLD- , , , .
JSP :
<!-- # 2: : demotag1.jsp -->
<HTML><HEAD>
<%@ taglib uri="/WEB-INF/mytaglib.tld"
prefix="mytag" %>
</HEAD>
<BODY>
<mytag:getinfo/>
</BODY>
</HTML>
:
Size = (3)
. *.tld , tag attribute. attribute <attribute> </attribute> :
name ( );
required , , true false ( );
rtexprvalue , JSP- ${expr} <%=expr%> ( true) ( false). false, , ( ).
, , set ().
firstname, :
// # 3: : HelloTag.java
package test.mytag; import javax.servlet.jsp.tagext.TagSupport; import java.io.IOException; public class HelloTag extends TagSupport { private String firstname; public void setFirstname(String firstname) { this. firstname = firstname; } public int doStartTag() { try { pageContext.getOut().write("Hello, " + firstname); } catch (IOException e) { e.printStackTrace(); } return SKIP_BODY; }} mytaglib.tld
:
<tag>
<name> hello </name>
<tag-class> test.mytag.HelloTag </tag-class>
<body-content> empty </body-content>
<attribute>
<name> firstname </name>
<required> true </required>
<rtexprvalue> true </rtexprvalue>
</attribute>
</tag>
demotag2.jsp :
# 4: : demotag2.jsp
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %><%@ taglib uri="/WEB-INF/mytaglib.tld" prefix="mytag"%><%@ page
language="java"
contentType="text/html; charset=CP1251"
pageEncoding="CP1251"
%><HTML><HEAD><TITLE>demotag2.jsp</TITLE>
</HEAD>
<BODY>
<c:set var="login" value="Bender"/>
< mytag:hello firstname ="${login}" />
</BODY>
</HTML>
:
http://localhost:8080/FirstProject/demotag2.jsp
:
Hello,
, , body. body - content. body-content:
empty ;
jsp , JSP-. JSP-;
tagdependent , . .
, BodyTagSupport, BodyTag. TagSupport ( BodyTagSupport), , :
void doInitBody() , doStartTag() doAfterBody();
int doAfterBody() . EVAL_BODY_AGAIN, doAfterBody() . SKIP_BODY, ;
int doEndTag() , .
, doStartTag() EVAL_BODY_INCLUDE EVAL_BODY_BUFFERED; SKIP_BODY, doInitBody() .
, num ( num setNum(String num)) , :
// # 5: : AttrTag.java
package test.mytag;
import java.io.IOException;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
public class AttrTag extends BodyTagSupport {
private int num;
public void setNum(String num) {
this. num = new Integer(num);
}
public int doStartTag() throws JspTagException {
try {
pageContext.getOut().write(
"<TABLE BORDER=\"3\" WIDTH=\"100%\">");
pageContext.getOut().write("<TR><TD>");
} catch (IOException e) {
Throw
new JspTagException(e.getMessage());
}
return EVAL_BODY_INCLUDE;
}
public int doAfterBody()
throws JspTagException {
if (num-- > 1) {
try {
pageContext.getOut().write("</TD></TR><TR><TD>");
} catch (IOException e) {
Throw
new JspTagException(e.getMessage());
}
return EVAL_BODY_AGAIN;
} else {
return SKIP_BODY;
}
}
public int doEndTag() throws JspTagException {
try {
pageContext.getOut().write("</TD></TR>");
pageContext.getOut().write("</TABLE>");
} catch (IOException e) {
Throw
new JspTagException(e.getMessage());
}
return SKIP_BODY;
}
}
.tld :
<tag>
<name> bodyattr </name>
<tag-class> test.mytag.AttrTag </tag-class>
<body-content> JSP </body-content>
<attribute>
<name> num </name>
<required> false </required>
<rtexprvalue> true </rtexprvalue>
</attribute>
</tag>
JSP bodyattr :
# 6: : demotag3.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-5" pageEncoding="ISO-8859-5"%>
<%@ taglib uri="/WEB-INF/mytaglib.tld" prefix="mytag" %>
<HTML><HEAD>
<TITLE>Example</TITLE>
</HEAD><BODY>
<jsp:useBean id="rw" scope="request" class=
"test.my.MySet"/>
<mytag:bodyattr num="${rw.size}">
${rw.element}
</mytag:bodyattr>
<mytag:bodyattr> </mytag:bodyattr>
</BODY></HTML>
JSP :
. 22.1.
Rows, :
/* # 7: -: MySet.java */
package test.my;
public class MySet extends java.util.HashSet {
private java.util.Iterator it;
public MySet() {
//
this. add("Sun");
this. add("Microsoft");
this. add("IBM");
}
public String getSize() {
it = this. iterator();
return Integer.toString(this. size());
}
public String getElement() {
return it.next().toString();
}
}
action
jsp:attribute XML-, :
<%@ taglib uri="/WEB-INF/mytaglib.tld" prefix="mytag" %>
<HTML>
<mytag:hello>
<jsp:attribute name="firstname">
Bender
</jsp:attribute>
</mytag:hello>
</HTML>
:
Hello, Bender
jsp:attribute, jsp:body:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core"
prefix="c"%>
<%@ taglib uri="/WEB-INF/mytaglib.tld" prefix="mytag" %>
<HTML>
<jsp:useBean id="rw" scope="request"
class="test.my.MySet"/>
<mytag:bodyattr>
<jsp:attribute name="num">
<c:out value="${requestScope.rw.size}"/>
</jsp:attribute>
<jsp:body>
<c:out value="${requestScope.rw.element}"/>
</jsp:body>
</mytag:bodyattr>
</HTML>
:
IBM |
Sun |
Microsoft |
jsp:element name XML jsp:attribute jsp:body:
<jsp:element name="H2" >
<jsp:attribute name="Style">
color:red
</jsp:attribute>
<jsp:body>
Simple Text
</jsp:body>
</jsp:element>
:
<H2 Style="color:red">Simple Text</H2>
jsp:doBody jsp:invoke
-. jsp:doBody , JspWriter . jsp:invoke jsp:doBody -. , bodyattr :
<%@ taglib uri="http://java.sun.com/jsp/jstl/core"
prefix="c"%>
<%@ taglib prefix="tags" tagdir="/WEB-INF/tags" %>
<tags:actiondemo>
<jsp:attribute name="num">
<c:out value="${sessionScope.mysetInstance.size}"/>
</jsp:attribute>
<jsp:body>
<c:out value="${sessionScope.mysetInstance.element}"/>
</jsp:body>
</tags:actiondemo>
actiondemo.tag /WEB-INF/tags:
<%@ tag import="test.my.MySet" %>
<%@ attribute name="num" fragment="true" %>
<%@ variable name-given="mysetInstance" %>
<%session.setAttribute("mysetInstance", new MySet());%>
<TABLE border=1>
<TR><TD>Rows number: <jsp:invoke fragment="num"</TD></TR>
<TR><TD><jsp:doBody /></TD></TR>
<TR><TD><jsp:doBody /></TD></TR>
<TR><TD><jsp:doBody /></TD></TR>
</TABLE>
tag page JSP. attribute actiondemo, fragment="true", jsp:invoke. variable JSP-.
:
Rows number: 3 |
IBM |
Sun |
Microsoft |
22
, (, .) .
1. , , . . . . .
2. , , , , , .
3. a1, a2,, an. Si = ai+ai+1 ++ aj 1£i£j£N . N .
4. . N , . N .
5. 100- .
, .
6. ,
k , , n m , k = n + m. , n m .
7. , , , . n . m < n , .
8. , , 1 2, 1 2 . . , , , 1 , 1 = 2, 2 , .
9. n- , ,
m . n, m . .
10. m . ,
. .
11. , , , . .
12. . 5 . (1 2, 2 3,..., 4 5) ? .
13. . , , .
14. . , ( ), .
15.
. , .
B
.
22
22.1.
<attribute> , ?
1) <attribute-name>;
2) <name>;
3) <attributename>;
4) <param-name>.
22.2.
BodyTag.
doAfterBody()?
1) BodyTag doAfterBody();
2) 0;
3) 1;
4) 0 1;
5) .
22.3.
, doStartTag() Tag.SKIP_BODY?
1) doAfterBody();
2) doBody();
3) skipBody();
4) doEndTag();.
5) .
22.4.
<taglib> web.xml?
1) <uri-tag>;
2) <tag-uri>;
3) <uri-name>;
4) <uri-location>;
5) <taglib-uri>.
22.5.
.tld?
1) value;
2) name;
3) rtexprvalue;
4) class.
22.6.
doInitBody() BodyTagSupport?
1) ;
2) ;
3) SKIP_BODY, EVAL_BODY_INCLUDE;
4) void.
22.7.
.tld , ?
1) body-content jsp;
2) script-enabled true;
3) , .
HTML
HTML (HyperText Markup Language) Internet , , , ,
, , .. JSP, , HTML-.
HTML. 1999 HTML 4.01. , XML. XHTML 1.0/ XHTML 2.0, MathML ( ) .
HTML-, HTML - , HTML, :
(Strict) , (deprecated):
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
(Transitional) HTML:
<!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
(Frameset) , :
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN"
"http://www.w3.org/TR/html4/frameset.dtd">
, XHTML 1.0, HTML 4.0. XHTML 2.0 . font, i, b, center, align, size bgcolor. ,
. , XHTML 2.0 section, , h .
, XHTML 2.0, XML, , . :
, ;
center, font, s, u, align, bgcolor, background, clear, color, face, size, .. , HTML (, , , ..), , ;
, . .
! ( li). , : <br>, <img> .. , /.
HTML- HTML- html htm. HTML- Web-, .
HTML- (). < html> ( < > ) ( </> ). :
<html>
<!-- -->
</ html>
HTML , ( XML ).
<HEAD>
<HTML>. . :
<HTML><HEAD>
<TITLE> </TITLE>
</HEAD></HTML>
<TITLE>
Web- , <TITLE> </TITLE> , .
<BODY>
<BODY> </BODY>. , . <HTML>, <HEAD> <BODY> . , Web- .
<!-- # 1: HTML -->
<HTML>
<HEAD><TITLE> </TITLE></HEAD>
<BODY>
<!-- -->
<H1> , 1</H1>
<H6> , 6</H6>
<ADDRESS> - e-mail:[email protected]</ADDRESS> <P>
</BODY></HTML>
: <P> . <ADDRESS>
. HTML . :
<!-- -->
HTML- , , .. 1, .. . 1 :
<H1> </H1>
(start-
stop-). , . , : <P>, , , . , .