首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

JSP 開發常用工具方法集:空值處理、字符串替換、HTML 編解碼、JS 彈窗、Shift_JIS 長度計算與日期格式化

     編輯:Java 相關知識

核心工具方法

1. 獲取客戶端主機名/IP

String getMyHostname(HttpServletRequest request) {
    try {
        String host = request.getRemoteHost().toUpperCase();
        return host;
    } catch (Exception e) { return null; }
}

2. 空值安全處理

String dealNull(String str) { return str == null ? "" : str; }
Object dealNull(Object obj) { return obj == null ? "" : obj; }
int dealEmpty(String s) { s = dealNull(s); return s.equals("") ? 0 : Integer.parseInt(s); }

3. 字符串替換

String replace(String str, String substr, String restr) {
    String[] parts = str.split(substr);
    if (parts.length == 0) return "";
    StringBuilder sb = new StringBuilder(parts[0]);
    for (int i = 1; i < parts.length; i++) {
        sb.append(restr).append(parts[i]);
    }
    return sb.toString();
}

4. HTML 編碼/解碼

String htmlEncode(String txt) {
    txt = replace(txt, "&", "&");
    txt = replace(txt, "\"", """);
    txt = replace(txt, "<", "<");
    txt = replace(txt, ">", ">");
    txt = replace(txt, " ", " ");
    return txt;
}
String unHtmlEncode(String txt) {
    txt = replace(txt, "&", "&");
    txt = replace(txt, """, "\"");
    txt = replace(txt, "<", "<");
    txt = replace(txt, ">", ">");
    txt = replace(txt, " ", " ");
    return txt;
}

5. 前端彈窗輸出

void sAlert(String title, String msg, String focus, JspWriter out) {
    out.println("");
}
void sConfirm(String title, String msg, String location, JspWriter out) {
    out.println("");
}

6. Shift_JIS 字節長度計算

int getStringLength(String s) {
    try { return s.getBytes("Shift_JIS").length; }
    catch (UnsupportedEncodingException e) { return 0; }
}

7. 定寬字符串截取輸出 (用於表格對齊)

void formatString(String s, int width, JspWriter out) {
    width--;
    int len = getStringLength(s);
    if (len > width) {
        int cut = 0;
        for (int i = 1; i <= width; i++) {
            if (s.substring(0, i).getBytes("Shift_JIS").length <= width) cut = i;
            else break;
        }
        for (int i = 0; i < cut; i++) {
            String ch = s.substring(i, i+1);
            out.print(ch.equals(" ") ? " " : htmlEncode(ch));
        }
        out.print(s.substring(0, cut).getBytes("Shift_JIS").length == width ? "|" : " |");
    } else {
        for (int i = 0; i < width - len; i++) out.print(" ");
        for (int i = 0; i < s.length(); i++) {
            String ch = s.substring(i, i+1);
            out.print(ch.equals(" ") ? " " : htmlEncode(ch));
        }
        out.print(htmlEncode(" |"));
    }
}

8. 數字/日期校驗與格式化

boolean isNumeric(String s) {
    try { Integer.parseInt(s); return true; }
    catch (NumberFormatException e) { return false; }
}

String mkDate(String dateStr, int mode) {
    // mode 0: YYMMDD -> YY/MM/DD
    // mode 1: YYMMDD 補零 -> 19/20YY/MM/DD
    // mode 2: YYYY/MM/DD -> YYYYMMDD
    // mode 3: YYMMDD 判斷世紀 -> CCYYMMDD
    // mode 4: YYYYMMDD -> YYYY/MM/DD
    // mode 5: YYYY/MM -> YYYYMM
    // 具體實現略...
    return formatted;
}

這些方法覆蓋了 JSP/Servlet 開發中 80% 的字符串處理、前端交互與數據格式化需求,可直接封裝為工具類復用。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved