本文目錄一覽:
JSP Base64
import java.io.*;
import java.net.*;
public final class Codes {
public final static byte[] base64Encode(byte[] byteData) {
if (byteData == null)
return null;
int iSrcIdx; // index into source (byteData)
int iDestIdx; // index into destination (byteDest)
byte byteDest[] = new byte[((byteData.length + 2) / 3) * 4];
for (iSrcIdx = 0, iDestIdx = 0; iSrcIdx byteData.length – 2; iSrcIdx += 3) {
byteDest[iDestIdx++] = (byte) ((byteData[iSrcIdx] 2) 077);
byteDest[iDestIdx++] =
(byte) ((byteData[iSrcIdx + 1] 4) 017 | (byteData[iSrcIdx] 4) 077);
byteDest[iDestIdx++] =
(byte) ((byteData[iSrcIdx + 2] 6)
003
| (byteData[iSrcIdx + 1] 2)
077);
byteDest[iDestIdx++] = (byte) (byteData[iSrcIdx + 2] 077);
}
if (iSrcIdx byteData.length) {
byteDest[iDestIdx++] = (byte) ((byteData[iSrcIdx] 2) 077);
if (iSrcIdx byteData.length – 1) {
byteDest[iDestIdx++] =
(byte) ((byteData[iSrcIdx + 1] 4) 017 | (byteData[iSrcIdx] 4) 077);
byteDest[iDestIdx++] = (byte) ((byteData[iSrcIdx + 1] 2) 077);
} else
byteDest[iDestIdx++] = (byte) ((byteData[iSrcIdx] 4) 077);
}
for (iSrcIdx = 0; iSrcIdx iDestIdx; iSrcIdx++) {
if (byteDest[iSrcIdx] 26)
byteDest[iSrcIdx] = (byte) (byteDest[iSrcIdx] + ‘A’);
else
if (byteDest[iSrcIdx] 52)
byteDest[iSrcIdx] = (byte) (byteDest[iSrcIdx] + ‘a’ – 26);
else
if (byteDest[iSrcIdx] 62)
byteDest[iSrcIdx] = (byte) (byteDest[iSrcIdx] + ‘0’ – 52);
else
if (byteDest[iSrcIdx] 63)
byteDest[iSrcIdx] = (byte) ‘+’;
else
byteDest[iSrcIdx] = (byte) ‘/’;
}
for (; iSrcIdx byteDest.length; iSrcIdx++)
byteDest[iSrcIdx] = (byte) ‘=’;
return byteDest;
}
public final static String base64Encode(String strInput) {
if (strInput == null)
return null;
return base64Encode(strInput,”GB2312″);
}
public final static String base64Encode(String strInput,String charSet) {
if (strInput == null)
return null;
String strOutput=null;
byte byteData[] = new byte[strInput.length()];
try {
//strInput.getBytes(0, strInput.length(), byteData, 0);
byteData = strInput.getBytes(charSet);
strOutput=new String(base64Encode(byteData),charSet);
//strOutput=new String(base64Encode(byteData),0);
} catch (UnsupportedEncodingException e) {
return null;
}
return strOutput;
}
/**
* 此處插入方法說明。
* 創建日期:(2000-11-4 18:27:35)
* @param steam java.io.InputStream
* @param charSet java.lang.String
*/
public final static String base64Encode(InputStream in, String charSet) {
try {
int c;
byte[] buff = new byte[1024];
ByteArrayOutputStream out = new ByteArrayOutputStream(2048);
while ((c = in.read(buff, 0, 1024)) != -1) {
out.write(buff, 0, c);
//index+=1024;
//out.write(c);
//attachContent+=ss;
}
in.close();
out.flush();
byte[] tmp2 = Codes.base64Encode(out.toByteArray());
out.close();
return new String(tmp2,charSet);
}
catch (IOException e) {
return “”;
}
}/**
* 此處插入方法說明。
* 創建日期:(2000-11-3 23:31:04)
* @return java.lang.String
* @param strIn java.lang.String
*/
public final static String chunkSplit(String strIn) {
return chunkSplit(strIn,76);
}/**
* 此處插入方法說明。
* 創建日期:(2000-11-3 23:31:04)
* @return java.lang.String
* @param strIn java.lang.String
*/
public final static String chunkSplit(String strIn,int splitLen) {
int index=0;
String strOut=””;
while(index+splitLenstrIn.length()){
strOut+=strIn.substring(index,index+splitLen)+”\n”;
index+=splitLen;
}
if(indexstrIn.length()){
strOut+=strIn.substring(index);
}
return strOut;
}
}
Tomcat自帶JSP實例,請教
哈哈,我做了一年的JSP,現在轉PHP了。
表單上的每一個元素,對應JavaBean裡面的一個變量,而且這個變量,應該有2個方法get和set,如name,getName()和setName(String name),這個樣子的類被稱為Bean。Bean類不用實例化(應該說JSP巳經幫我們實例化了)
其實這種接收參數的方式一般和框架配合使用,如Strust(現是2.0版本)。
如果是純JSP的話用string name=Request.getParameter(“name”);(我喜歡用這種,省去建Bean了)
jsp中base標籤問題,在火狐、google瀏覽器正常,在IE8下不正常
base標籤在ie8顯示不正確是因為代碼的寫法錯誤,正確的寫法:
base href=”xxxxxx” /跳轉地址
img src=”Images/logoSite.gif” /
jsp中base標籤在瀏覽器的兼容性:
瀏覽器支持如下:
定義和用法
base 標籤為頁面上的所有鏈接規定默認地址或默認目標。
通常情況下,瀏覽器會從當前文檔的 URL 中提取相應的元素來填寫相對 URL 中的空白。
使用 base 標籤可以改變這一點。瀏覽器隨後將不再使用當前文檔的 URL,而使用指定的基本 URL 來解析所有的相對 URL。這其中包括 a、img、link、form 標籤中的 URL。
提示和注釋:
注釋:base 標籤必須位於 head 元素內部。
JSP頁面文件中base標記用法實例分析
本文實例分析了JSP頁面文件中base標記用法。分享給大家供大家參考,具體如下:
我們在用IDE工具生成JSP頁面時通常都包含下面的兩段代碼,
%
String
path
=
request.getContextPath();
String
basePath
=
request.getScheme()+”://”+request.getServerName()+”:”+request.getServerPort()+path+”/”;
%
head
base
href=”%=basePath%”
/head
它們絕對不是無用代碼,詳細如下:
base標記是一個基鏈接標記,是一個單標記。用以改變文件中所有連結標記的參數內定值。它只能應用於標記head與/head之間。你網頁上的所有相對路徑在鏈接時都將在前面加上基鏈接指向的地址。
重要屬性:
href—設定前綴的鏈接地址
target—設定文件顯示的窗口,同a標記中的target
簡單例子:
html
head
base
href=
target=”_blank”
meta
http-equiv=”Content-Type”
content=”text/html;
charset=gb2312″
titlebase標記/title
link
rel=”Shortcut
Icon”
href=”ani.CUR”
/head
body
a
href=”x.htm”
target=”_self”x.html/a
a
href=”y.htm”y.html/a
/body
/html
當點了鏈接後,跳出的文件是或,它就是在這些相對路徑的文件前加上基鏈接指向的地址。如果目標文件中的鏈接沒有指定target屬性,就用base標記中的target屬性。
常在框架結構中用,如左右兩個框架,把左邊的框架中文件里的連接都顯示在右邊的框架里。只要用base標記,把其target屬性值寫為右框架名稱,這就不用再為左框架里的文件中的每一個連接都指定target屬性。
當使用時,BASE
元素必須出現在文檔的
HEAD
內,在任何對外部源的引用之前。
另外,如果頁面轉向某個Servlet,而Servlet里又是forward到的某個jsp頁面,如果這時寫相對路徑就應該先找到Servlet的路徑,也就是web.xml中配置的url-pattern中的路徑,如:假設有個x.jsp放在webapplication根目錄下,而主頁index.jsp是提交到servlet上去的,由Serlet來分發forward到x.jsp,Servlet的url配置如下:
複製代碼
代碼如下:url-pattern/servlet/TestServlet/url-pattern
那麼Servlet完成forward轉向後,如果沒有base
href=”%=basePath%”
x.jsp中script
type=”text/javascript”
src=”script/check.js”/script就會失效,因為Servlet的訪問路徑為那麼web服務器會到下去找check.js此時這裡肯定是沒有這個文件的,所以,如果遇到這樣的情況建議使用絕對路徑就不會有錯複製代碼
代碼如下:script
type=”text/javascript”
src=”%=path%/script/check.js”/script
希望本文所述對大家JSP程序設計有所幫助。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/233536.html