javaweb瀛︿範鎬荤粨锛堝崄涓€锛夆€斺€旇嚜瀹氫箟鏍囩寮€鍙戝簱
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javaweb瀛︿範鎬荤粨锛堝崄涓€锛夆€斺€旇嚜瀹氫箟鏍囩寮€鍙戝簱相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/strong' title='strong'>strong
tld set commit demo1 coding 1.0 榛樿 desc涓€銆佽嚜瀹氫箟鏍囩寮€鍙戝簱绠€浠嬶細
銆€銆€Tag鎺ュ彛鐨勬柟娉曪細
銆€銆€銆€銆€
浜屻€佽嚜瀹氫箟鏍囩鍏ラ棬锛氳緭鍑哄鎴锋満ip
銆€銆€1.缂栧啓涓€涓疄鐜皌ag鎺ュ彛鐨刯ava绫?/span>
銆€銆€銆€銆€ViewIPTag.java
1 package com.web.tag;
2
3 import java.io.IOException;
4
5 import javax.servlet.http.HttpServletRequest;
6 import javax.servlet.jsp.JspException;
7 import javax.servlet.jsp.JspWriter;
8 import javax.servlet.jsp.tagext.TagSupport;
9
10 //Tag鎺ュ彛瀹炵幇绫?/span>
11 public class ViewIPTag extends TagSupport{
12 @Override
13 public int doStartTag() throws JspException {
14
15 HttpServletRequest request = (HttpServletRequest) this.pageContext.getRequest();
16 JspWriter out = this.pageContext.getOut();
17
18
19 String ip = request.getRemoteAddr();
20 try {
21 out.print(ip);
22 } catch (IOException e) {
23 throw new RuntimeException(e);
24 }
25
26 return super.doStartTag();
27 }
28 }
銆€銆€銆€銆€
銆€銆€2.鍦╰ld鏂囦欢涓鏍囩澶勭悊鍣ㄧ被杩涜鎻忚堪锛坱ld鏂囦欢鐨勪綅缃?WEB-INF涓?鍙互鎶刟pache-tomcat/webappsexamplesWEB-INFjsp2jsp2-example-taglib.tld锛?/span>
銆€銆€銆€銆€com.tld:杩欎釜鏂囦欢鏀惧湪WEB-INF鐩綍涓?/span>
銆€銆€銆€銆€銆€銆€
1 <taglib xmlns="http://java.sun.com/xml/ns/j2ee"
2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
4 version="2.0">
5
6 <description>A tag library exercising SimpleTag handlers.</description>
7 <tlib-version>1.0</tlib-version>
8 <short-name>com</short-name>
9 <uri>http://www.sina.cn</uri><!-- 缁戝畾url -->
10
11
12 <tag>
13 <name>viewIP</name><!-- 鏍囩鐨勫悕绉?-->
14 <tag-class>com.web.tag.ViewIPTag</tag-class><!-- 鏍囩瀹炵幇绫荤殑瀹屾暣绫诲悕 -->
15 <body-content>empty</body-content><!-- 鏍囩浣撲负绌?-->
16 </tag>
17
18 </taglib>
銆€銆€
銆€銆€
銆€銆€3.鍦╦sp椤甸潰涓鍏ュ拰浣跨敤鑷畾涔夋爣绛?/span>
銆€銆€銆€銆€1.jsp
銆€銆€銆€銆€銆€銆€
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
2 <%@taglib uri="http://www.sina.cn" prefix="com" %> <!-- 瀵煎叆鏍囩 -->
3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
4 <html>
5 <head>
6 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
7 <title>Insert title here</title>
8 </head>
9 <body>
10 鎮ㄧ殑ip:<com:viewIP/><!-- 浣跨敤鏍囩 -->
11 </body>
12 </html>
涓夈€乀ag鎺ュ彛鐨勬墽琛屾祦绋?/strong>
銆€銆€
銆€銆€鑷畾涔夋爣绛捐皟鐢ㄥ浘锛?/span>
銆€銆€銆€銆€
銆€銆€
銆€銆€銆€jsp缈昏瘧鎴恠ervlet閮ㄥ垎婧愪唬鐮?
銆€銆€銆€銆€
1 out.write(" 鎮ㄧ殑ip鏄細");
2 if (_jspx_meth_com_005fviewIP_005f0(_jspx_page_context))
3 return;
4 out.write("
");
5 out.write(" </body>
");
6 out.write("</html>
");
7 } catch (java.lang.Throwable t) {
8 if (!(t instanceof javax.servlet.jsp.SkipPageException)){
9 out = _jspx_out;
10 if (out != null && out.getBufferSize() != 0)
11 try {
12 if (response.isCommitted()) {
13 out.flush();
14 } else {
15 out.clearBuffer();
16 }
17 } catch (java.io.IOException e) {}
18 if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
19 else throw new ServletException(t);
20 }
21 } finally {
22 _jspxFactory.releasePageContext(_jspx_page_context);
23 }
24 }
25
26 private boolean _jspx_meth_com_005fviewIP_005f0(javax.servlet.jsp.PageContext _jspx_page_context)
27 throws java.lang.Throwable {
28 javax.servlet.jsp.PageContext pageContext = _jspx_page_context;
29 javax.servlet.jsp.JspWriter out = _jspx_page_context.getOut();
30 // com:viewIP
31 com.web.tag.ViewIPTag _jspx_th_com_005fviewIP_005f0 = (com.web.tag.ViewIPTag) _005fjspx_005ftagPool_005fcom_005fviewIP_005fnobody.get(com.web.tag.ViewIPTag.class);
32 boolean _jspx_th_com_005fviewIP_005f0_reused = false;
33 try {
34 _jspx_th_com_005fviewIP_005f0.setPageContext(_jspx_page_context);//璋冪敤setPageContext()鏂规硶
35 _jspx_th_com_005fviewIP_005f0.setParent(null); 36 int _jspx_eval_com_005fviewIP_005f0 = _jspx_th_com_005fviewIP_005f0.doStartTag(); 37 if (_jspx_th_com_005fviewIP_005f0.doEndTag() == javax.servlet.jsp.tagext.Tag.SKIP_PAGE) { 38 return true; 39 } 40 _005fjspx_005ftagPool_005fcom_005fviewIP_005fnobody.reuse(_jspx_th_com_005fviewIP_005f0); 41 _jspx_th_com_005fviewIP_005f0_reused = true; 42 } finally { 43 org.apache.jasper.runtime.JspRuntimeLibrary.releaseTag(_jspx_th_com_005fviewIP_005f0, _jsp_getInstanceManager(), _jspx_th_com_005fviewIP_005f0_reused); 44 } 45 return false; 46 }
銆€銆€銆€
銆€
鍥涖€?浼犵粺鏍囩寮€鍙戞妧鏈?/span>
銆€銆€寮€鍙戜汉鍛樺湪缂栧啓Jsp椤甸潰鏃讹紝缁忓父杩橀渶瑕佸湪椤甸潰涓紩鍏ヤ竴浜涢€昏緫锛?/span>
銆€銆€銆€銆€
銆€銆€
銆€銆€1銆佹帶鍒秊sp椤甸潰鏌愪竴閮ㄥ垎鍐呭鏄惁鎵ц
銆€銆€銆€銆€API:
銆€銆€銆€銆€銆€銆€
銆€銆€銆€銆€銆€銆€銆€銆€
銆€銆€鏍囩瀹炵幇绫?
銆€銆€銆€銆€銆€TagDemo1.java:
1 package com.web.tag;
2
3 import javax.servlet.jsp.JspException;
4 import javax.servlet.jsp.tagext.TagSupport;
5
6 public class TagDemo1 extends TagSupport{
7
8 @Override
9 public int doStartTag() throws JspException {
10
11 //瀹為檯寮€鍙戝満鏅€斺€斺€斺€斿鏋滄湁鏉冮檺鍒欒緭鍑猴紝娌℃湁鏉冮檺鍒欎笉杈撳嚭
12 return TagSupport.EVAL_BODY_INCLUDE;
13 }
14
15 }
銆€銆€
銆€銆€鏍囩澹版槑锛?/span>
銆€銆€銆€銆€
1 <tag>
2 <name>demo1</name>
3 <tag-class>com.web.tag.TagDemo1</tag-class>
4 <body-content>JSP</body-content><!--鏍囩浣撶殑鍐呭 empty JSP scriptless tagdepentend-->
5 </tag>
6
銆€銆€tld鏂囦欢绫诲瀷涓殑鍥涚鏍囩浣?EMPTY JSP scriptless tagdepentend
銆€銆€
銆€銆€jsp:
銆€銆€銆€銆€
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <%@taglib uri="http://www.sina.cn" prefix="com" %> 3 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 4 <html> 5 <head> 6 <title>浣跨敤鑷畾涔夋爣绛炬帶鍒秊sp閮ㄥ垎鍐呭鏄惁杈撳嚭锛堟爣绛句綋锛?span style="color: #0000ff"></title> 7 </head> 8 <com:demo1>hahaha</com:demo1> 9 <body> 10 11 </body> 12 </html>
銆€銆€2銆佹帶鍒舵暣涓猨sp椤甸潰鍐呭鏄惁鎵ц
銆€銆€銆€銆€API:
銆€銆€銆€銆€銆€銆€
銆€銆€銆€銆€TagDemo2.java:
銆€銆€銆€銆€銆€銆€
銆€銆€
銆€銆€銆€銆€鏍囩澹版槑锛?/span>
1 <tag>
2 <name>demo2</name>
3 <tag-class>com.web.tag.TagDemo2</tag-class>
4 <body-content>empty</body-content><!--鏍囩浣撲负绌?->
5 </tag>
6
銆€銆€銆€銆€浣跨敤鏍囩锛?/span>
銆€銆€銆€銆€銆€銆€
銆€銆€3銆佹帶鍒舵爣绛句綋閲嶅杈撳嚭
銆€銆€銆€API锛?br>銆€銆€銆€銆€
銆€銆€銆€鏍囩瀹炵幇绫伙細銆€
1 package com.web.tag;
2
3 import javax.servlet.jsp.JspException;
4 import javax.servlet.jsp.tagext.IterationTag;
5 import javax.servlet.jsp.tagext.Tag;
6 import javax.servlet.jsp.tagext.TagSupport;
7 //鎺у埗鏍囩浣撹緭鍑?娆?/span>
8 public class TagDemo3 extends TagSupport{
9 //娆℃暟
10 int x = 5;
11 @Override
12 public int doStartTag() throws JspException {
13 return Tag.EVAL_BODY_INCLUDE;
14 }
15
16 @Override
17 //鍦ㄦ爣绛句綋鎵ц瀹屼箣鍚庛€佺粨鏉熸爣绛句箣鍓嶈皟鐢?/span>
18 public int doAfterBody() throws JspException {
19 x--;
20 if(x>0){
21 return IterationTag.EVAL_BODY_AGAIN;
22 }else {
23 return IterationTag.SKIP_PAGE;
24 }
25 }
26 }
銆€銆€銆€銆€
銆€銆€銆€銆€鏍囩澹版槑锛?/span>
1 <tag>
2 <name>demo3</name>
3 <tag-class>com.web.tag.TagDemo3</tag-class>
4 <body-content>JSP</body-content>
5 </tag>
銆€銆€銆€銆€
銆€銆€銆€銆€浣跨敤鏍囩锛?/span>
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <%@taglib uri="http://www.sina.cn" prefix="com" %> 3 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 4 <html> 5 <head> 6 <title>浣跨敤鑷畾涔夋爣绛炬帶鍒秊sp閮ㄥ垎鍐呭鏄惁杈撳嚭锛堟爣绛句綋锛?span style="color: #0000ff"></title> 7 </head> 8 <body> 9 <com:demo3>鎺у埗鏍囩浣撻噸澶嶈緭鍑?<br/></com:demo3> 10 </body> 11 </html>
銆€銆€銆€銆€
銆€銆€銆€4銆佹敼鍙樻爣绛句綋鍐呭杈撳嚭
銆€銆€銆€銆€銆€API锛?br>
銆€銆€銆€銆€銆€銆€銆€
銆€銆€銆€銆€鏍囩瀹炵幇绫伙細
銆€銆€銆€銆€銆€銆€TagDemo4.java:銆€銆€銆€銆€銆€
1 package com.web.tag;
2
3 import java.io.IOException;
4
5 import javax.servlet.jsp.JspException;
6 import javax.servlet.jsp.tagext.BodyContent;
7 import javax.servlet.jsp.tagext.BodyTag;
8 import javax.servlet.jsp.tagext.BodyTagSupport;
9 import javax.servlet.jsp.tagext.Tag;
10
11 //淇敼鏍囩浣撳唴瀹硅緭鍑?/span>
12 public class TagDemo4 extends BodyTagSupport{
13
14 @Override
15 public int doStartTag() throws JspException {
16 return BodyTag.EVAL_BODY_BUFFERED;//杩斿洖杩欎釜鍙傛暟锛屾湇鍔″櫒浼氭妸鏍囩浣撳皝瑁呮垚瀵硅薄浣滀负鍙傛暟璋冪敤setBodyContent(BodyContent b)鏂规硶
17 }
18 @Override
19 public int doEndTag() throws JspException {
20 //寰楀埌鏍囩浣?/span>
21 BodyContent bc = this.getBodyContent();
22 //寰楀埌鍐呭
23 String content = bc.getString();
24 //杞垚澶у啓
25 content = content.toUpperCase();
26
27 try {
28 this.pageContext.getOut().write(content);
29 } catch (IOException e) {
30 throw new RuntimeException(e);
31 }
32
33 return Tag.EVAL_PAGE;//缁х画杈撳嚭jsp椤甸潰鍏朵粬鍐呭
34 }
35 }
褰撳湪doStartTag()鏂规硶涓繑鍥濨odyTag.EVAL_BODY_BUFFERED鍙傛暟鏃讹紝鏈嶅姟鍣ㄤ細鎶婃爣绛句綋鍐呭灏佽鎴愬璞′綔涓哄弬鏁拌皟鐢╯etBodyContent(BodyContent b)鏂规硶锛岀劧鍚庡湪doEngTag锛堬級
涓皟鐢╣etBodyContent锛堬級鏂规硶杩斿洖BodyContent瀵硅薄锛屼粠鑰屽緱鍒版爣绛句綋鍐呭杩涜淇敼杈撳嚭銆?/span>
銆€銆€銆€銆€鏍囩澹版槑锛氥€€銆€
1 <tag>
2 <name>demo4</name>
3 <tag-class>com.web.tag.TagDemo4</tag-class>
4 <body-content>JSP</body-content>
5 </tag>
6
銆€銆€銆€銆€
銆€銆€銆€銆€浣跨敤鏍囩锛?/span>
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <%@taglib uri="http://www.sina.cn" prefix="com" %> 3 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 4 <html> 5 <head> 6 <title>浣跨敤鑷畾涔夋爣绛炬帶鍒秊sp閮ㄥ垎鍐呭鏄惁杈撳嚭锛堟爣绛句綋锛?span style="color: #0000ff"></title> 7 </head> 8 <com:demo4>aaaaaa</com:demo4> 9 <body> 10 11 </body> 12 </html>
銆€銆€銆€銆€5銆乀ag鎺ュ彛鐨勪綋绯伙細
銆€銆€銆€銆€
銆€銆€銆€
銆€銆€銆€銆€銆€銆€
浜斻€佺畝鍗曟爣绛惧紑鍙戞妧鏈?/span>
銆€銆€鐢变簬浼犵粺鏍囩浣跨敤涓変釜鏍囩鎺ュ彛鏉ュ畬鎴愪笉鍚岀殑鍔熻兘锛屾樉寰楄繃浜庣箒鐞愶紝涓嶅埄浜庢爣绛炬妧鏈殑鎺ㄥ箍锛?SUN鍏徃涓洪檷浣庢爣绛炬妧鏈殑瀛︿範闅惧害锛屽湪JSP 2.0涓畾涔変簡涓€涓洿涓虹畝鍗曘€佷究浜庣紪鍐欏拰璋冪敤鐨凷impleTag鎺ュ彛鏉ュ疄鐜版爣绛剧殑鍔熻兘銆傚疄鐜癝impleTag鎺ュ彛鐨勬爣绛鹃€氬父绉颁负绠€鍗曟爣绛俱€傜畝鍗曟爣绛惧叡瀹氫箟浜?涓柟娉曪細
- setJspContext鏂规硶
- setParent鍜実etParent鏂规硶
- setJspBody鏂规硶
- doTag鏂规硶
銆€銆€API锛?/span>
銆€銆€銆€銆€
銆€銆€銆€銆€銆€銆€瀹炵幇绫伙細
銆€銆€銆€銆€銆€銆€
銆€銆€銆€1銆佹帶鍒舵爣绛句綋鏄惁鎵ц
銆€銆€銆€銆€鏍囩瀹炵幇绫伙細
銆€銆€銆€銆€銆€銆€SimpleTagDemo1.java:銆€銆€銆€銆€銆€
1 import javax.servlet.jsp.JspException;
2 import javax.servlet.jsp.tagext.JspFragment;
3 import javax.servlet.jsp.tagext.SimpleTagSupport;
4 //鎺у埗鏍囩浣撴槸鍚︽墽琛?/span>
5 public class SimpleTagDemo1 extends SimpleTagSupport{
6
7 @Override
8 public void doTag() throws JspException, IOException {
9 //鑾峰彇鏍囩浣撳璞?/span>
10 JspFragment jf = this.getJspBody();
11 for(int i=0;i<5;i++){
12 //鎵ц浜旀锛屽鏋滀笉鎵ц锛屽垯浠€涔堥兘涓嶅啓灏卞彲浠?/span>
13 jf.invoke(this.getJspContext().getOut());//null涔熷彲浠ラ粯璁よ緭鍑虹粰娴忚鍣?/span>
14 }
15
16 }
17 }
銆€銆€銆€鏍囩澹版槑:
1 <?xml version="1.0" encoding="UTF-8" ?>
2
3 <taglib xmlns="http://java.sun.com/xml/ns/j2ee"
4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
6 version="2.0">
7
8 <description>A tag library exercising SimpleTag handlers.</description>
9 <tlib-version>1.0</tlib-version>
10 <short-name>simple</short-name>
11 <uri>/simpletag</uri>
12
13
14 <tag>
15 <name>demo1</name>
16 <tag-class>com.web.simpletag.SimpleTagDemo1</tag-class>
17 <body-content>scriptless</body-content><!-- jsp2.0涔嬪悗涓嶈繍琛屾爣绛句綋閲岄潰鍐欒剼鏈唬鐮侊紝鎵€浠ョ敤scriptless -->
18 </tag>
19
20 </taglib>
銆€銆€銆€銆€浣跨敤鏍囩锛氥€€銆€
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
2 <%@taglib uri="/simpletag" prefix="simple" %>
3 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
4 <html>
5 <head>
6 <title>My JSP 鈥?.jsp鈥?starting page</title>
7 </head>
8
9 <body>
10 <simple:demo1>hahaha <br/></simple:demo1>
11 </body>
12 </html>
銆€銆€
銆€銆€銆€銆€
以上是关于javaweb瀛︿範鎬荤粨锛堝崄涓€锛夆€斺€旇嚜瀹氫箟鏍囩寮€鍙戝簱的主要内容,如果未能解决你的问题,请参考以下文章
2019-2020-1瀛︽湡20192429銆婄綉缁滅┖闂村畨鍏ㄤ笓涓氬璁恒€嬬涓夊懆瀛︿範鎬荤粨
#2019-2020-1瀛︽湡 20192419 銆婄綉缁滅┖闂村畨鍏ㄤ笓涓氬璁恒€嬬涓冨懆瀛︿範鎬荤粨 锛堣涔﹀績寰楋級
Spring Boot锛堝崄涓€锛塕edis闆嗘垚浠嶥ocker瀹夎鍒板垎甯冨紡Session鍏变韩