第一步:建立一个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>Preferences>Java>Code Generation>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>Preferences>Java>Code Generation>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=即可看到结果。