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

Discussion in 'Java' started by lord0, Dec 15, 2003.

  1. lord0

    lord0 Guest

    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
     
    lord0, Dec 15, 2003
    #1
    1. Advertising

  2. lord0

    Wendy S Guest

    "lord0" <> wrote
    > 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.)

    --
    Wendy in Chandler, AZ
     
    Wendy S, Dec 15, 2003
    #2
    1. Advertising

  3. lord0

    lord0 Guest


    > 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?

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

    >
    > 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.
     
    lord0, Dec 15, 2003
    #3
  4. lord0

    Sudsy Guest

    lord0 wrote:
    > 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?" ;-)
     
    Sudsy, Dec 15, 2003
    #4
  5. lord0

    charly Guest

    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 :)
     
    charly, Dec 16, 2003
    #5
  6. lord0 wrote:

    >>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 :-(


    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
     
    John C. Bollinger, Dec 16, 2003
    #6
  7. > 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
     
    Michiel Konstapel, Dec 20, 2003
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chad Z. Hower aka Kudzu

    Separating code from design

    Chad Z. Hower aka Kudzu, Jan 23, 2004, in forum: ASP .Net
    Replies:
    6
    Views:
    438
    Chad Z. Hower aka Kudzu
    Jan 24, 2004
  2. Earl Teigrob
    Replies:
    3
    Views:
    601
    John Saunders
    Jun 10, 2004
  3. IRAQI OIL IS TOO PRECIOUS

    @ Friedman is a malicious mischief monger and misleader

    IRAQI OIL IS TOO PRECIOUS, May 10, 2004, in forum: C Programming
    Replies:
    2
    Views:
    373
    Christopher Benson-Manica
    May 10, 2004
  4. Íéêüëáïò Êïýñáò

    Separating design from code

    Íéêüëáïò Êïýñáò, Mar 22, 2011, in forum: Python
    Replies:
    0
    Views:
    199
    Íéêüëáïò Êïýñáò
    Mar 22, 2011
  5. Replies:
    29
    Views:
    741
    Rainer Weikusat
    Jul 26, 2012
Loading...

Share This Page