简体   繁体   English

将大量数据从JavaScript传递到Java函数

[英]Passing large amount of data from javascript to a java function

I have developed an htmleditor in java.Now i have installed that applet on my website to format data that would be coming from database.My question is that I am calling a java function from javascript,when i pass small amount of text to my java function callPanelToSetText(String data) it sets the jtextpane correctly.However when i pass large amount of text the applet hangs and does not display the text in jtextpane. 我已经在Java中开发了一个htmleditor。现在我已经在我的网站上安装了该applet以格式化来自数据库的数据。我的问题是,当我将少量文本传递给Java时,我正在从javascript调用Java函数。函数callPanelToSetText(String data)可以正确设置jtextpane。但是,当我传递大量文本时,applet会挂起,并且不会在jtextpane中显示文本。

  <head>
    <title>Test page for launching the applet via JNLP</title>
</head>
<body>
    <h3>Test page for launching the applet via JNLP</h3>
    <script src="http://java.com/js/deployJava.js"></script>
    <script>
        var attributes = {

             code:       "researchtexteditor.EditorApplet",
            archive:    "ResearchHTMLEditor.jar, lib/jortho.jar",
            width:      600,
            height:     600,
              id:  'EditorValue'

        };
        var parameters = {jnlp_href:"launch.jnlp"}; <!-- Applet Parameters -->
        var version = "1.7"; <!-- Required Java Version -->
        deployJava.runApplet(attributes, parameters, version);
    </script>
    <!-- Or use the following applet element to launch the applet using jnlp_href -->
    <!--
    <applet width="300" height="300">
        <param name="jnlp_href" value="launch.jnlp"/>
    </applet>
    -->
</body>
  <p><a href="javascript:enterNums();">Launch Example</a></p>
<p><a href="javascript:enterNums_get();">Launch Example1</a></p>
</html>
<script language="javascript">
function enterNums(){
  var content='<?php echo $row['rep_contents'];?>';
   alert(content);
//document.write('Value from Jtextpane 11 '+content);
   EditorValue.callPanelToSetText(content);

 }
    function enterNums_get(){
   var TextVal=EditorValue.getTextData();

   document.write('Value from Jtextpane '+TextVal);
  } 

 <!-- ... -->

\\ \\

   Function callPanelToSetText(String value) from java is as below

    public static void callPanelToSetText(String value)
    {
           try {

                 SimpleAttributeSet attr=new SimpleAttributeSet();
                  StyleConstants.setFontFamily(attr,"Arial");
                        StyleConstants.setFontSize(attr,13);
                          StyleConstants.setForeground(attr,Color.BLACK);
                         StyleConstants.setBold(attr,false); 
                            StyleConstants.setItalic(attr,false); 
   editorPanel1.htmlDoc.insertString(editorPanel1.htmlDoc.getLength(),value,attr);
           } catch (BadLocationException ex) {
               Logger.getLogger(EditorApplet.class.getName()).log(Level.SEVERE, null, ex);
           }

    }

 The text that i wish to set on jtextpane is 

   String val="CHAPTER 1    INTRODUCTION    13\n" +
      "1.1  Report Description  13\n" +
      "1.2  Reason for doing the study  14\n" +
      "1.3  Key Benefits    14\n" +
      "1.4  Key Market Segments 15\n" +
      "1.5  Key Audiences   15\n" +
      "1.6  Research Methodology    15\n" +
      "1.6.1    Secondary research  16\n" +
      "1.6.2    Primary research    16\n" +
      "1.6.3    Analyst tools and models    18\n" +
      "CHAPTER 2    EXECUTIVE SUMMARY   19\n" +
      "2.1  Market beyond: what to expect by 2025   22\n" +
      "2.1.1    Moderate growth scenario    22\n" +
      "2.1.2    Rapid growth scenario   24\n" +
      "2.1.3    Diminishing growth scenario 26\n" +
      "CHAPTER 3    MARKET OVERVIEW 29\n" +
      "3.1  Market Definition and Scope 29\n" +
      "3.2  Key findings    30\n" +
      "3.2.1    Top Factors Impacting transparent conductive films market   30\n" +
      "3.2.1.1  Rising adoption of touch UI devices 30\n" +
      "3.2.1.2  Declining cost of smartphones   30\n" +
      "3.2.1.3  Low power consumption   30\n" +
      "3.2.1.4  Minimal reflection  30\n" +
      "3.2.1.5  Thinness    31\n" +
      "3.2.1.6  Flexibility/robustness  31\n" +
      "3.2.1.7  Lack of one-size-fits-all solution  31\n" +
      "3.2.1.8  The multiplicity of options is giving rise to market uncertainty and confusion  32\n" +
      "3.2.2    Top Investment Pockets  34\n" +
      "3.2.3    Top winning strategies  34\n" +
      "3.3  Porter’s five force analysis    35\n" +
      "3.3.1    Large number of suppliers leads to lower bargaining power of suppliers  36\n" +
      "3.3.2    Lower switching cost leads to high Buyer power  37\n" +
      "3.3.3    Unavailability of substitute lowers the may raise the threat of complete substitution   37\n" +
      "3.3.4    Economies of scale leads to low threat of new entrants  37\n" +
      "3.3.5    Numerous competitors lead to high rivalry   38\n" +
      "3.4  Value chain analysis    38\n" +
      "";

