Separating design from code - help a Perl monger - java noob

L

lord0

Hi there,

I've recently been dabbling in JSP and servlets after years of hacking Perl
*shucks*. What I would like to do it to seperate the design of my webpages
i.e. the HTML as much as possible from the code i.e. the java.

Now I stand to be corrected but JSP don't seem to be the solution as you end
up (well I do) with loadsa code in a page which is then compiled into a
servlet anyway - yuk, yuk. Servlets don't seem quite right, though I'd
rather use servlets than JSP, as I seem to end up with servlets full of
HTML - yuk.

In Perl I'd use something like HTML::Template which allows you to embed
"variables" in HTML pages which are then filled in by the script i.e.

<p>Welcome back <TMPL_VAR USERNAME></p>

I'd like to do something like this with a servlet and a (HTML) template

I have looked into this but I find the array of _possible_ solutions a
trifle confusing. i.e. Struts, XMLC, HTML.Template.java, JSP includes etc
etc.

Is there an generally accepted/common/best way to do this? Any advice/urls
etc appreciated

Cheers

Lord0
 
W

Wendy S

lord0 said:
Now I stand to be corrected but JSP don't seem to be the solution as you end
up (well I do) with loadsa code in a page which is then compiled into a
servlet anyway - yuk, yuk. Servlets don't seem quite right, though I'd
rather use servlets than JSP, as I seem to end up with servlets full of
HTML - yuk.

You don't use Servlets *or* JSP, you use both. Servlets to do the
processing, perhaps query a database or whatever else you need done, then
you forward to a JSP that does the display. Keep the java code out of the
JSP, and keep the HTML out of the Servlet.
<p>Welcome back <TMPL_VAR USERNAME></p>

What Servlet container are you using? Tomcat 5 can do expressions:
<p>Welcome back ${userName}</p>

In earlier versions, you might use the JSTL taglib:
<p>Welcome back <c:eek:ut value="${userName}"/></p>

Do you have a purpose in mind? You mentioned Struts, which is great if you
have more than a trivial number of HTML forms to deal with, but I don't
recommend starting with it until you understand Servlets and JSP on their
own.

Although it's old, Jason Hunter's _Servlet Programming_ book still provides
a good introduction. (Or perhaps there is a newer version out.)
 
L

lord0

You don't use Servlets *or* JSP, you use both. Servlets to do the
processing, perhaps query a database or whatever else you need done, then
you forward to a JSP that does the display. Keep the java code out of the
JSP, and keep the HTML out of the Servlet.

Using both seems a bit clunky - I guess I'm looking at it wrong. I thought
that JSP were "compiled" into servlets therefore the servlet to JSP solution
didn't seem like the most efficient but as I said my knowledge ain't that
good :-(

could you give a brief example?
What Servlet container are you using? Tomcat 5 can do expressions:
<p>Welcome back ${userName}</p>

Tomcat 4.1.29
In earlier versions, you might use the JSTL taglib:
<p>Welcome back <c:eek:ut value="${userName}"/></p>

Do you have a purpose in mind? You mentioned Struts, which is great if you
have more than a trivial number of HTML forms to deal with, but I don't
recommend starting with it until you understand Servlets and JSP on their
own.
Really I'm using it all as a test bed and I was looking for the
"recommended" way to keep design and programmatic issues seperate. I can get
servlets and JSP running fine, connecting to DB's, parsing form values etc
no problemo but it all kinda looked like the dodgy old CGI scripts you get
with loadsa embedded HTML.
 
S

Sudsy

lord0 said:
Really I'm using it all as a test bed and I was looking for the
"recommended" way to keep design and programmatic issues seperate. I can get
servlets and JSP running fine, connecting to DB's, parsing form values etc
no problemo but it all kinda looked like the dodgy old CGI scripts you get
with loadsa embedded HTML.

That's the idea: the servlets and custom tags don't contain HTML,
only the JSPs. And the JSPs shouldn't include scriptlets (embedded
Java code) but use tag libraries to access data populated in any
of the available scopes. The idea is that the Java programmer
doesn't worry about creating HTML tags in their code and the
graphic artist doesn't have to incorporate Java code in their
carefully laid-out pages.
That being said, most shops don't have the financial resources
to separate the tasks and so the programmer ends up doing it all
anyway! "Would you like some Flash with that?" ;-)
 
C

charly

Greetings,

I used to program in PHP and do the same thing than you with templates.

Then switched to Java and discoverd Jsps and servlets.

Then wondered how to go back to templates

So far, I've found that taglibs are great :

You put one tag which does Java tasks, it can even get paramters (google
with taglibs for more info) :

You put your tags in your jsp, write a class corresponding to your tag
and that's it :

A link which does better job than me to explain things :) :

http://www.orionserver.com/ -> tutorial ->taglib

Good luck :)
 
J

John C. Bollinger

lord0 said:
Using both seems a bit clunky - I guess I'm looking at it wrong. I thought
that JSP were "compiled" into servlets therefore the servlet to JSP solution
didn't seem like the most efficient but as I said my knowledge ain't that
good :-(

Using both in the same application, just as Wendy described, is a widely
accepted practice. The forward is accomplished inside the servlet / JSP
container, and thus its efficiency is not usually an issue. I guess I'm
a bit confused, though -- you asked about seperating logic from
presentation, but then found a solution that did so "clunky". It's not
clear to me from your comments what you don't like about it, but I guess
you must have some alternative idea about the form the solution should
take. Care to elaborate?

It is true that JSP code is translated (that's the term used in the
specs, IIRC) into Java code for a servlet, and then the servlet source
compiled to a corresponding Java class for use servicing requests. The
servlet class is used as long as the associated JSP code is unchanged --
perhaps longer, depending on the configuration of the JSP engine.
That lends an alternative reading to Wendy's comment: you _always_ use
both together because a JSP is just a friendly face over a servlet.

You might also consider custom tags, either via one of the available tag
libraries (e.g. JSTL) or by writing your own. Sudsy aluded to that
solution, and you might find it more elegant.

JSP also has some bindings to JavaBeans components, which you can take
you at least some of the way toward removing Java code from your JSP.
The code goes into a bean instead.


John Bollinger
(e-mail address removed)
 
M

Michiel Konstapel

In Perl I'd use something like HTML::Template which allows you to embed
"variables" in HTML pages which are then filled in by the script i.e.

<p>Welcome back <TMPL_VAR USERNAME></p>

I'd like to do something like this with a servlet and a (HTML) template

I have looked into this but I find the array of _possible_ solutions a
trifle confusing. i.e. Struts, XMLC, HTML.Template.java, JSP includes etc
etc.

Is there an generally accepted/common/best way to do this? Any
advice/urls
etc appreciated

Sounds like you should take a look at templating engines. I've had very
good experiences with Velocity (http://jakarta.apache.org/velocity).
HTH,
Michiel
 

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,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top