一、web.xml文件
<?xml version="1.0" encoding="ISO-8859-1"?>
web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2.2.dtd">
二、标记库描述符文件decor_0_1.tld
<?xml version="1.0" encoding="ISO-8859-1" ?>
taglib
PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"
"http://java.sun.com/j2ee/dtds/web-jsptaglib_1_1.dtd">
A simple tag library for decorative HTML output
三、登录文件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>
四、标记处理程序:
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";
}
/** // If there is a title, display it as a cell of the outer table // Now begin an inner table that has a different color than // This return value tells the JSP class to process the body of the tag /** // This return value says to continue processing the JSP page.
* This method is called when a
* attributes will first be processed by calling the setter methods above.
**/
public int doStartTag() throws JspException {
try {
JspWriter out = pageContext.getOut();
out.print("
"<table bgcolor='" + borderColor + "' " +
"cellpadding='" + borderWidth + "'>");
if (title != null)
out.print("<tr><td align='" + titleAlign + "'>" +
"
"color='" + titleColor + "'>" +
title + "</td></tr>");
// the border.
out.print("<tr><td><table bgcolor='" + color + "' " +
"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());
}
return EVAL_BODY_INCLUDE;
}
* This method is called when the closing tag is encountered
**/
public int doEndTag() throws JspException {
// Try to output HTML to close the <table> and
// Catch IOExceptions and rethrow them as JspExceptions
try {
JspWriter out = pageContext.getOut();
out.println("</td></tr></table></td></tr></table>
}
catch (IOException e) { throw new JspException(e.getMessage()); }
return EVAL_PAGE;
}
}
↑返回目录
前一篇: JSP网站登录记忆跳转实现的一种方法
后一篇: 用bean保存表单状态