1

Okay, this is got to be the easiest question of the day. This is my first stab at Java and 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.

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

3
  • I think what you need is a web-based distributed process. Commented Jul 31, 2011 at 22:45
  • Are you sure I don't need a B-to-B leveraged portal? Commented Jul 31, 2011 at 22:47
  • The missing link is the servlet: stackoverflow.com/tags/servlets/info Commented Aug 1, 2011 at 1:37

2 Answers 2

9

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

Regards, Luca

Sign up to request clarification or add additional context in comments.

Comments

2

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.

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

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

Then, create a JSP page, see this tutorial.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.