Tuesday, 26 April 2016

Java EE Servlets

Introduction

I've been taking a short break before moving on to new challenges. With this occasion I'm tidying several older personal projects. Among these, my Haiku Gallery.

I resumed the review from watching again "Intro to Java. Unit 14. Intro to Java EE. GlassFish. Servlets." by Yakov Fain:



After reading Lesson 26 of Java Programming 24-Hour Trainer I wrote the vote servlet, adapted the client side of my application, packed everything in a WAR file using Eclipse IDE, and uploaded the archive to the Azure Cloud. As illustrated below.

The Client Side of VoteMyHaiku App

<div>
<h2>Windy Afternoon</h2>
<p>Windy afternoon</p>
<p>Your smile</p>
<p>On a stranger's face</p>
<form action=http://votemyhaiku.azurewebsites.net/VoteMyHaiku/vote method=Get>
<input type=Submit name="smile" value="Like" class="my_button"/>
</form>
</div>

The Servlet

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
String button1 = request.getParameter("smile");
String button2 = request.getParameter("wait");
if (button1 != null) {
out.println("<html><body>");
out.println("<h1>Smile!!!</h1>");
out.println("</body></html>");
}
else if (button2 != null){
out.println("<html><body>");
out.println("<h1>Wait!</h1>");
out.println("</body></html>");
}
}

Vote My Haiku in the Azure Cloud:

http://votemyhaiku.azurewebsites.net/

Conclusions

1. I love Visual Studio Online "Monaco"!
2. Adam Bien is right: Java EE is easier than Javascript.

Homework (to self)

1. Warming up: Respond with beautiful HTML from the Vote Server Servlet.
2. Advanced: Persist votes in an SQL database. Re-order haiku pieces dynamically, based on voting results, and create a 'Top 3' section on the page.

No comments: