TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
一、web.xml文件
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2.2.dtd">
<web-app>
<taglib>
<taglib-uri>login</taglib-uri>
<taglib-location>/WEB-INF/tlds/decor_0_1.tld</taglib-location>
</taglib>
</web-app>

二、标记库描述符文件decor_0_1.tld
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"
"http://java.sun.com/j2ee/dtds/web-jsptaglib_1_1.dtd"> <!-- The tags above say that this is an XML document, and formally -->
<!-- specify the document type. --> <taglib>
<tlibversion>0.1</tlibversion>
<jspversion>1.1</jspversion>
<shortname>decor</shortname>
<info>
A simple tag library for decorative HTML output
</info> <!-- The <tag> tag defines a single tag of the tag library -->
<tag>
<name>box</name>
<tagclass>examples.DecorBox</tagclass>
<info>Display a colored box with a border</info>
<attribute>
<name>align</name>
<required>false</required> <!-- It is not required -->
<rtexprvalue>true</rtexprvalue> <!-- It may have a <%= %> value -->
</attribute>
<attribute>
<name>color</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>borderColor</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>margin</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>borderWidth</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>title</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>titleColor</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>titleAlign</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
三、登录文件login.jsp <%@ page contentType="text/html; charset=GBK" %>
<%@ taglib uri="login" prefix="decor" %> <%! public boolean verify(String username, String password) {
return ((username!=null) && (password!=null) && password.equals("java"));
} %> <%
String nextPage = request.getParameter("nextpage");
if (nextPage == null) nextPage = "index.jsp"; String title = request.getParameter("title");
if (title == null) title = "Please Login"; String username = request.getParameter("username");
String password = request.getParameter("password"); if ((username != null) && (password != null) && verify(username, password)) {
session.setAttribute("username", username);
response.sendRedirect(nextPage);
}
else {
String message = "";
if ((username != null) || (password != null)) {
message = "无效的用户名或口令,请重新输入";
}
%>
<head><title>登录</title></head>
<body bgcolor="white">
<decor:box color="yellow" margin="25" borderWidth="3" title="登录">
<div align=center>
<font face="helvetica"><h1><%=title%></h1></font>
<font face="helvetica" color="red"><b><%=message%></b></font>
<form action="login.jsp" method="post">
<table>
<tr>
<td align="right">
<b><font face="helvetica">用户名:</font></b>
</td>
<td><input name="username"></td>
</tr><tr>
<td align="right">
<b><font face="helvetica">口令:</font></b>
</td>
<td><input type="password" name="password"></td>
</tr><tr>
<td align="center" colspan=2><font face="helvetica"><b>
<input type=submit value="Login">
</b></font></td>
</tr>
</table>
<input type="hidden" name="nextpage" value="<%=nextPage%>">
<input type="hidden" name="title" value="<%=title%>">
</form>
</div>
</decor:box>
</body>
<%
}
%>
四、标记处理程序:
package examples;
import javax.Servlet.jsp.*; // JSP classes
import javax.servlet.jsp.tagext.*; // Tag Library classes
import java.io.IOException;
public class DecorBox extends TagSupport {
String align; // Alignment of the box
String title; // Title for the box
String titleColor; // Title foreground color
String titleAlign; // Title alignment relative to box
String color; // Box background color
String borderColor; // Border (and title background) color
String margin; // Pixels between box edge and content
String borderWidth; // Pixel width of the box border
public void setAlign(String value) { align = value; }
public void setTitle(String value) { title = value; }
public void setTitleColor(String value) { titleColor = value; }
public void setTitleAlign(String value) { titleAlign = value; }
public void setColor(String value) { this.color = value; }
public void setBorderColor(String value) { borderColor = value; }
public void setMargin(String value) { margin = value; }
public void setBorderWidth(String value) { borderWidth = value; }
public void setPageContext(PageContext context) {
super.setPageContext(context); align = "center";
title = null;
titleColor = "white";
titleAlign = "left";
color = "lightblue";
borderColor = "black";
margin = "20";
borderWidth = "4";
} /**
* This method is called when a <decor:box> tag is encountered. Any
* attributes will first be processed by calling the setter methods above.
**/
public int doStartTag() throws JspException {
try {
JspWriter out = pageContext.getOut();
out.print("<div align="" + align + "">" +
"<table bgcolor="" + borderColor + "" " +
"border="0" cellspacing="0" " +
"cellpadding="" + borderWidth + "">"); // If there is a title, display it as a cell of the outer table
if (title != null)
out.print("<tr><td align="" + titleAlign + "">" +
"<font face="helvetica" size="+1" " +
"color="" + titleColor + ""><b>" +
title + "</b></font></td></tr>"); // Now begin an inner table that has a different color than
// the border.
out.print("<tr><td><table bgcolor="" + color + "" " +
"border="0" cellspacing="0" " +
"cellpadding="" + margin + ""><tr><td>");
}
catch (IOException e) {
// Unlike a PrintWriter, a JspWriter can throw IOExceptions
// We have to catch them and wrap them in a JSPException
throw new JspException(e.getMessage());
} // This return value tells the JSP class to process the body of the tag
return EVAL_BODY_INCLUDE;
} /**
* This method is called when the closing </decor:box> tag is encountered
**/
public int doEndTag() throws JspException {
// Try to output HTML to close the <table> and <div> tags.
// Catch IOExceptions and rethrow them as JspExceptions
try {
JspWriter out = pageContext.getOut();
out.println("</td></tr></table></td></tr></table></div>");
}
catch (IOException e) { throw new JspException(e.getMessage()); } // This return value says to continue processing the JSP page.
return EVAL_PAGE;
}
}
源码下载:http://file.javaxxz.com/2014/10/1/182310672.zip |
|