Java Servlets — Single Page Website

So I want to write Java Servlets for my moe.bot. It only needs dynamic content and requires no page refreshes. I want it to run fast and have as little overhead as possible.

To start I’ll refresh with this tutorial.

 

// Import required java libraries
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

// Extend HttpServlet class
public class HelloWorld extends HttpServlet {
 
  private String message;

  public void init() throws ServletException
  {
      // Do required initialization
      message = "Hello World";
  }

  public void doGet(HttpServletRequest request,
                    HttpServletResponse response)
            throws ServletException, IOException
  {
      // Set response content type
      response.setContentType("text/html");

      // Actual logic goes here.
      PrintWriter out = response.getWriter();
      out.println("<h1>" + message + "</h1>");
  }
  
  public void destroy()
  {
      // do nothing.
  }
}

So what is the life-cycle of this simple servlet?

  • init() starts the servlet
  • service() gets called by the servlet container
  • doGet() or doPost() gets executed by the servlet
  • The default servlet calls processRequest()
  • destroy() frees up the resources for the servlet if needed.

Here’s the pdf from 347 C5-JavaServlets

Annotations take care of much of the url patterns

@Webservlet(name = “PlayShowServlet” , urlPattern = { “/justListen”, “/listenAndRate”}
public class TimeServlet extends HTTPServlet{
  public void doGet(HTTPServletRequest request, HTTPServletResponse, response){
    PrintWriter out = response.getWrite();
    response.setContentType(“text/html”);
    out.println(“<html><head><title>Title</title> …..”);
    out.close();
}}

Now that I have a servlet returning data, I need to return all of the information needed. Since there is so much information, I am going to automate the process and return the entire show in JSON format. This is very much like what is in the archive database. Here is the first tutorial I found for Java to JSON conversion. I may just do it manually.

Jackson 2 – Convert Java Object to / from JSON

@Test
 public void testSongParse() throws IOException {
 ObjectMapper mapper = new ObjectMapper();
 Songs obj = OutgoingData.retrieveSongs();
 
 //Object to JSON in file
 mapper.writeValue(new File("jsonOut.txt"), obj);

//Object to JSON in String
 String jsonInString = mapper.writeValueAsString(obj);
 System.out.println(jsonInString);
 
 // Convert object to JSON string and pretty print
 jsonInString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
 System.out.println(jsonInString);
 }

This works fantastic for POJO’s. But will it work for Collections of Objects? I have a set of Songs associated with a parent Show object.

I’m honestly amazed! It works perfectly. It recognized the set of Songs and made it into an array of Songs with all the needed fields.

{"id":1,
 "venue":"Governors Island",
 "showUrl":"moe2008-06-15.AT4041",
 "showName":"moe. Live at Governors Island on 2008-06-15",
 "date":null,
 "score":0,
 "setList":[
 {"id":2,"showId":1,"name":"moe2008-06-15d1t-01.flac","mtime":"1231297224","source":"original","format":"Flac","length":"27.56","track":"01","title":"crowd/tuning-","album":"2008-06-15 - Governors Island","score":0,"show":null},
 {"id":7,"showId":1,"name":"moe2008-06-15d1t-02.flac","mtime":"1231297504","source":"original","format":"Flac","length":"303.1","track":"02","title":"Tailspin","album":"2008-06-15 - Governors Island","score":0,"show":null},
 {"id":3,"showId":1,"name":"moe2008-06-15d1t-03.flac","mtime":"1231298215","source":"original","format":"Flac","length":"590.22","track":"03","title":"Darkness>","album":"2008-06-15 - Governors Island","score":0,"show":null},
 {"id":4,"showId":1,"name":"moe2008-06-15d1t-04.flac","mtime":"1231299060","source":"original","format":"Flac","length":"648.01","track":"04","title":"Bring You Down","album":"2008-06-15 - Governors Island","score":0,"show":null},
 {"id":6,"showId":1,"name":"moe2008-06-15d1t-05.flac","mtime":"1231299460","source":"original","format":"Flac","length":"357.33","track":"05","title":"Tambourine","album":"2008-06-15 - Governors Island","score":0,"show":null},
 {"id":5,"showId":1,"name":"moe2008-06-15d1t-06.flac","mtime":"1231300713","source":"original","format":"Flac","length":"922.56","track":"06","title":"Rise*>","album":"2008-06-15 - Governors Island","score":0,"show":null},
 {"id":1,"showId":1,"name":"moe2008-06-15d1t-07.flac","mtime":"1231301493","source":"original","format":"Flac","length":"732.15","track":"07","title":"Happy Hour Hero*","album":"2008-06-15 - Governors Island","score":0,"show":null}
 ]
 }

Now that I have the data I need to use it in my view.

I also found this for getting the session factory going.