在JSP文件中,如何在javascript中调用Scriptlet代码

时间:2012-03-03 18:41:18

标签: javascript call scriptlet

我有一个静态的scriptlet代码可以正常工作。我想在用户点击html页面中的按钮时调用此静态代码。那么是否可以在javascript中创建一个函数,当用户单击html按钮并在函数内部放置scriptlet代码时调用该函数?

请提供如何实现此目的的确切代码

我的静态scriptlet代码如下:

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%@ page import="java.io.*" %>
<%@ page import="com.lowagie.text.Document" %>
<%@ page import="com.lowagie.text.Element" %>
<%@ page import="com.lowagie.text.html.simpleparser.HTMLWorker" %>
<%@ page import="com.lowagie.text.html.simpleparser.StyleSheet" %>
<%@ page import="com.lowagie.text.pdf.PdfWriter" %>
<%@ page import="com.lowagie.text.pdf.codec.Base64" %>
<%@ page import="java.util.ArrayList" %>
<%
    // This scriptlet declares and initializes "date"
    Document pdfDocument = new Document();
        Reader htmlreader = new BufferedReader(new InputStreamReader(
                                 new FileInputStream("E:\\test.html")
                                ));


        htmlreader.read();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PdfWriter.getInstance(pdfDocument, baos);
        pdfDocument.open();

        StyleSheet styles = new StyleSheet();
        styles.loadTagStyle("body", "font", "Bitstream Vera Sans");
        ArrayList arrayElementList = HTMLWorker.parseToList(htmlreader, styles);
        for (int i = 0; i < arrayElementList.size(); ++i) {
            Element e = (Element) arrayElementList.get(i);
            pdfDocument.add(e);
        }
        pdfDocument.close();



        byte[] bs = baos.toByteArray();
        String pdfBase64 = Base64.encodeBytes(bs); //output
        File pdfFile = new File("E:\\test.pdf");
        FileOutputStream output = new FileOutputStream(pdfFile);
        output.write(bs);
        output.close();
    System.out.println( "Evaluating date now" );
    java.util.Date date = new java.util.Date();
%>
Hello!  The time is now
<%
    // This scriptlet generates HTML output
    out.println( String.valueOf( date ));
%>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

is it possible to create a function in javascript which is called when the 
user clicks the html button

答案:是的

and inside the function the scriptlet code be placed?

答案:否

解决方案是使用Javascript ajax。如果您使用jquery,请使用jquery ajax