macleo
东东车
2010-07-15 22:57
浏览 227
已采纳

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

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
    CaiHuajiang 2010-07-16 00:09
    已采纳

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

    点赞 评论
  • dk101
    DK101 2010-07-16 11:47

    对的,我和楼上看法一样,但是没有去研究过,不同的容器,具体怎么实现。

    点赞 评论

相关推荐