Can anyone please tell me where i am going wrong.Thanks and Regards in advance. 谁能告诉我我要去哪里错了,谢谢和问候。

First, the Java Applets are obsolete. 首先,Java Applets已过时。 Instead of this, you can use a MVC (model view controller) with HTML5+Jquery+Ajax for the view and Java for the controller and the model . 取而代之的是,您可以将MVC(模型视图控制器)与HTML5 + Jquery + Ajax一起用于视图,将JavaJava一起用于控制器和模型。 For me this is the best option. 对我来说,这是最好的选择。 With this approach, using HTML5+Jquery+Ajax, you can retrieve the data from you database using ajax. 通过这种方法,使用HTML5 + Jquery + Ajax,您可以使用ajax从数据库中检索数据。 The performance is improved and you will have not the limitation of the Applets. 性能得到了改善,您将不受Applet的限制。

It's difficult to provide an answer based solely on those code fragments, but I suspect it's not the amount of data which is triggering this (but passing data from javascript to java (and the other way around) through LiveConnect has serious limitations, in the order of megabytes and not what you showed). 仅仅基于这些代码片段很难提供答案,但是我怀疑不是通过LiveConnect触发数据的数据量很大(但是从JavaScript到Java以及其他方法)将数据传递到顺序上却有严重的限制。兆字节,而不是您显示的内容)。

I suggest these test steps: 我建议这些测试步骤:

1) Please create a test method, let's call it testCallPanelToSetText, with identical signature (parameters). 1)请创建一个测试方法,我们将其称为testCallPanelToSetText,具有相同的签名(参数)。

Change the javascript to pass the text to this method instead. 更改javascript以将文本传递给此方法。 On this method's first implementation you'll do nothing. 在此方法的第一个实现上,您将什么也不做。 See if the applet hangs. 查看小程序是否挂起。 If it doesn't hang, you can eliminate that assumption (that the cause of the problem is the amount of data). 如果它没有挂起,则可以消除该假设(问题的原因是数据量)。

2) On the second implementation, you'll test if you have a threading issue. 2)在第二种实现中,您将测试是否存在线程问题。

LiveConnect (JS <-> Java bridge) has some specs on how multiple threads should interact, and swing has an even stricter requirement. LiveConnect(JS <-> Java桥)对多个线程应该如何交互有一些规范,而swing则有更严格的要求。 Please read this: Swing and multiple threads and make sure you don't call any swing affected function from the LiveConnect thread, it'll probably differ from the swing thread. 请阅读以下内容: Swing和多个线程 ,并确保您没有从LiveConnect线程调用任何受Swing影响的函数,它可能与Swing线程有所不同。 Make all methods called from javascript schedule the swing calls so that they are run later, on the swing thread, and not immediately (read the article, it's worthy). 使从javascript调用的所有方法都调度swing调用,以便它们稍后在swing线程上运行,而不是立即运行(阅读文章,这很有价值)。

The fact that it works for you on few bytes has to do with the probabilistic behavior of most multithreading bugs, and you should be aware of that. 它对您有用的事实只有几个字节,这与大多数多线程错误的概率行为有关,您应该意识到这一点。

After you implement that, it should work, if not, you have to keep in mind this is the required design anyway, as long as you have multiple threads in question. 实现该功能后,它应该可以工作,如果没有,则必须记住这是必需的设计,只要您有多个线程。

EDIT: try to rename your callPanelToSetText to doCallPanelToSetText and make callPanelToSetText a wrapper: 编辑:尝试将您的callPanelToSetText重命名为doCallPanelToSetText并使callPanelToSetText成为包装器:

public static void callPanelToSetText(final String value) {
  SwingUtilities.invokeLater(new Runnable() {

  @Override
    public void run() {
      doCallPanelToSetText(value);
    }
  });
}

and then make the JS call callPanelToSetText as before. 然后像以前一样让JS调用callPanelToSetText。 See if that solves the problem. 看看是否能解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM