第七章 JSTL与自定义标签 实训七

BigCitiesServlet.java

package com.demo;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class BigCitiesServlet
 */
@WebServlet("/bigcities")
public class BigCitiesServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public BigCitiesServlet() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	Map<String,String>capitals = new HashMap<String,String>();
	capitals.put("俄罗斯","莫斯科");
	capitals.put("日本","东京");
	capitals.put("中国","北京");
	
	Map<String,String[]>bigcities = new HashMap<String,String[]>();
	bigcities.put("澳大利亚", new String[] {"悉尼","墨尔本","布里班斯"});
	bigcities.put("美国", new String[] {"纽约","洛杉矶","加利福尼亚"});
	bigcities.put("中国", new String[] {"北京","上海","广州"});
	
	request.setAttribute("capitals", capitals);
	request.setAttribute("bigcities", bigcities);
	RequestDispatcher rd = request.getRequestDispatcher("/bigcities.jsp");
	rd.forward(request, response);
	
	
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

bigCities.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>国家和城市</title>
</head>
<body>
<table>
	<tr style = "background:#448755;color:white;font-weight:bold">
		<td>国家</td>
		<td>首都</td>
	</tr>
	<c:forEach items = "${requestScope.capitals }" var = "mapItem">
	<tr>
		<td>${mapItem.key }</td>
		<td>${mapItem.value }</td>
	</tr>
	</c:forEach>
</table>
<br/>

<table>
	<tr style = "background:#448755;color:white;font-weight:bold">
		<td>国家</td>
		<td>城市</td>
	</tr>
	<c:forEach items = "${requestScope.bigcities }" var = "mapItem">
	<tr>
		<td>${mapItem.key }</td>
		<td>
			<c:forEach items = "${mapItem.value }" var = "city" varStatus = "status">
			${city }<c:if test = "${!status.last }">,</c:if>
			</c:forEach>
		</td>
	</tr>
	</c:forEach>
</table>
<br/>
		



</body>
</html>

 

MyFirstTag.java

package com.mytag;
import java.io.IOException;

import javax.servlet.jsp.JspContext;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.JspTag;
import javax.servlet.jsp.tagext.SimpleTag;


public class MyFirstTag implements SimpleTag {
	JspContext jspContext;
	public void doTag()throws IOException,JspException{
		System.out.println("执行doTag()方法");
		jspContext.getOut().print("这是我的第一个标签。");
	}
	public void setParent(JspTag parent) {
		System.out.println("执行setParent()方法");
	}
	public JspTag getParent() {
		System.out.println("执行getParent()方法");
		return null;
	}
	public void setJspContext(JspContext jspContext) {
		System.out.println("执行setJspContext()方法");
		this.jspContext = jspContext;
	}
	public void setJspBody(JspFragment body) {
		System.out.println("执行setJspBody()方法");
	}
	
	

}

mytaglib.tld

<?xml version="1.0" encoding="UTF-8"?>
<taglib xmlns = "http://java.sun.com/xml/ns/j2ee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_1.xsd"
	version = "2.1">
	<description>Simple tag examples</description>
	<tlib-version>1.0</tlib-version>
	<short-name>My First Taglib Example</short-name>
	<uri>http://www.mydomain.com/sample</uri>
	<tag>
		<name>firstTag</name>
		<tag-class>com.mytag.MyFirstTag</tag-class>
		<body-content>empty</body-content>
	
	</tag>
	<tag>
		<description>Computer Square Root</description>
		<name>sqrt</name>
		<tag-class>com.mytag.MathTag</tag-class>
		<body-content>empty</body-content>
		<attribute>
			<name>x</name>
			<requered>true</requered>
		</attribute>
	</tag>
</taglib>

my-first-tag.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri = "http://www.mydomain.com/sample" prefix = "demo" %>    
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>第一个标签</title>
</head>
<body>
	您好!!!<br/>
	<demo:firstTag></demo:firstTag>

</body>
</html>

 

MathTag.java

package com.mytag;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;

public class MathTag extends SimpleTagSupport{
	double x;
	public void setX(String x) {
		double num  = 0;
		try {
			num = Double.parseDouble(x);
		}catch(NumberFormatException nfe) {}
			this.x = num;
		
	}
	public void doTag() throws JspException,IOException{
		JspWriter out = getJspContext().getOut();
		out.println(x + "的平方根是:"+Math.sqrt(x));
	}

}

math.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri = "http://www.mydomain.com/sample" prefix = "demo" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>sqrt标签</title>
</head>
<body>
	<demo:sqrt x="100"/><br>
	<demo:sqrt x="200"/><br>
	<demo:sqrt x="0"/><br>

</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值