东东车 2010-07-15 14:57
浏览 231
已采纳

请教一个标签定制的基础问题

DispIP类
[code="java"]package macleo;
import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

public class DispIP extends TagSupport {

/**
 * 
 */
private static final long serialVersionUID = 1L;

public int doStartTag() throws JspException
{
    String ip = pageContext.getRequest().getRemoteAddr();//问题
    try {
        pageContext.getOut().write(ip);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return super.doStartTag();
}

}[/code]
我查阅了j2ee的api,有点问题不明白,请教各位达人,请指点一下.
1.TagSupport类的定义如下:
[code="java"]/**

  • A base class for defining new tag handlers implementing Tag. *
  • The TagSupport class is a utility class intended to be used as

  • the base class for new tag handlers. The TagSupport class
  • implements the Tag and IterationTag interfaces and adds additional
  • convenience methods including getter methods for the properties in
  • Tag. TagSupport has one static method that is included to
  • facilitate coordination among cooperating tags. *
  • Many tag handlers will extend TagSupport and only redefine a

  • few methods. */

public class TagSupport implements IterationTag, Serializabl[/code]
其中有
[code="java"] // protected fields

/**
 * The PageContext.
 */
protected PageContext pageContext;[/code]

2.PageContext定义如下,是抽象类
[code="java"] *

  • The following methods provide support for forwarding, inclusion
  • and error handling:
  • forward(), include(),
  • and handlePageException(). */

abstract public class PageContext
extends JspContext[/code]

其中,getRequest()方法未实现是abstract的定义如下:
[code="java"]
/**
* The current value of the request object (a ServletRequest).
*
* @return The ServletRequest for this PageContext
*/

abstract public ServletRequest getRequest();[/code]

3.getRemoteAddr方法定义(亦未实现)在interface的ServletRequest中,定义如下:
[code="java"] /**
* Returns the Internet Protocol (IP) address of the client
* or last proxy that sent the request.
* For HTTP servlets, same as the value of the
* CGI variable REMOTE_ADDR.
*
* @return a String containing the
* IP address of the client that sent the request
*
*/

public String getRemoteAddr();
[/code]

***我的问题是:DispIP类中此句,两方法均为实现,何以能编译通过呢????晕,请指点!感谢!
[code="java"]String ip = pageContext.getRequest().getRemoteAddr();//问题[/code]

展开全部

  • 写回答

2条回答 默认 最新

  • CaiHuajiang 2010-07-15 16:09
    关注

    这两个类由容器具体实现.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部