I really did not want to be posting this, but here I am. It looks like I have been unable to find any way to simply display a java variable in an html doc. I have a feeling this isn't possible and I should be using javascript or something else but I would love it if someone could point me in the right direction.
My Java code is simple and basically just creates a single int that I would like to use in my html doc. For arguments sake let's imagine this:
JAVA CODE
public class Counter {
public static void main(String[] args) {
int number = 24;
}
}
Once again for argument sake
HTML CODE
<HEAD>
<script src="java/counter.java"></script>
</HEAD>
I've looked into things like document.write() and Element.innerHTML but I believe these things are probably associated with Javascript and/or JSP. I'm expecting more of an explanation rather than some simple code since I'm sure I'm going about this wrong. Just want to know how I should create my "counter", whether it be in Javascript or something else. I am using PHP and CSS so I also am also not sure if I should be using those to access my programs final int. First dive in to multi language programming so be gentle. Thanks!
you have to write your variable like this
"<%= variable name here %>"
in your JSP file
The first thing i have to mention is "YOU CANNOT DO IT WITHOUT AN APPLET OR JAVA WEB SERVER" with live updates of your variable.
If you don't want to live update your web page, you can create a .html file with java and open it through your browser.
If you are free to use another programming option you can use javascript instead of java.
If you need both live update of variable and java as programming language, you should use one of java applets and jsp page with server. I recommend to use jsp since it is highly available of resources.
First of all, lets clear smth here. Java is an interpreting language, which means you cannot run java without calling its executable interpreter or a web container or application deployment platform.
If you run java code, very firstly you should compile it and get compiled byte code. This would be a ".class" file. Then if you want to run it as standalone application then you should use platform dependent interpreter. For Windows OS, it is "java.exe", for linux platforms it would be "java" executable. For WEB applications there are some sort of containers like Jetty, Tomcat which can run class files and compile (JSP files not java).
Lets assume that, you are using Tomcat with your web application. Then with your app, you can run your code with JSP file or with async call it is difficult in your case.
Find a very simple JSP tutorial and try howto run your code.
Related
I'm fairly new to web development, but I've take a couple hands-on introductory courses where the basic structure of a website is you have an HTML file (or several ejs template files), a CSS file, and then you have something called a "server" file which is a JavaScript file, typically called app.js
I know that if I wanted to, I could include all my CSS inside the HTML file in <style> tags. My question is, should I have the need to do so, would it be possible for me to include the server code in <script> tags within a single HTML file?
Debating on whether or not I should put forth the effort to attempt to do so, but if it's generally problematic (note: I don't car about "best practice") for some reason such as it prevents you from working with certain packages/modules like EJS or Node.js then I will likely conclude that it doesn't make sense to try. As a novice web developer, I fear I am not aware of certain restrictions that I would likely run into if any (besides lack of module support).
would it be possible for me to include the server code in <script> tags within a single HTML file?
No.
Web browsers, which execute the scripts in webpages, do not provide JavaScript programs with the APIs required to run a server.
If you want to write a web server in JavaScript then you'll need to run it using a tool like Node.js and not a web browser.
No, you cannot put server-side code in HTML or in JS files that are sent to the client. Server-side code is code that is run on a server that you host, while client-side code is code that is run on the computer of the person who is visiting your webpage.
You can put client-side code in HTML <script> tags., and you can also include client-side code in separate JS files. The drawbacks are immense though, as:
Code highlighters won't be able to highlight your code and point out things like syntax errors as easily.
You won't be able to use tools like Babel and TypeScript, which input and output JS files.
You can't use NPM modules without a bundler like Webpack, which itself will output JS files.
Overall, there isn't really a good reason to use the <script> tag. It takes 2 extra seconds to just create another file for your JS, and it's more organized, more modularized and easier to use with external tools.
I'm working on a legacy ASP.NET project, which I'm trying to massage into shape slowly, but I can't make significant changes without it all collapsing like a chocolate finger house....
I've tried to find a solution for this, but failed miserably due to the specific mix of terminology ("javascript", "embedded" and/or "resource" just give me information as to how to embed a .js file...), and the fact that it's probably a weird way of doing things.
The project makes use of App_GlobalResources for translatable strings, which is used in C# code behind, the markup and in some of the JavaScript. For example:
HTML:
Text="<%$Resources: Resources, MeasuresLabel %>"
JS:
setDialogTitle('summaryDialog', physicalElementName, Resources.Resources.Summary);
This was all working fine, until we started using NUnit for some integration testing. To make the Resources.resx available to NUnit I've followed Scott Allen's suggestion, which works for NUnit but means that the above Resources.Resources is undefined.
So, my question is with this embedded file, how can I make it accessible to the JavaScript (or is there a better way I can make it available to NUnit)?
There's an the added complication due to the code I want to access it with being in separate .js files, rather than within script tags in the .aspx files.
EDIT
After looking at it some more, the real challenge is the properties that are accessed in the JS are static. Due to the number of places these are referenced, it's not practical to add a variable for each string that's being used.
I think that JS files aren't going through the ASP.NET engine and they are served as they are, that's why Resources.Resources.Summary isn't defined in your case, while it will work when embedded inside <script> tag in .aspx file.
The only way I know around this would be to store the used resource values in global javascript variables on the .aspx page and then use them in loaded JS files.
I am relatively new to web programming (just started html, javascript, php). I wanted to create a database manager for a website I am building. Now, in ios I would create a .h file called DBManager, with a list og methods I can call, and then import this file in any other .m file and do something like:
[DBManager uploadInage: image with completition...];
How can I do this for the web?
There is not a direct equivalent in the languages you mention.
HTML is a markup language and as such does not have funcitons at all.
Javascript and PHP are both interpreted languages, which can be used inline in your HTML. You will find that they aren't as structured in some ways as compiled languages like Objective-C. You can use different approaches to structuring your code, but there is no compiler enforcing them.
So for example, you can deploy some Javascript with your HTML which makes a method call, and it doesn't matter if that method was even written yet. As long as it can be found at runtime, it will work.
The same goes for PHP. Nothing will prevent you from writing and deploying your code making any method calls you like. As long as those methods can be found in some file that's included when the script runs, the code will work.
include() and require() are the commands used to pull in other .php scripts which contain the classes or functions you need. You could write a "header" script which includes all of your dependencies if you like to structure things that way. There are also dependency management tools like Composer - but these are entirely optional.
I don't think its safe (and even possible? Nah, I'm pretty sure it isn't possible ;) ) to directly communicate with a database from HTML / JavaScript / ... .
Objective - C is a language that has a good MVC pattern. (Model / View / Controller). HTML is a markup - language (VIEW - only). You can call a webservice from this view, (With the aid of AJAX or other..), and that webservice can communicate with a database, and provide you the needed information. ==> Anyhow, there is a need to create a webservice here!
You can also integrate everything in a Web-application. You can build a webapp on (I like Java, others like .NET, and there are many more...). This is an application that (just like Objective-C) uses an MVC pattern. The HTML page acts as the view, (Storyboard in XCode), you have a controller that builds and fills/returns the view to the user, (just as the ViewControllers).
You should look at both options, depending at what you want to do, both choices work.
I'm happy to help you with further questions or remarks.
I am developing an application and have some important information inside the code like IPs and stuff which has to be private. I use apache web server as a server and wanna know if there is any way hide a javascript code or moving it outside root folder ?
No, by definition. Javascript code is run on the client's computer. That means that the client must have access to the Javascript source for it to work. You could use AJAX to hide certain data until it is needed, but even then the client (and any user with Firebug) would be able to view it.
The only way to "hide" it would be through some sort of obfuscation utility or to minify the javascript. But, unfortunately, once the javascript code makes it to the browser. There is not much you can do about it.
I'm working with a tool that only allows for Javascript as the scriptting language. With the script, I need to launch a process. How would I go about this?
The javascript code is running on the client that will launch the process. The javascript interpeter is RhinoJS.
So my question remains:
1. Is there a way that I can call a specific Java class from Rhino [ProcessBuilder]?
or
2. Is there a way to launch an executable from Javascript? [I've tried the UniversalXPConnect route, but it turns out that the version of Rhino I'm using doesn't really worry about permissions]
That was quick [I found the answer right after I asked]:
var pb = new java.lang.ProcessBuilder("notepad.exe", "c:\test");
pb.start();
Basically RhinoJS has a quirk to allow it to directly access Java functionality. So basically once should just launch the process from there.