Mark Taylor
Mark Taylor

Reputation: 1188

Moving method outputs from a java class to HTML

I'm programming in Java and have a simple hello-world type class which can provide outputs to the console quite happily.

What I would like to do is transfer the outputs of this, or a similar class, to HTML - preferably without using having to use an applet.

In my head I'm imagining a an included line of code in the HTML file which will read along the lines of 'call this class file with the following parameters and print the results here'

I'm quite new to Java but have a lot of experience in programming in general.

Kind Regards

Upvotes: 0

Views: 236

Answers (1)

Ravi K Thapliyal
Ravi K Thapliyal

Reputation: 51711

In my head I'm imagining a an included line of code in the HTML file which will read along the lines of 'call this class file with the following parameters and print the results here'

You are on the right track. This html in Java would be a .jsp file (like already suggested by @henryabra) which you would have to deploy into a web container (like Tomcat) which would then process your inline block of code and call your Java class method for you any time someone requests it over the web.

So, basically you could just rename your .html (ready with all the body and table and heading tags that would determine how the content would be rendered by a browser) to a .jsp file and then add dynamic blocks of code into it with jsp scriptlets.

<h1>Hello World</h1>
<p> My message to you is:<br />
<% // this is a scriptlet
  out.write(new HelloWorld().getMessage());
  // out lets you write to the html response stream
%>
</p>

Please, note that although JSP files are quite similar to the html ones they'll require a page directive which you would also use to import your Java classes as follows

<%@ page language="Java"
 contentType="text/html;charset=UTF-8" import="pkg.path.to.HelloWorld" %>

Upvotes: 1

Related Questions