站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Java 专题Jsp自定义标记入门程序:制作登录界面

Jsp自定义标记入门程序:制作登录界面

摘要: Jsp自定义标记入门程序:制作登录界面

</td> </tr> <tr> <td height="35" valign="top" class="ArticleTeitle"> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="282" height="86" align="center" valign="top"> </td> <td width="402" valign="top">

一、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">




login
/WEB-INF/tlds/decor_0_1.tld


</td></tr> </table>





二、标记库描述符文件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">


0.1
1.1
decor

A simple tag library for decorative HTML output


box
examples.DecorBox
Display a colored box with a border

align
false true

color
false
true


borderColor
false
true


margin
false
true


borderWidth
false
true


title
false
true


titleColor
false
true


titleAlign
false
true



三、登录文件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>登录</head>
<body bgcolor='white'>


<%=title%>


<%=message%>
<form action='login.jsp' method='post'>
<table>
<tr>
<td align='right'>
用户名:
</td>
<td><input name='username'></td>
</tr><tr>
<td align='right'>
口令:
</td>
<td><input type='password' name='password'></td>
</tr><tr>
<td align='center' colspan=2>
<input type=submit value='Login'>
</td>
</tr>
</table>
<input type='hidden' name='nextpage' value='<%=nextPage%>'>
<input type='hidden' name='title' value='<%=title%>'>
</form>


</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 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("

" +
"<table bgcolor='" + borderColor + "' " + "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 + "'>" +
" "color='" + titleColor + "'>" +
title + "
</td></tr>");

// Now begin an inner table that has a different color than
// 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());
}

// 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 tag is encountered
**/
public int doEndTag() throws JspException {
// Try to output HTML to close the <table> and

tags.
// 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()); }

// This return value says to continue processing the JSP page.
return EVAL_PAGE;
}
}


</td> </tr> <tr>


↑返回目录
前一篇: JSP网站登录记忆跳转实现的一种方法
后一篇: 用bean保存表单状态