Java到JSP - 如何将Java应用程序集成到JSP网页中? [英] Java to JSP - How do I integrate a Java application into a JSP web page?

查看:113
本文介绍了Java到JSP - 如何将Java应用程序集成到JSP网页中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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.

这是我的申请:

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?

谢谢!!!

推荐答案

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

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

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)你必须编译java文件并生成 .class 。您可以使用命令 javac 来完成此操作。否则,您可以查看eclipse工作区的文件夹,在项目文件夹中,您将找到从eclipse生成的 .class

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.

3)将此文件放在名为 WEB_INF\classes\example 的文件夹中,该文件夹保留在tomcat文档文件夹的根目录中。 (例子是包的名称)

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

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

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

<!-- 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>

问候,
卢卡

这篇关于Java到JSP - 如何将Java应用程序集成到JSP网页中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