Solved

input string to form url to read a file from a server

Posted on 1997-05-07
7
211 Views
Last Modified: 2010-04-16
I have been trying to write an applet which allows the user to input 4 digits and take the input string to form a url
with it.
         I have an applet which can get a file from the server and one which can echo user input but I am stuck.
     
        The url is http://abcdefg/hijkl/m_nnnn.txt 
The user is to input "nnnn" and my applet is supposed to
form the url and display the file in their browser.
I have tried to combime these two programs but the  getfile
program is not quite right because it depends upon having an url in the applettag
The textfield program merely echos the user input.
I want to take that input and then form the url to read the file. Can anyone please help me. Thank you. Joseph Ruisi
        the code to echo user input is -
import java.awt.*;

public class TextFieldTest extends java.applet.Applet {

public void init() {
   
       MyTextField text = new MyTextField();
     add(text);
    }
 }
  class MyTextField extends TextField {
   
       public MyTextField() {
   super("Type Your Text",20);
}
}
and the program to get the file is

import java.awt.*;
import java.net.*;
import java.io.*;


public class getFile extends java.applet.Applet {

TextArea text;
String URLString;

public void init() {
URLString = getParameter("url");
setLayout(new BorderLayout());
}

public void start() {
text = new TextArea();

add("Center",text);
add("South", new Button("press"));
try {
int nread;
byte buffer[] = new byte[1024];
URL url = new URL(URLString);
InputStream is = url.openStream();
while ((nread = is.read (buffer)) > 0) {
text.appendText(new String(buffer, 0));
}
}
catch (Exception exp) {
text.setText("Cannot Load: " + URLString  + "\n" + exp.toString());
}
}

}

0
Comment
Question by:boksi1
  • 3
  • 3
7 Comments
 
LVL 1

Accepted Solution

by:
mlimotte earned 100 total points
ID: 1220230
You only need one applet... something like the following, should do what you are asking for.  You may also want to add code to restrict what is entered into the TextArea.


import java.awt.*;
import java.net.*;
import java.io.*;

public class getFile extends java.applet.Applet {
   
    TextArea text       = new TextArea();
    Button   btnOk      = new Button("Ok");
    String   server     = "abcdefg",
             url_prefix = "/hijkl/m_",
             url_suffix = ".txt";

    public void init() {
        setLayout(new BorderLayout());
        add("Center",text);
        add("South", btnOk);
    }
   
    public boolean action(Event event, Object what) {
        if (event.target == btnOk)
        {
   
            try {
                URL url = new URL ("http", server,
                    url_prefix + text.getText() + url_suffix);
                getAppletContext.showDocument(url);
            }
            catch (Exception exp) {
                text.setText("Cannot Load");
            }
   
            return true;
        }
        else
            return super.action(event, what);
    }

}

0
 
LVL 6

Expert Comment

by:jpk041897
ID: 1220231
Edited text of question
0
 

Author Comment

by:boksi1
ID: 1220232
Yes First and simply thank you - your approach in one applet is straightforward and I think I understand it. I apologize for my ignorance, but when I compile it in JDK1.1.1 I get,
...undefined variable or class name; getAppletContext
the statement which is flagged is
    try {
                      URL url = new URL ("http", server,
                      url_prefix + text.getText() + url_suffix);
              ----->  getAppletContext.showDocument(url);
                      }
I would like to eliminate this error, I agree with your single applet design, truly portable, and neat. Can you help with the compile message- I really want to test this - it is a pleasure to
talk with someone as knowledgeable as yourself amd I am grateful for your help. I want to use this if I can eliminate the compiler message. Thank you. Yours truly, Joseph Ruisi

0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:boksi1
ID: 1220233
Yes I am hoping that mlimotte will add a further comment
and I can eliminate the compiler error. Thanks so much, Joseph Ruisi
0
 

Author Comment

by:boksi1
ID: 1220234
Sorry, that was my fault.  getAppletContext is a method, not a property of Applet.  Therefore it should be accessed with () as in:

 ...

URL url = new URL ("http", server,                         url_prefix + text.getText() + url_suffix);
getAppletContext().showDocument(url);

 ...

0
 
LVL 1

Expert Comment

by:mlimotte
ID: 1220235
I left the () out of the original code.  Simply add the parens () after the word getAppletContext and before the period.


0
 
LVL 1

Expert Comment

by:mlimotte
ID: 1220236
Yes - getFile has compiled successfully and I will be testing it
Thank you for the help- I am now on the right track-
Mlimotte- THANK YOU!!

0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Android studio getdrawable(int) is deprecated 4 88
print map entry 34 69
String array comparison 4 34
going to wrong jsp page 2 22
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question