Utils.java 717 B

12345678910111213141516171819202122232425262728293031323334
  1. package hello;
  2. import javax.servlet.http.HttpServletRequest;
  3. import java.io.IOException;
  4. abstract class Utils {
  5. private static final ThreadLocal<Context> threadContext = new ThreadLocal<Context>() {
  6. @Override
  7. protected Context initialValue() {
  8. return new Context();
  9. }
  10. };
  11. static Context getContext() throws IOException {
  12. Context ctx = threadContext.get();
  13. ctx.json.reset();
  14. return ctx;
  15. }
  16. public static int parseBoundParam(HttpServletRequest request) {
  17. int count = 1;
  18. try {
  19. count = Integer.parseInt(request.getParameter("queries"));
  20. if (count > 500) {
  21. count = 500;
  22. } else if (count < 1) {
  23. count = 1;
  24. }
  25. } catch (NumberFormatException ignore) {
  26. }
  27. return count;
  28. }
  29. }