『壹』 jsp調用js的變數
1、首先在jsp頁面上,定義二個變數。
『貳』 jsp頁面如何導成pdf格式
先在伺服器上生成PDF文件,然後用戶通過點擊指向PDF文件的超鏈接選擇下載或打開。這是一個思路,或者說是思路之一。本文實現了這個思路,又給出另外一個思路並通過兩種途徑實現之。
1)直接在伺服器上生成PDF文件。
<%@ page import ="com.lowagie.text.*
,com.lowagie.text.pdf.*, java.io.*"%>
<%
String filename =
"PDF"+(new Random()).nextInt()+".pdf" ;
Document document =
new Document(PageSize.A4);
ServletOutputStream out1
= response.getOutputStream();
try{
PdfWriter writer =
PdfWriter.getInstance(document,
new FileOutputStream(filename) );
document.open();
document.add(new Paragraph("Hello World"));
document.close();
}
catch(Exception e){}
%>
上面的程序在伺服器上生成了一個靜態的PDF文件。顯然,每次運行所得的PDF文件的名稱應該是獨一無二不能有重的。本程序通過隨機函數來命名生成的PDF文件。本程序的缺點就是,每次運行都會在伺服器上產生一個PDF文件,如果不及時刪除,數量會越來越大,這顯然是站點維護者所不願意看到的。
2)將PDF文件通過流的形式輸送到客戶端的緩存。這樣做的好處是不會在伺服器上留下任何「遺跡」。
i)直接通過JSP頁面生成
<%@
page import="java.io.*,
java.awt.Color,com.lowagie.text.*,
com.lowagie.text.pdf.*"%>
<%
response.setContentType
( "application/pdf" );
Document document = new Document();
ByteArrayOutputStream buffer
= new ByteArrayOutputStream();
PdfWriter writer=
PdfWriter.getInstance( document, buffer );
document.open();
document.add(new Paragraph("Hello World"));
document.close();
DataOutput output =
new DataOutputStream
( response.getOutputStream() );
byte[] bytes = buffer.toByteArray();
response.setContentLength(bytes.length);
for( int i = 0;
i < bytes.length;
i++ )
{
output.writeByte( bytes[i] );
}
%>
ii)通過Servlet生成
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.lowagie.text.*;
import com.lowagie.text.pdf.*;
public void doGet
(HttpServletRequest request,
HttpServletResponse response)
throws IOException,ServletException
{
Document document =
new Document(PageSize.A4, 36,36,36,36);
ByteArrayOutputStream ba
= new ByteArrayOutputStream();
try
{
PdfWriter writer =
PdfWriter.getInstance(document, ba);
document.open();
document.add(new
Paragraph("Hello World"));
}
catch(DocumentException de)
{
de.printStackTrace();
System.err.println
("A Document error:" +de.getMessage());
}
document.close();
response.setContentType
("application/pdf");
response.setContentLength(ba.size());
ServletOutputStream out
= response.getOutputStream();
ba.writeTo(out);
out.flush();
}
『叄』 jsp中的document對象有哪些方法,具體說明
document對象詳解
document 文擋對象 - JavaScript腳本語言描述
---------------------------------------------------------------------
注:頁面上元素name屬性和JavaScript引用的名稱必須一致包括大小寫
否則會提示你一個錯誤信息 "引用的元素為空或者不是對象"
---------------------------------------------------------------------
對象屬性
document.title //設置文檔標題等價於HTML的<title>標簽
document.bgColor //設置頁面背景色
document.fgColor //設置前景色(文本顏色)
document.linkColor //未點擊過的鏈接顏色
document.alinkColor //激活鏈接(焦點在此鏈接上)的顏色
document.vlinkColor //已點擊過的鏈接顏色
document.URL //設置URL屬性從而在同一窗口打開另一網頁
document.fileCreatedDate //文件建立日期,只讀屬性
document.fileModifiedDate //文件修改日期,只讀屬性
document.fileSize //文件大小,只讀屬性
document.cookie //設置和讀出cookie
document.charset //設置字元集 簡體中文:gb2312
---------------------------------------------------------------------
對象方法
document.write() //動態向頁面寫入內容
document.createElement(Tag) //創建一個html標簽對象
document.getElementById(ID) //獲得指定ID值的對象
document.getElementsByName(Name) //獲得指定Name值的對象
---------------------------------------------------------------------
images集合(頁面中的圖象)
a)通過集合引用
document.images //對應頁面上的<img>標簽
document.images.length //對應頁面上<img>標簽的個數
document.images[0] //第1個<img>標簽
document.images[i] //第i-1個<img>標簽
b)通過nane屬性直接引用
<img name="oImage">
document.images.oImage //document.images.name屬性
c)引用圖片的src屬性
document.images.oImage.src //document.images.name屬性.src
d)創建一個圖象
var oImage
oImage = new Image()
document.images.oImage.src="/1.jpg"
同時在頁面上建立一個<img>標簽與之對應就可以顯示
『肆』 在js中可以引用jsp中的數據嗎
你好,
通常來說是可以的。我給你列舉兩種方法,你根據你的情況選擇適合你的吧。
方法一:將jsp中的變數輸出到html中的type為hidden的input中,js從input中去取值
<!--jsp頁面中的input元素-->
<inputtype="hidden"value="<%=serverData%>"id="J_ServerData">
<!--
當然,掛在某一個DOM元素的屬性上也是可以的
<divid="J_DataDiv"data-server-data="<%=serverData%>">頁面中的任意DOM節點</div>
-->
<script>
//JS中讀取jsp頁面中輸出到頁面中的值
varinp=document.getElementById('J_ServerData');
//如果是div
//varnode=document.getElementById('J_DataDiv');
//取值
varserverData=inp.value;
//如果是屬性
//varserverData=node.getAttribute('data-server-data');
console.log(serverData);
</script>
方法二:JS可以通過AJAX與服務端通信,只需服務端封裝一個JSP介面,前端即可取到服務端數據,以jQuery為例
$.ajax({
url:'server_addr/api.jsp',
type:'get',
success:function(data){
//data就是服務端(jsp)返回的數據
console.log(data);
}
});
大致就這么多,希望能解決你的疑惑,如有疑問可追問~