Using Java to produce HTML like a php script

S

spudtheimpaler

I am a university student using a program that requires updated xml
from a webpage (well, a webserver). Now i know this can be done with
php and apparently quite easily but I would really rather do this with
Java (as the program that will be manipulating the information to be
described is going to be in java if possible).

So my question is are there any tools / apis that are good at
outputting xml in a web page/server setting for java?

Kind Regards,

Mitch.
 
R

Rhino

I am a university student using a program that requires updated xml
from a webpage (well, a webserver). Now i know this can be done with
php and apparently quite easily but I would really rather do this with
Java (as the program that will be manipulating the information to be
described is going to be in java if possible).

So my question is are there any tools / apis that are good at
outputting xml in a web page/server setting for java?
Sorry, your question and your subject seem to be at odds with one another.

What format are you reading and what format are you writing?

If you're simply looking to generate HTML within a Java program, as your
subject suggests, here is a simple Java class that does exactly that:

================================================================
public class MyHtmlWriter {

public MyHtmlWriter() {
}

/**
* Writes the file.
*
* @param outfile the name of the file that will be written by this
class.
*/
public void writeFile(String outfile) {

/* Identify the output file. */
String strOutfile = OUTPUT_FILE_PATH + "my.html";

/* Delete the old version of the output file. */
File outfile = new File(strOutfile);
outfile.delete();

/* Open the output file. */
FileOutputStream fos = null;
OutputStreamWriter osw = null;
PrintWriter pw = null;

try {
fos = new FileOutputStream(strOutfile, true);
} catch (FileNotFoundException excp) {
System.err.println("FileNotFoundException on file " + strOutfile
+ ". Error: " + excp);
excp.printStackTrace();
return;
}

osw = new OutputStreamWriter(fos);
pw = new PrintWriter(new BufferedWriter(osw), true);


printWriter.println("<html>");
printWriter.println("<body>");
printWriter.println("<p>This is my document.</p>");
printWriter.println("</body>");
printWriter.println("</html>");

printWriter.flush(); /* write out any unwritten lines */
printWriter.close(); /* close the file */

/* Tell the user where to find the report. */
System.out.println("The output was written to file " + outfile +
".");

return;
}
}

================================================================

If that is not what you're trying to do, please clarify your requirements.

Java is certainly capable of reading and writing XML if that is a concern
for you.

It is also possible to use something called XSL to convert XML documents to
HTML. XSL could involve Java but doesn't have to; I've got some XML docs
(log files) that I convert to HTML without involving Java at all: there is
an XSL file that describes how the XML is supposed to be converted to HTML,
then I run a trivial script to do the actual conversion, supplying the name
of the XML file and the name of the XSL file. The XSL is capable of some
reasonable smarts so it can choose particular records and ignore others,
format some records differently than others, etc.

It's probably not a good idea to use Java for everything, just because it is
possible. You can use a wrench to hammer something but sometimes a hammer is
a better choice. :)

Rhino
 
J

John C. Bollinger

Hal Rosser wrote:
[[attribution dropped] wrote:]
Try JSP and Servlets with Apache Server
This book by Murach will lead you by the hand
http://www.murach.com/books/jsps/index.htm

To amplify on that a bit, Java Servlets are the fundamental Java
technology for serving web requests. Java Server Pages (JSP) is a web
scripting framework that operates on top of servlets and is tightly
integrated. There are other frameworks that run either alongside or on
top of these but chances are good that you will not need them.

If you will be serving XML documents that are mostly boilerplate but
have some dynamic parts, then JSP is a great choice. If you will be
serving highly dynamic documents then you probably want a pure servlet.
In the middle, the choice is a little less clear.
 
S

Stefan Ram

Rhino said:
If you're simply looking to generate HTML within a Java program, as your
subject suggests, here is a simple Java class that does exactly that:

Today, to answer a question in another newsgroup, I wrote
an example to show how to do basic HTML-templating with a
small class using only standard J2SE operations.

public class Main
{

/* Assume, you have a basket of shopping items */

final static java.lang.String basket[] =
{ "bread <will cost only $2 tomorrow>",
"butter",
"cheese" };

/* And a template that is not restricted to a fixed
number of shopping items */

final static java.lang.String template =
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\"\n" +
" \"http://www.w3.org/TR/html4/strict.dtd\">\n" +
"<HTML>\n" +
" <HEAD>\n" +
" <TITLE>Warenkorb</TITLE>\n" +
" </HEAD>\n" +
" <BODY>\n" +
" <OL>\n" +
"$ware[[ <LI>$name\n]]" +
" </OL>\n" +
" </BODY>\n" +
"</HTML>\n" +
"\n";

/* notice that the template " <LI>$name\n" is given
for every $ware, implying a loop */

public static java.lang.String quoteCharacters
( final java.lang.String text )
{ final java.lang.StringBuilder stringBuilder =
new java.lang.StringBuilder( "" );
for( int i = 0; i < text.length(); )
{ int code = text.codePointAt( i ); i += Character.charCount( code );
if( code < ' ' || code > '~' || '<' == code ||
'>' == code || '&' == code || '\'' == code || '\"' == code )
{ stringBuilder.append( "&#" );
stringBuilder.append( code );
stringBuilder.append( ";" ); }
else
{ stringBuilder.appendCodePoint( code ); }}
return stringBuilder.toString(); }

public static java.lang.String wareI
( final java.lang.String template, final int i )
{ return java.util.regex.Matcher.quoteReplacement
( template.
replaceAll( "\\$name", java.util.regex.Matcher.quoteReplacement
( quoteCharacters( basket[ i ] )))); }

public static java.lang.String wares
( final java.lang.String template )
{ final java.lang.StringBuilder stringBuilder =
new java.lang.StringBuilder();
for( int i = 0; i < basket.length; ++i )
{ stringBuilder.append( "" + wareI( template, i )); }
return stringBuilder.toString(); }

public static java.lang.String ware
( final java.lang.String template )
{ java.util.regex.Pattern pattern =
java.util.regex.Pattern.compile( "(\\$ware)\\[\\[([^\001]*?)\\]\\]" );
java.util.regex.Matcher matcher = pattern.matcher( template );
java.lang.StringBuffer stringBuffer = new java.lang.StringBuffer();
while( matcher.find() )
{ final java.lang.String string = matcher.group( 2 );
matcher.appendReplacement
( stringBuffer, wares( string )); }
matcher.appendTail( stringBuffer );
return stringBuffer.toString(); }

public static void main( final java.lang.String[] args )
{ java.lang.String template;
template = ware( Main.template );
java.lang.System.out.println( template );
java.lang.System.out.println( basket ); }}

The output is:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<HTML>
<HEAD>
<TITLE>Warenkorb</TITLE>
</HEAD>
<BODY>
<OL>
<LI>bread <will cost only $2 tomorrow>
<LI>butter
<LI>cheese
</OL>
</BODY>
</HTML>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top