Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 307|回复: 0

[jsp学习]Jsp自定义标记入门程序:制作登录界面

[复制链接]
  • TA的每日心情
    开心
    2021-3-12 23:18
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2014-10-1 18:23:11 | 显示全部楼层 |阅读模式
    一、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
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-5-22 23:46 , Processed in 0.390899 second(s), 48 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表