I am attempting to build a world clock, to display GMT, PST etc.
The only problem I'm having is that when the user types nothing in the text field, it's not going into the if statement that only executes when the user doesn't enter anything and displays the local time. I have provided all my code to see what the problem is.
html code:
<body>
<form action="WorldClockWebApp" method="post">
<table>
<tr>
<td>Enter Time Zone:</td>
<td><input type="text" name="timezone" /></td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td><input type="submit" value="Submit" /></td>
</tr>
</table>
</form>
</body>
servlet code:
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String input = request.getParameter("timezone");
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("HH:mm:ss");
ZoneId z;
boolean valid = true;
TimeZone t = null;
String[] ids = TimeZone.getAvailableIDs();
if (input != "") // If the user enters in a timezone.
{
for (String id : ids) {
if (input.equalsIgnoreCase(id))
t = TimeZone.getTimeZone(id);
}
}
if (t == null)
response.getWriter().append("<h1>What's the time Mr Wolf?</h1>")
.append("<p>No time provided, but the local time is " + t.getDisplayName().toString() + " is:</p>")
.append("<h2>" + LocalTime.now(t.toZoneId()).format(dtf) + "</h2>")
.append("<form action=\"/project4/WorldClock.html\">" + "<button type=\"submit\">Go Back</button>"
+ "</form>"); // Button gives user option to go back.
else
response.getWriter().append("<h1>What's the time Mr Wolf?</h1>")
.append("<p>The current time in " + t.getDisplayName().toString() + " is:</p>")
.append("<h2>" + LocalTime.now(t.toZoneId()).format(dtf) + "</h2>")
.append("<form action=\"/project4/WorldClock.html\">" + "<button type=\"submit\">Go Back</button>"
+ "</form>"); // Button gives user option to go back.
}