Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Basic Java Servlet Question

Options
  • 11-11-2013 3:27pm
    #1
    Registered Users Posts: 2,815 ✭✭✭


    I’ve been looking into expanding my knowledge of Java into web based technologies and I’m starting with servlets. I am able to output HTML to the browser using the “out.println” method; and receive parameters from the user via the request object.

    Obviously, a website can have static and dynamic content and I presume the static stuff isn’t outputted like that (including the HTML, HEAD, META, STYLE, SCRIPT tags)

    So let’s say I want a simple webpage that displays one line

    “The server time is “ + serverTime

    “The server time is ” is static. What is the simplest way to achieve this? I presume there needs to be a webpage template somewhere with the serverTime as a variable which is written to by the servlet.

    Thanks


Comments

  • Registered Users Posts: 1,990 ✭✭✭lynchie


    You are right in that outputting static html from servlets is not the best approach. Once you master the idea of servlets, the next step is to introduce MVC (Model View Controller). If you read up on JSP (Java Server Pages) you will see how the jsp acts as the static / dynamic page which can be fed parameters / data to/from a servlet and be used to populate the view.


  • Registered Users Posts: 6,111 ✭✭✭Talisman


    Servlets are the equivalent of CGI - displaying the date/time would be overkill for the technology. As a general rule servlets would be used where there is more program code required than HTML output.

    A Java Servlet Page (JSP) is similar to PHP/ASP where the page is primarily HTML code that contains tags for displaying dynamic content.

    The code for displaying the date/time in JSP would be as follows:
    <% java.util.Date now = new java.util.Date() %>
    The server time is <%= now %>
    


  • Registered Users Posts: 2,815 ✭✭✭SimonTemplar


    I'm trying to understand the relationship between servlets and jsp. At a very basic level, servlets is HTML in Java, and JSP is Java in HTML. To me, that would suggest that the two are opposing technologies instead of working together in the solution stack.

    Are we saying that a website that uses the servlet/JSP combination would never have the servlet outputting raw HTML via the println method?

    Talisman wrote: »
    Servlets are the equivalent of CGI - displaying the date/time would be overkill for the technology. As a general rule servlets would be used where there is more program code required than HTML output.

    A Java Servlet Page (JSP) is similar to PHP/ASP where the page is primarily HTML code that contains tags for displaying dynamic content.

    The code for displaying the date/time in JSP would be as follows:
    <% java.util.Date now = new java.util.Date() %>
    The server time is <%= now %>
    

    Thanks Talisman.

    I was only using serverTime as an example dynamic content versus static. It could have used a value from a db.


  • Registered Users Posts: 2,021 ✭✭✭ChRoMe


    Talisman wrote: »
    Servlets are the equivalent of CGI - displaying the date/time would be overkill for the technology. As a general rule servlets would be used where there is more program code required than HTML output.

    A Java Servlet Page (JSP) is similar to PHP/ASP where the page is primarily HTML code that contains tags for displaying dynamic content.

    The code for displaying the date/time in JSP would be as follows:
    <% java.util.Date now = new java.util.Date() %>
    The server time is <%= now %>
    

    For the love of god don't be telling people to use scriptlets in 2013.


  • Registered Users Posts: 6,111 ✭✭✭Talisman


    I'm trying to understand the relationship between servlets and jsp. At a very basic level, servlets is HTML in Java, and JSP is Java in HTML. To me, that would suggest that the two are opposing technologies instead of working together in the solution stack.

    Are we saying that a website that uses the servlet/JSP combination would never have the servlet outputting raw HTML via the println method?
    Consider a MVC application, this approach separates the business logic and data access layers from the presentation layer. The servlet receives a request which it processes and returns the data to the relevant JSP page to display.

    JSP and Servlet Tutorial: The Model View Controller (MVC) Architecture: Integrating Servlets and JSP


  • Advertisement
  • Registered Users Posts: 6,111 ✭✭✭Talisman


    ChRoMe wrote: »
    For the love of god don't be telling people to use scriptlets in 2013.
    My bad: it was a simple example to show the similarity to ASP/PHP.


  • Registered Users Posts: 870 ✭✭✭moycullen14


    I'm trying to understand the relationship between servlets and jsp. At a very basic level, servlets is HTML in Java, and JSP is Java in HTML. To me, that would suggest that the two are opposing technologies instead of working together in the solution stack.

    Are we saying that a website that uses the servlet/JSP combination would never have the servlet outputting raw HTML via the println method?



    Well, jsps ARE servlets. In fact the jsps get converted to Java Code and compiled by the Web Container (e.g. tomcat). So it's not a good idea to think of them as something different. Look on a jsp as a sort of front end to a servlet.

    Another way to look at it, is raw servlets and jsps perform different functions.
    The servlet is a 'controller' that sits between your data ('model') and produces a 'view' via a jsp. The goal is to have as little as possible HTML in a servlet and Java in a Jsp (separation of form and function) and have them hang together in a consistent way.

    Have a look at this:
    http://www.agiledeveloper.com/articles/JSPMVC.pdf

    I know it's struts and a bit old hat but if you want to understand the relationship between jsp, servlets & java then struts is a good place to start.


Advertisement