aspose file tools*
The moose likes Servlets and the fly likes How to send info from a .java file to html Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to send info from a .java file to html" Watch "How to send info from a .java file to html" New topic
Author

How to send info from a .java file to html

Andreea Georgiana
Greenhorn

Joined: Sep 10, 2013
Posts: 5
Hello,

I am working on an application that manages some items and I need to send information (from the .java file to html) about the number of items so I can put that number as a value in a form using html and javascript. I also need to send the information backwords. How can I do that?

Thank you!
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10370
    
    8

Welcome to the Ranch.

An html file is a static resource and as such you cannot "send" any information to it.
A java file is the source code. It is not used when you run any java application. The compiler compiles the java files to class files and those are the ones which are used.

You will need to rephrase what problem you are trying to solve here, so we can offer assistance.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Andreea Georgiana
Greenhorn

Joined: Sep 10, 2013
Posts: 5
Maneesh Godbole wrote:Welcome to the Ranch.

An html file is a static resource and as such you cannot "send" any information to it.
A java file is the source code. It is not used when you run any java application. The compiler compiles the java files to class files and those are the ones which are used.

You will need to rephrase what problem you are trying to solve here, so we can offer assistance.



I'm sorry for my mistake. I will try to explain it better.
I have some java classes and after creating some objects using them, I want to count those objects and display that number in an html form.
How can I do that?
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10370
    
    8

So
1) Is the html file being served from some server like e.g. Tomcat?
2) Where are the java objects created? On the same machine in #1 above or a different machine?
3) Is this an academic learning exercise or a real world problem you are trying to solve?
Andreea Georgiana
Greenhorn

Joined: Sep 10, 2013
Posts: 5
Maneesh Godbole wrote:So
1) Is the html file being served from some server like e.g. Tomcat?
2) Where are the java objects created? On the same machine in #1 above or a different machine?
3) Is this an academic learning exercise or a real world problem you are trying to solve?


1. I don't know, I did not use on purpose Tomcat or anything else.
2. I want to create the items on the same machine
3. This is both for learning and it is a real problem. I want to create a web app that displays a calendar and for each day of the calendar I want to display 2 numbers: number of items available and number of items used. But in order to calculate these numbers I need to see the length of the array holding the items.

Can you please help?
Thank you
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10370
    
    8

I want to create a web app

.htm/.html files are usually rendered in a Browser. But they need (usually)* to be served by some server. Typically a web server like Tomcat. This is how web apps work.
*Of course this does not mean you cannot open the html file from your local disk in your local browser.
I want to create a web app that displays a calendar and for each day of the calendar I want to display 2 numbers: number of items available and number of items used. But in order to calculate these numbers I need to see the length of the array holding the items.

Since you are starting off, I would recommend small steps.

Step A) Create the html file dynamically, write it to your disk and open in the browser. (Both file and browser are on your own machine)
Step B) Do the same as A, but over the network.

For Step A.
1) Do you know how to write a small java program which will create an Array of objects, populate it and then print the number of objects on to the console?
2) Do you know how to create html files?
3) Do you know how to write a file to the disk?

Andreea Georgiana
Greenhorn

Joined: Sep 10, 2013
Posts: 5
Maneesh Godbole wrote:
I want to create a web app

.htm/.html files are usually rendered in a Browser. But they need (usually)* to be served by some server. Typically a web server like Tomcat. This is how web apps work.
*Of course this does not mean you cannot open the html file from your local disk in your local browser.
I want to create a web app that displays a calendar and for each day of the calendar I want to display 2 numbers: number of items available and number of items used. But in order to calculate these numbers I need to see the length of the array holding the items.

Since you are starting off, I would recommend small steps.

Step A) Create the html file dynamically, write it to your disk and open in the browser. (Both file and browser are on your own machine)
Step B) Do the same as A, but over the network.

For Step A.
1) Do you know how to write a small java program which will create an Array of objects, populate it and then print the number of objects on to the console?
2) Do you know how to create html files?
3) Do you know how to write a file to the disk?



1. Yes, I know how to do that
2. I have created a page with a calendar(found the calendar on the internet), which looks like this:



I can see my page in a browser.
My app is running on a glassfish server. Is that a replacement for tomcat?
Now I want to display the number of elements in my array.

Thank you very much for your time!
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10370
    
    8

Ok.
So now you need to make it "dynamic" i.e. serve the html but the value of number of objects changes at runtime.
The usual way to do it is using servlets (not really. You should be using JSP with servlets but since you are starting off, it should be ok)

Write a servlet which will generate the html
While generating the html, figure out the num objects and accommodate this value in your html.
Once done generating, serve this html to the calling browser.

More on servlets here http://www.oracle.com/technetwork/java/index-jsp-135475.html
http://simple.souther.us/ (The author Ben, is one of the Sherrifs here at the ranch)
http://www.coderanch.com/how-to/java/ServletsFaq

If you run into problems, post your code (using code tags) along with the details of the problem you are facing in the Servlets Forum
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38795
    
  23
Maneesh Godbole wrote: . . . post your code (using code tags) along with the details of the problem you are facing in the Servlets Forum
Good idea, Maneesh. I shall move this discussion to the servlets forum anyway.

I have added code tags because you are new, and the code looks a lot better , but it would have been better still if you had used spaces for indenting, not tabs. Please read the code tags link, particularly about long lines.
Andreea Georgiana
Greenhorn

Joined: Sep 10, 2013
Posts: 5
I will try to implement this solution and I will ask if I need more help. Thank you very much for your help!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to send info from a .java file to html