好的,这肯定是当今最简单的问题。这是我第一次接触Java和JSP。
我只是使用Eclipse编写了一个小的Java应用程序。现在,我想将此小应用程序提供到网页中。我需要弄清楚Java应用程序和网页之间的联系。
这是我的应用程序:
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; } }
}
如何获取已编译的应用程序以呈现为网页?接下来我需要采取什么步骤?
谢谢!!!
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)您必须编译Java文件并生成一个.class。您可以使用命令来完成javac。否则,您可以在Eclipse工作区的文件夹中查找,并在项目的文件夹中找到.classeclipse生成的文件。
.class
javac
3)将此文件放在一个名为WEB_INF\classes\exampletomcatdocuments文件夹根目录的文件夹中。(示例是包的名称)
WEB_INF\classes\example
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>
要求用户插入单词的表单会自动调用此示例:
<!-- 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>
问候卢卡