• Post Reply Bookmark Topic Watch Topic
  • New Topic

sending an Javascript value to a Java class  RSS feed

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I made a Java program that needs to be sents 3 integer values, and will return 1 integer value which will be printed w/ javascript. It needs to be accesessed through a website, so I was going to use a segment of javascript to do this. How can I send javascript "var" value to a java "int" value? If that isn't possible, how would I go about doing this? Thanks for the help.
 
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm afraid you're mixing apples & oranges, which might be delicious but probably won't compute.

In web apps, JavaScript is written mixed into an HTML page and executed inside the browser. Java is compiled and installed on a web server and executes on the server. At least this is the "normal" model you'll see most often, and it doesn't easily allow JavaScript to call Java methods or vice versa.

As always, there are exceptions. It is possible to write Java Applets that run inside the browser. Sometimes you'll see this done for animation or complex data entry forms. You'll notice a few seconds delay and the status bar on the browser says "loading applet" or something like that. Anyhow, Java Applets can interact with the JavaScript on the HTML page. It's not trivial and it's not pretty, but it is possible.

Could you describe your situation in a little more detail? What is the user seeing on the browser? What do they do to kick off communication with Java? What do they see next?
 
Mike Hebear
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help. I am writing this for my high school which runs on a 7 day cycle. I created a text file with all the school days (weekends and holidays are excluded) and want to simply display what cycle day (1-7) coresponds with each date. This is the java program i have written that reads from the text file and returns what cycle day it is, the only problem is that in the constructor it needs to be sent the current date when a user visits the site:

import chn.util.*;

public class cycleDay
{
private int myMonth, myDay, myYear;

public cycleDay(int month, int day, int year)
{
myMonth=month;
myDay=day;
myYear=year;
}

public int getCycleDay()
{
FileInput inFile = new FileInput("noWeekEnds.txt");
int x=0, day=0, month=0, year=0;
while (!(day==myDay && month==myMonth && year==myYear))
{
month=inFile.readInt();
day=inFile.readInt();
year=inFile.readInt();
x++;
}
x%=7;
if (x==0)
x=7;
return x;
}
}

I know it reads all the dates in correctly and it returns the correct cycle day... I just dont know the easiest way to diplay it on a website, and the easiest way to send the program the current date. Sorry this post was so long, and thanks for the help! (www.hinghamschools.com)
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cool. Are you a student there?

What is your web server? If it's one that supports a java plug-in you could get into Servlets and JSPs. When the server is getting the HTML page it doesn't just read it from disk and send it back to the browser. It calls out to some Java code that can read databases, do computations or just about anything else and give back complete HTML pages or just values to plug back into the HTML. This whole area is not trivial to learn, but it has good buzz these days and would not be a bad thing to know.

Perhaps simpler alternatives exist in CGI or PHP. Ask your web server owner if they support any of these technologies and can get you started. This site has forums (forae?) for JSP and Servlets if you want to explore those options more.
 
Mike Hebear
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I am a student, and thanks for the help. I'll look into the JSP and servlets.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!