(JSP)在文本域中显示超链接——如何在Servlet中使用服务器端代码

        首先先介绍一下HTML的文本解析代码:

 

package  project.util;

/**
 * This class is used for parsing the string that contains
 * HTML elements, especially hyperlinks.</br>
 * It contains a developer-defined syntax to allow the user
 * to make a customized hyperlink.
 * 
 * 
@author Zenny Chen
 *
 
*/


public   class  HTMLParser  {
    
    
// HTML elements for output
    private static final String begin_enter = "<br>";
    
private static final String end_enter = "</br>";
    
private static final String begin_hyperlink = "<a href=";
    
private static final String end_href = ">";
    
private static final String end_hyperlink = "</a>";
    
    
// some interpunctions
    private static final char char_enter = ' ';
    
private static final char quotation_mark = '"';
    
    
// hyperlink types to parse
    private static final String notes_link = "notes://";
    
private static final String http_link = "http://";
    
    
// customized format
    private static final String link_ref = "@link=";
    
private static final String link_name = "@name=";
    
    
private int cursor = 0;
    
private int content_length = 0;
    
private char[] src = null;
    
private char[] dest = null;
    
private final int mark_length = 5;
    
/**
     * [0] stores href value initial position</br>
     * [1] stores href value eventual position</br>
     * [2] stores link name initial position</br>
     * [3] stores link name eventual position</br>
     * [4] stores next continuing position
     
*/

    
private int[] position_marks = new int[mark_length];
    
    
public HTMLParser() {
        
    }

    
    
/**
     * It is the only method that user will use in this class.
     * 
@param ref the raw string that user inputs
     * 
@return a hyperlink formatted string
     
*/

    
public String getHTMLFormString(String ref) {
        src 
= ref.toCharArray();
        content_length 
= ref.length();
        
final int length = (content_length << 2+ 128;
        dest 
= new char[length];
        
        
boolean has_enter_mark = false;
        
        
for(int i=0; i<content_length;) {
            
if(isHyperlink(notes_link, i) || isHyperlink(http_link, i)) {
                writeString(begin_hyperlink.toCharArray(), begin_hyperlink.length());
                dest[cursor
++= quotation_mark;
                
int len = getLengthTillSpace(i);
                
int k = i;
                
for(int j=0; j<len; j++)
                    dest[cursor
++= src[k++];
                dest[cursor
++= quotation_mark;
                writeString(end_href.toCharArray(), end_href.length());
                
for(int j=0; j<len; j++)
                    dest[cursor
++= src[i++];
                writeString(end_hyperlink.toCharArray(), end_hyperlink.length());
            }

            
else if(isConfiguredHyperlink(i)) {
                writeString(begin_hyperlink.toCharArray(), begin_hyperlink.length());
                dest[cursor
++= quotation_mark;
                
                
for(int j=position_marks[0]; j<=position_marks[1]; j++)
                    dest[cursor
++= src[j];
                
                dest[cursor
++= quotation_mark;
                writeString(end_href.toCharArray(), end_href.length());
                
                
for(int j=position_marks[2]; j<=position_marks[3]; j++)
                    dest[cursor
++= src[j];
                
                writeString(end_hyperlink.toCharArray(), end_hyperlink.length());
                i 
= position_marks[4];
                
continue;
            }

            
else if(src[i] == char_enter) {
                i
++;
                
if(has_enter_mark)
                    writeString(end_enter.toCharArray(), end_enter.length());
                has_enter_mark 
= true;
                writeString(begin_enter.toCharArray(), begin_enter.length());
            }

            
else
                dest[cursor
++= src[i++];
        }

        
        
if(has_enter_mark)
            writeString(end_enter.toCharArray(), end_enter.length());
        
        String r 
= new String(dest, 0, cursor);
        
return r;
    }

    
    
/**
     * Check if the string is a specified hyperlink defined in this class.
     * 
@param link the string that'll be checked
     * 
@param initpos the initial position of the string to check
     * 
@return a boolean value
     
*/

    
private boolean isHyperlink(final String link, int initpos) {
        
char[] ch = link.toCharArray();
        
char[] cap = link.toUpperCase().toCharArray();
        
        
final int len = link.length();
        
int i = initpos;
        
        
for(int j=0; j<len; j++, i++{
            
if(i == content_length)
                
return false;
            
if(src[i] != ch[j] && src[i] != cap[j])
                
return false;
        }

        
        
return true;
    }

    
    
/**
     * Determine if the string(&src[initpos])is a customized hyperlink
     * format.</br>
     * The form is: "{
@link=xxx @name=xxx}"
     * 
@param initpos
     * 
@return a boolean value
     
*/

    
private boolean isConfiguredHyperlink(int initpos) {
        
int i = initpos;
        
boolean phase_ok = false;
        
        
// phase 1
        if(src[i++== '{'{
            
// phase 2
            int len = content_length - i;
            
for(int j=0; j<len; j++, i++{
                
if(src[i] == ' ')
                    
continue;
                
else if(src[i] == '@'{
                    phase_ok 
= true;
                    
break;
                }

                
else
                    
return false;
            }

            
// phase 3
            if(!phase_ok)
                
return false;
            phase_ok 
= false;
            
if(isHyperlink(link_ref, i)) {
                i 
+= link_ref.length();
                position_marks[
0= i;
            }

            
else
                
return false;
            len 
= content_length - i;
            
for(int j=0; j<len; j++, i++{
                
if(src[i] == ' '{
                    position_marks[
1= i - 1;
                    phase_ok 
= true;
                    
break;
                }

                
else if(src[i] == char_enter)
                    
return false;
            }

            
if(!phase_ok)
                
return false;
            
while(src[++i] == ' ');
            
            
// phase 4
            if(src[i] != '@')
                
return false;
            
if(!isHyperlink(link_name, i))
                
return false;
            i 
+= link_name.length();
            position_marks[
2= i;
            len 
= content_length - i;
            
for(int j=0; j<len; j++, i++{
                
if(src[i] == '}'{
                    position_marks[
3= i - 1;
                    position_marks[
4= i + 1;
                    
return true;
                }

                
if(src[i] == char_enter)
                    
return false;
            }

        }

        
return false;
    }

    
    
/**
     * check if a character is a space symbol
     * 
@param c a character that'll be judged
     * 
@return a boolean value
     
*/

    
private boolean isSpace(char c) {
        
return c == ' ' || c == char_enter || c == ' ';
    }

    
    
/**
     * write the string to dest[] buffer
     * 
@param ch the source
     * 
@param size the length of the string to be output to the destination
     
*/

    
private void writeString(char[] ch, int size) {
        
for(int i=0; i<size; i++{
            dest[cursor
++= ch[i];
        }

    }

    
    
/**
     * Get the length of the string from initpos to a space symbol 
     * 
@param initpos the initial position of the src[] CharArray
     * 
@return the length
     
*/

    
private int getLengthTillSpace(int initpos) {
        
int i;
        
for(i=initpos; i<content_length; i++{
            
if(isSpace(src[i]))
                
break;
        }

        
return i - initpos;
    }

}

 

        然后在你需要启动服务器端的地方插入下面代码:

 

            HTMLParser parser  =   new  HTMLParser();
            intro_contents 
=  parser.getHTMLFormString(yourstring);
            serverFactory 
=   new  ServerFactory(intro_contents);
            serverFactory.beginCommunication(
true );

        如果是想启动两个服务器线程以至于可以在下一个页面中让两个applet可以同时运行,则可以这么做:

 

           HTMLParser parser  =   new  HTMLParser();
            String value 
=  parser.getHTMLFormString(yourstring);
            serverFactory 
=   new  ServerFactory(value);
            serverFactory.beginCommunication(
true );

            String value2 
=  parser.getHTMLFormString(yourstring2);
            ServerFactory serverFactory2 
=   new  ServerFactory(value2);
            serverFactory2.beginCommunication(
false );

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值