Skip to content

Commit

Permalink
Refactor some servlets
Browse files Browse the repository at this point in the history
  • Loading branch information
k-tamura committed Feb 15, 2017
1 parent e0cf9d3 commit 4d80b14
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,44 @@
public class IntegerOverflowServlet extends HttpServlet {

protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
int days = -1;
int hours = -1;
PrintWriter writer = null;
String errorMessage = "";
try {
Locale locale = req.getLocale();
StringBuilder bodyHtml = new StringBuilder();

bodyHtml.append("<form action=\"iof\" method=\"post\">");
bodyHtml.append("<input type=\"text\" name=\"days\" size=\"8\" maxlength=\"8\">");
bodyHtml.append(MessageUtils.getMsg("label.days", locale));
bodyHtml.append("<br>");
bodyHtml.append("<input type=\"submit\" value=\"" + MessageUtils.getMsg("label.convert", locale) + "\">");
bodyHtml.append("<br>");
if (req.getParameter("days") != null) {
int days = -1;
String strDays = req.getParameter("days");
if (strDays != null) {
try {
days = Integer.parseInt(req.getParameter("days"));
days = Integer.parseInt(strDays);
} catch (NumberFormatException e) {
// ignore
}
if (days >= 0) {
// days * 24 => hours
bodyHtml.append(days + " " + MessageUtils.getMsg("label.days", locale) + " = " + days * 24 + " "
+ MessageUtils.getMsg("label.hours", locale));
hours = days * 24;
} else {
bodyHtml.append("<font color=\"red\">" + MessageUtils.getMsg("msg.enter.positive.number", locale)
+ "</font>");
errorMessage = "<font color=\"red\">" + MessageUtils.getMsg("msg.enter.positive.number", locale)
+ "</font>";
}
}

StringBuilder bodyHtml = new StringBuilder();
bodyHtml.append("<form action=\"iof\" method=\"post\">");
if (days >= 0) {
bodyHtml.append("<input type=\"text\" name=\"days\" size=\"8\" maxlength=\"8\" value=" + strDays + ">");
} else {
bodyHtml.append("<input type=\"text\" name=\"days\" size=\"8\" maxlength=\"8\">");
}
bodyHtml.append(" * 24 = ");
if (days >= 0) {
bodyHtml.append(hours + " ");
}
bodyHtml.append("<br>");
bodyHtml.append("<br>");
bodyHtml.append("<input type=\"submit\" value=\"" + MessageUtils.getMsg("label.calculate", locale) + "\">");
bodyHtml.append("<br>");
bodyHtml.append(errorMessage);
bodyHtml.append("<br>");
bodyHtml.append(MessageUtils.getMsg("msg.note.positive.number",
new String[] { String.valueOf(Integer.MAX_VALUE / 24) }, locale));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,39 +21,48 @@ public class LossOfTrailingDigitsServlet extends HttpServlet {

protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
PrintWriter writer = null;
double number = -1;
String errorMessage = "";
double number = Double.NaN;
String strNumber = null;
String errorMessage = null;
try {
Locale locale = req.getLocale();
try {
strNumber = req.getParameter("number");
if(strNumber != null){
number = Double.parseDouble(strNumber);
}
} catch (NumberFormatException e) {
// ignore
}
if (Double.isNaN(number) || number <= -1 || number == 0 || 1 <= number) {
errorMessage = MessageUtils.getMsg("msg.enter.decimal.value", locale);
}

StringBuilder bodyHtml = new StringBuilder();

bodyHtml.append("<form action=\"lotd\" method=\"post\">");
bodyHtml.append("<input type=\"text\" name=\"number\" size=\"18\" maxlength=\"18\">");
if (!Double.isNaN(number) && errorMessage == null) {
bodyHtml.append("<input type=\"text\" name=\"number\" size=\"18\" maxlength=\"18\" value=" + strNumber
+ ">");
} else {
bodyHtml.append("<input type=\"text\" name=\"number\" size=\"18\" maxlength=\"18\">");
}
bodyHtml.append(" + 1 = ");
String strNumber = req.getParameter("number");
if (strNumber != null) {
try {
number = Double.parseDouble(strNumber);
} catch (NumberFormatException e) {
// ignore
}
if (-1 < number && number != 0 && number < 1) {
bodyHtml.append(String.valueOf(number + 1));
} else {
errorMessage = "<font color=\"red\">" + MessageUtils.getMsg("msg.enter.too.small.value", locale)
+ "</font>";
}
if (!Double.isNaN(number) && errorMessage == null) {
bodyHtml.append(String.valueOf(number + 1));
}
bodyHtml.append("<br>");
bodyHtml.append("<br>");
bodyHtml.append("<input type=\"submit\" value=\"" + MessageUtils.getMsg("label.calculate", locale) + "\">");
bodyHtml.append("<br>");
bodyHtml.append(errorMessage);
if (errorMessage != null && strNumber != null) {
bodyHtml.append("<font color=\"red\">" + errorMessage + "</font>");
}
bodyHtml.append("<br>");
bodyHtml.append(MessageUtils.getMsg("msg.note.enter.too.small.value", locale));
bodyHtml.append(MessageUtils.getMsg("msg.note.enter.decimal.value", locale));
bodyHtml.append("</form>");
HTTPResponseCreator.createSimpleResponse(res, MessageUtils.getMsg("title.loss.of.trailing.digits.page", locale),
bodyHtml.toString());
HTTPResponseCreator.createSimpleResponse(res,
MessageUtils.getMsg("title.loss.of.trailing.digits.page", locale), bodyHtml.toString());

} catch (Exception e) {
Logger.error(e);
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,10 @@ public class RoundOffErrorServlet extends HttpServlet {
protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
PrintWriter writer = null;
int number = -1;
double result = 0;
String errorMessage = "";
try {
Locale locale = req.getLocale();
StringBuilder bodyHtml = new StringBuilder();
bodyHtml.append("<form action=\"roe\" method=\"post\">");
bodyHtml.append("<input type=\"text\" name=\"number\" size=\"1\" maxlength=\"1\">");
bodyHtml.append(" - 0.9 = ");
String strNumber = req.getParameter("number");
if (strNumber != null) {
try {
Expand All @@ -37,12 +34,25 @@ protected void service(HttpServletRequest req, HttpServletResponse res) throws S
// ignore
}
if (1 <= number && number <= 9) {
bodyHtml.append(String.valueOf(number - 0.9));
result = number - 0.9;
} else {
errorMessage = "<font color=\"red\">" + MessageUtils.getMsg("msg.enter.positive.number", locale)
+ "</font>";
}
}

StringBuilder bodyHtml = new StringBuilder();
bodyHtml.append("<form action=\"roe\" method=\"post\">");
if (result != 0) {
bodyHtml.append("<input type=\"text\" name=\"number\" size=\"1\" maxlength=\"1\" value=" + strNumber + ">");
} else {
bodyHtml.append("<input type=\"text\" name=\"number\" size=\"1\" maxlength=\"1\">");
}
bodyHtml.append(" - 0.9 = ");
if (result != 0) {
bodyHtml.append(String.valueOf(result));
}
bodyHtml.append("<br>");
bodyHtml.append("<br>");
bodyHtml.append("<input type=\"submit\" value=\"" + MessageUtils.getMsg("label.calculate", locale) + "\">");
bodyHtml.append("<br>");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,11 @@ public class TruncationErrorServlet extends HttpServlet {
protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
PrintWriter writer = null;
double number = -1;
double result = 0;
String errorMessage = "";
try {
Locale locale = req.getLocale();
StringBuilder bodyHtml = new StringBuilder();

bodyHtml.append("<form action=\"te\" method=\"post\">");
bodyHtml.append("10.0 " + MessageUtils.getMsg("label.obelus", locale) + " ");
bodyHtml.append("<input type=\"text\" name=\"number\" size=\"1\" maxlength=\"1\">");
bodyHtml.append(" = ");

String strNumber = req.getParameter("number");
if (strNumber != null) {
try {
Expand All @@ -39,12 +35,26 @@ protected void service(HttpServletRequest req, HttpServletResponse res) throws S
// ignore
}
if (0 < number && number < 10) {
bodyHtml.append(String.valueOf(10.0 / number));
result = 10.0 / number;
} else {
errorMessage = "<font color=\"red\">" + MessageUtils.getMsg("msg.enter.positive.number", locale)
+ "</font>";
}
}

StringBuilder bodyHtml = new StringBuilder();
bodyHtml.append("<form action=\"te\" method=\"post\">");
bodyHtml.append("10.0 " + MessageUtils.getMsg("label.obelus", locale) + " ");
if (result != 0) {
bodyHtml.append("<input type=\"text\" name=\"number\" size=\"1\" maxlength=\"1\" value=" + strNumber + ">");
} else {
bodyHtml.append("<input type=\"text\" name=\"number\" size=\"1\" maxlength=\"1\">");
}
bodyHtml.append(" = ");
if (result != 0) {
bodyHtml.append(String.valueOf(result));
}
bodyHtml.append("<br>");
bodyHtml.append("<br>");
bodyHtml.append("<input type=\"submit\" value=\"" + MessageUtils.getMsg("label.calculate", locale) + "\">");
bodyHtml.append("<br>");
Expand Down
6 changes: 3 additions & 3 deletions src/main/resources/indexpage_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ function.description.memory.leak3=Memory leak occurs in C heap space after click
function.name.infinite.loop=Infinite Loop
function.description.infinite.loop=Infinite loop occurs after clicking this link.
function.name.dead.lock=Dead Lock (Java)
function.description.dead.lock=Dead lock (Java) occurs after loading the page few times.
function.description.dead.lock=Dead lock (Java) can occur.
function.name.dead.lock2=Dead Lock (SQL)
function.description.dead.lock2=Dead lock (SQL) occurs after loading the page few times.
function.description.dead.lock2=Dead lock (SQL) can occur.
function.name.endless.waiting.process=Endless Waiting Process
function.description.endless.waiting.process=Endless waiting process occurs after loading the page.
function.description.endless.waiting.process=Endless waiting process can occur.
function.name.jvm.crash.eav=JVM Crash
function.description.jvm.crash.eav=JVM crashes after clicking this link.
function.name.redirect.loop=Redirect Loop
Expand Down
6 changes: 3 additions & 3 deletions src/main/resources/indexpage_ja.properties
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ function.description.memory.leak3=\u3053\u306e\u30ea\u30f3\u30af\u3092\u30af\u30
function.name.infinite.loop=\u7121\u9650\u30eb\u30fc\u30d7
function.description.infinite.loop=\u3053\u306e\u30ea\u30f3\u30af\u3092\u30af\u30ea\u30c3\u30af\u3059\u308b\u3068\u3001\u7121\u9650\u30eb\u30fc\u30d7\u304c\u767a\u751f\u3057\u307e\u3059\u3002
function.name.dead.lock=\u30c7\u30c3\u30c9\u30ed\u30c3\u30af (Java)
function.description.dead.lock=\u30da\u30fc\u30b8\u3092\u6570\u56de\u30ed\u30fc\u30c9\u3059\u308b\u3068\u3001\u30c7\u30c3\u30c9\u30ed\u30c3\u30af(Java)\u304c\u767a\u751f\u3057\u307e\u3059\u3002
function.description.dead.lock=\u30c7\u30c3\u30c9\u30ed\u30c3\u30af(Java)\u3092\u767a\u751f\u3055\u305b\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002
function.name.dead.lock2=\u30c7\u30c3\u30c9\u30ed\u30c3\u30af (SQL)
function.description.dead.lock2=\u30da\u30fc\u30b8\u3092\u6570\u56de\u30ed\u30fc\u30c9\u3059\u308b\u3068\u3001\u30c7\u30c3\u30c9\u30ed\u30c3\u30af(SQL)\u304c\u767a\u751f\u3057\u307e\u3059\u3002
function.description.dead.lock2=\u30c7\u30c3\u30c9\u30ed\u30c3\u30af(SQL)\u3092\u767a\u751f\u3055\u305b\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002
function.name.endless.waiting.process=\u5b8c\u4e86\u3057\u306a\u3044\u30d7\u30ed\u30bb\u30b9\u306e\u5f85\u6a5f
function.description.endless.waiting.process=\u30da\u30fc\u30b8\u3092\u30ed\u30fc\u30c9\u3059\u308b\u3068\u3001\u5b8c\u4e86\u3057\u306a\u3044\u30d7\u30ed\u30bb\u30b9\u306e\u5f85\u6a5f\u304c\u767a\u751f\u3057\u307e\u3059\u3002
function.description.endless.waiting.process=\u5b8c\u4e86\u3057\u306a\u3044\u30d7\u30ed\u30bb\u30b9\u306e\u5f85\u6a5f\u3092\u767a\u751f\u3055\u305b\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002
function.name.jvm.crash.eav=JVM\u30af\u30e9\u30c3\u30b7\u30e5
function.description.jvm.crash.eav=\u3053\u306e\u30ea\u30f3\u30af\u3092\u30af\u30ea\u30c3\u30af\u3059\u308b\u3068JVM\u304c\u30af\u30e9\u30c3\u30b7\u30e5\u3057\u307e\u3059\u3002
function.name.redirect.loop=\u30ea\u30c0\u30a4\u30ec\u30af\u30c8\u30eb\u30fc\u30d7
Expand Down
7 changes: 2 additions & 5 deletions src/main/resources/messages_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ description.reverse.name=If you enter your name, then the reversed name is shown
label.asc=asc
label.calculate=calculate
label.character.count=Character Count
label.convert=Convert
label.days=days
label.desc=desc
label.hours=hours
label.name=Name
label.obelus=/
label.order=Order
Expand All @@ -24,7 +21,7 @@ msg.deadlock.occurs=A lock could not be obtained due to a deadlock.
msg.enter.name.and.passwd=If you enter your name and password, then your secret number is shown.
msg.enter.name=Please enter your name.
msg.enter.positive.number=Please enter a positive number.
msg.enter.too.small.value=Enter a decimal value.
msg.enter.decimal.value=Enter a decimal value.
msg.error.user.not.exist=User does not exist or password does not match.
msg.example.name.and.passwd=For example, Mark and password
msg.executed.batch=Created and executed the batch:
Expand All @@ -34,7 +31,7 @@ msg.java.heap.space.leak.occur=Memory leak occurs in Java heap space every time
<span class="glyphicon glyphicon-info-sign"></span>&nbsp; If keeping on loading this page, OutOfMemoryError is finally thrown.
msg.note.enter.one=Round off error occurs if you enter 1.
msg.note.enter.specific.nembers=Truncation error occurs if you enter 3 or 7 or 9.
msg.note.enter.too.small.value=Loss of trailing digits occurs if you enter 0.0000000000000001.
msg.note.enter.decimal.value=Loss of trailing digits occurs if you enter 0.0000000000000001.
msg.note.positive.number=Integer overflow occurs if you enter a large number greater than {0}.
msg.note.sql.deadlock=If you select "asc" and click the "update" button immediately after you select "desc" and click the "update" button, \
then dead lock occurs in database.
Expand Down
7 changes: 2 additions & 5 deletions src/main/resources/messages_ja.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ description.reverse.name=\u540d\u524d\u3092\u5165\u529b\u3059\u308b\u3068\u3001\
label.asc=\u6607\u9806
label.calculate=\u8a08\u7b97\u3059\u308b
label.character.count=\u6587\u5b57\u6570
label.convert=\u5909\u63db
label.days=\u65e5
label.desc=\u964d\u9806
label.hours=\u6642\u9593
label.name=\u540d\u524d
label.obelus
label.order=\u9806\u5e8f
Expand All @@ -24,7 +21,7 @@ msg.deadlock.occurs=\u30c7\u30c3\u30c9\u30ed\u30c3\u30af\u306b\u3088\u308a\u30ed
msg.enter.name.and.passwd=\u540d\u524d\u3068\u30d1\u30b9\u30ef\u30fc\u30c9\u3092\u5165\u529b\u3059\u308b\u3068\u3001\u6697\u8a3c\u756a\u53f7\u304c\u8868\u793a\u3055\u308c\u307e\u3059\u3002
msg.enter.name=\u540d\u524d\u3092\u5165\u529b\u3057\u3066\u4e0b\u3055\u3044\u3002
msg.enter.positive.number=\u6b63\u306e\u6574\u6570\u3092\u5165\u529b\u3057\u3066\u4e0b\u3055\u3044\u3002
msg.enter.too.small.value=\u5c0f\u6570\u3092\u5165\u529b\u3057\u3066\u4e0b\u3055\u3044\u3002
msg.enter.decimal.value=\u5c0f\u6570\u3092\u5165\u529b\u3057\u3066\u4e0b\u3055\u3044\u3002
msg.error.user.not.exist=\u30e6\u30fc\u30b6\u30fc\u304c\u5b58\u5728\u3057\u306a\u3044\u304b\u3001\u30d1\u30b9\u30ef\u30fc\u30c9\u304c\u4e00\u81f4\u3057\u307e\u305b\u3093\u3002
msg.example.name.and.passwd=\u4f8b\u3048\u3070\u3001Mark \u3068 password
msg.executed.batch=\u30d0\u30c3\u30c1\u3092\u4f5c\u6210\u3001\u5b9f\u884c\u3057\u307e\u3057\u305f:
Expand All @@ -35,7 +32,7 @@ msg.java.heap.space.leak.occur=\u3053\u306e\u30da\u30fc\u30b8\u3092\u8aad\u307f\
msg.note.enter.count=\u5927\u304d\u306a\u6587\u5b57\u6570\u3092\u5165\u529b\u3059\u308b\u3068\u3001\u5b8c\u4e86\u3057\u306a\u3044\u30d7\u30ed\u30bb\u30b9\u306e\u5f85\u6a5f\u304c\u767a\u751f\u3057\u307e\u3059\u3002
msg.note.enter.one=1\u3092\u5165\u529b\u3059\u308b\u3068\u3001\u4e38\u3081\u8aa4\u5dee\u304c\u767a\u751f\u3057\u307e\u3059\u3002
msg.note.enter.specific.nembers=3\u30017\u30019\u3092\u5165\u529b\u3059\u308b\u3068\u3001\u6253\u3061\u5207\u308a\u8aa4\u5dee\u304c\u767a\u751f\u3057\u307e\u3059\u3002
msg.note.enter.too.small.value=0.0000000000000001\u3092\u5165\u529b\u3059\u308b\u3068\u3001\u60c5\u5831\u6b20\u843d\u304c\u767a\u751f\u3059\u308b\u3053\u3068\u304c\u3042\u308a\u307e\u3059\u3002
msg.note.enter.decimal.value=0.0000000000000001\u3092\u5165\u529b\u3059\u308b\u3068\u3001\u60c5\u5831\u6b20\u843d\u304c\u767a\u751f\u3059\u308b\u3053\u3068\u304c\u3042\u308a\u307e\u3059\u3002
msg.note.positive.number={0}\u3088\u308a\u5927\u304d\u3044\u6570\u3092\u5165\u529b\u3059\u308b\u3068\u3001\u6574\u6570\u30aa\u30fc\u30d0\u30fc\u30d5\u30ed\u30fc\u304c\u767a\u751f\u3057\u307e\u3059\u3002
msg.note.sql.deadlock=\u300c\u964d\u9806\u300d\u3092\u9078\u629e\u3057\u3066\u300c\u66f4\u65b0\u300d\u30dc\u30bf\u30f3\u3092\u30af\u30ea\u30c3\u30af\u3057\u305f\u76f4\u5f8c\u306b\u300c\u6607\u9806\u300d\u3092\u9078\u629e\u3057\u3001\
\u300c\u66f4\u65b0\u300d\u30dc\u30bf\u30f3\u3092\u30af\u30ea\u30c3\u30af\u3059\u308b\u3068\u3001\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306b\u30c7\u30c3\u30c9\u30ed\u30c3\u30af\u304c\u767a\u751f\u3057\u307e\u3059\u3002
Expand Down

0 comments on commit 4d80b14

Please sign in to comment.