Java to JSP - 如何将 Java 应用程序集成到 JSP 网页中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6892798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 17:47:22  来源:igfitidea点击:

Java to JSP - How do I integrate a Java application into a JSP web page?

javajsp

提问by Evik James

Okay, this is got to be the easiest question of the day. This is my first stab at Java and JSP.

好的,这一定是今天最简单的问题。这是我第一次尝试 Java 和 JSP。

I just wrote a little Java application using using Eclipse. Now I want to serve up this little application into a web page. I need to figure out the connection between Java applications and web pages.

我刚刚使用 Eclipse 编写了一个小 Java 应用程序。现在我想将这个小应用程序提供给一个网页。我需要弄清楚 Java 应用程序和网页之间的联系。

Here's my application:

这是我的应用程序:

public class PhraseOMatic {

    public static void main(String[] args) {

        // CREATE WORD ARRAYS
    String[] wordListOne = {"24/7", "Multi-tier", "30,000 foot", "B-to-B", "win-win", "front-end", "back-end", "web-based"};
    String[] wordListTwo = {"empowered", "sticky", "concentric", "distributed", "leveraged", "shared", "accelerated", "aligned"};
    String[] wordListThree = {"process", "tipping point", "mindshare", "mission", "space", "paradigm", "portal", "vision"};

    // CALCULATE ARRAY LENGTHS
    int oneLength = wordListOne.length;
    int twoLength = wordListTwo.length;
    int threeLength = wordListThree.length;


    // PRINT OUT THE PHRASE
    int i = 1;
    while (i < 10) {
        // GENERATE RANDOM NUMBERS
        int rand1 = (int) (Math.random() * oneLength);
        int rand2 = (int) (Math.random() * twoLength);
        int rand3 = (int) (Math.random() * threeLength);

        // BUILD A PHRASE
        String phrase = wordListOne[rand1] + " " + wordListTwo[rand2] + " " + wordListThree[rand3];

        // PRINT OUT PHRASE
        System.out.println("What we need is a " + phrase + ".");
        i = i + 1;

    }   

}

}

}

How to I get the compiled application to render as a web page? What steps do I need to take next?

如何让编译的应用程序呈现为网页?我接下来需要采取哪些步骤?

Thanks!!!

谢谢!!!

回答by Maverik

1) You have to define a class with some methods that returns some results. For example

1)您必须定义一个具有一些返回一些结果的方法的类。例如

package example;
public class WordLength {

private String word="";
public int length=0;

public WordLength(){}

public void setWord(String w){
    word = w;
    length = word.length();
}

public String getWord(){
    return word;
}

public int getLength(){
    return length;
}

}

2) You have to compile the java file and generate a .class. You can do it by using the command javac. Otherwise you can look in the folders of you eclipse workspace, and in the folder of the project you will find the .classgenerated from eclipse.

2) 您必须编译 java 文件并生成一个.class. 您可以使用命令来完成javac。否则,您可以查看 eclipse 工作区的文件夹,并在项目的文件夹中找到.classeclipse 生成的。

3)Put this file in a folder called WEB_INF\classes\examplethat stays in the root of your tomcat documents folder. (example is the name of the package)

3)将此文件放在一个名为的文件夹WEB_INF\classes\example中,该文件夹位于您的 tomcat 文档文件夹的根目录中。(示例是包的名称)

4)In your jsp file import the java class and use it:

4)在你的jsp文件中导入java类并使用它:

<!-- wordLegth.jsp -->

<%@ page language="java" import="java.util.*" %>

<html>
  <head>
    <title>Word length</title>
  </head>
  <body>

    <jsp:useBean id="counter" scope="session" class="example.WordLength"/>

    <% 
      String w1= request.getParameter("p1"); 
      int l1 = 0;
      counter.setWord(w1);
      l1 = counter.getLength();
    %>

    <p> The word <%= w1 %> has <%= l1 %> characters.</p>

  </body>
</html>

This example is automatically invoked by a form that ask the user to insert a word:

此示例由要求用户插入单词的表单自动调用:

<!-- form.html -->
<html>
  <head>
    <title>Form</title>
  </head>
  <body>

    <form action="wordLegth.jsp">

      <p> Word 1: <input name="p1"></p>
      </p>
      <input type="submit" value="Count">
    </form>
  </body>
</html>

Regards, Luca

问候, 卢卡

回答by Pindatjuh

You have to return a string, in this case a nice phrase. To do this, do not use System.out.println, as it sends the string to the standard output. We want the string on a webpage, so this is not good to have.

您必须返回一个字符串,在这种情况下是一个不错的短语。为此,请勿使用System.out.println, 因为它将字符串发送到标准输出。我们想要网页上的字符串,所以这不好。

Use a StringBuilderto collect your phrases. Return it as an array of strings, String[].

使用 aStringBuilder收集您的短语。将其作为字符串数组返回,String[]

As the previous paragraph suggests, we need to get rid of the main-method: I think this staticmethod will suffice: PhraseOMatic.generatePhrases().

正如前一段所暗示的,我们需要去掉main-method:我认为这个static方法就足够了:PhraseOMatic.generatePhrases()

Then, create a JSP page, see this tutorial.

然后,创建一个 JSP 页面,参见本教程

<%
     for(String phrase : PhraseOMatic.generatePhrases()) {
         out.println(phrase);
         out.println("<br/>");
     }
%>