偶的第一个tag-servlet

第一步:建立一个tld
命名为first.tld
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib PUBLIC
 "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"
 "web-jsptaglibrary_1_1.dtd">
<taglib>
    <tlibversion>1.0</tlibversion>
    <jspversion>1.1</jspversion>
    <shortname>first tag</shortname>
   
    <tag>
        <name>display</name>
        <tagclass>com.bcb.mywork.Xml</tagclass>
        <bodycontent>empty</bodycontent>
        <attribute>
            <name>name</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
 </tag>
</taglib>

即提出第一个需求,根据此需求,为其定义class
命名为:com.bcb.mywork.Xml.java
/*
 * Created on 2004-10-13
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package com.bcb.mywork;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.TagSupport;

/**
 * @author Administrator
 *
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
public class Xml extends TagSupport {
 protected String name;
 
 public int doStartTag() throws JspException {
  Object out =
   pageContext.getAttribute("gettext", PageContext.SESSION_SCOPE);
  System.err.println(out);
  try {

   pageContext.getOut().print(out);
  } catch (java.io.IOException e) {
   throw new JspTagException(e.getMessage());
  }
  return SKIP_BODY;
 }

 public int doEndTag() throws JspException {
  return EVAL_PAGE;
 }
 public void setName(String Username) {

  Username = name;

 }

 public String getName() {

  return name;

 }
}

写servlet
命名为:com.bcb.mywork.HelloWorld.java
package com.bcb.mywork;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.Servlet;
import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspWriter;

import com.ibm.CORBA.iiop.Response;

/**
 * @version  1.0
 * @author
 */
public class HelloWorld extends HttpServlet implements Servlet {

 /**
 * @see javax.servlet.http.HttpServlet#void (javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
 */
 private String context = new String();
 public void doGet(HttpServletRequest req, HttpServletResponse resp)
  throws ServletException, IOException {

  HttpSession session = req.getSession();
  session.setAttribute("gettext", "HelloWorld");
  System.err.println(session);
  String URL = "/helloworld.jsp";
  RequestDispatcher Forward = req.getRequestDispatcher(URL);
  Forward.forward(req, resp);
 }

 /**
 * @see javax.servlet.http.HttpServlet#void (javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
 */
 public void doPost(HttpServletRequest req, HttpServletResponse resp)
  throws ServletException, IOException {
  context = req.getParameter("posttext");
  System.out.println(context);
  HttpSession session = req.getSession();
  session.setAttribute("gettext", context);
  String URL = "/helloworld.jsp";
  RequestDispatcher Forward = req.getRequestDispatcher(URL);
  Forward.forward(req, resp);

 }

}

制作jsp页面
命名为:helloworld.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<%@ page language="java" contentType="text/html; charset=GBK" pageEncoding="GBK"%>
<%@taglib uri="/WEB-INF/tlds/first.tld" prefix="jet"%>
<META http-equiv="Content-Type" content="text/html; charset=GBK">
<META name="GENERATOR" content="IBM WebSphere Studio">
<TITLE>helloworld.jsp</TITLE>
</HEAD>
<BODY>

<form name="form" method="post" action="/mywork/servlet/com.bcb.mywork.HelloWorld?text=posttext">

<TABLE border="1">
 <TBODY>
  <TR>
   <TD colspan="2"><jet:display name="gettext" />
   </TD>
  </TR>
  <TR>
   <TD>
   <INPUT type="text" name="posttext">
   </TD>
   <TD>
   <INPUT type="submit" name="submit" value="确认">
   </TD>
  </TR>
 </TBODY>
</TABLE>
</form>

</BODY>
</HTML>


在浏览器中输入http://localhost:9080/mywork/servlet/com.bcb.mywork.HelloWorld?text=即可看到结果。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值