packagehi;importhi.servlet;importhi.route;importjava.util.ArrayList;importjava.util.Arrays;importjava.util.HashMap;importjava.util.regex.Matcher;public class jdemo implementshi.servlet {private static hi.route r =hi.route.get_instance();publicjdemo() {
jdemo.r.get("^/(hello|test)/?$", (hi.request req, hi.response res, Matcher m) ->{this.do_hello(req, res);
});
jdemo.r.get("^/error/?$", (hi.request req, hi.response res, Matcher m) ->{this.do_error(req, res);
});
jdemo.r.get("^/redirect/?$", (hi.request req, hi.response res, Matcher m) ->{this.do_redirect(req, res);
});
jdemo.r.add(new ArrayList(Arrays.asList("GET", "POST")), "^/form/?$",
(hi.request req, hi.response res, Matcher m)->{this.do_form(req, res);
});
jdemo.r.get("^/session/?$", (hi.request req, hi.response res, Matcher m) ->{this.do_session(req, res);
});
jdemo.r.get("^/md5/?$", (hi.request req, hi.response res, Matcher m) ->{this.do_md5(req, res);
});
}public voidhandler(hi.request req, hi.response res) {
jdemo.r.run(req, res);
}private voiddo_hello(hi.request req, hi.response res) {
res.headers.get("Content-Type").set(0, "text/plain;charset=UTF-8");
res.status= 200;
res.content= "hello,world";
}private voiddo_error(hi.request req, hi.response res) {
res.headers.get("Content-Type").set(0, "text/plain;charset=UTF-8");
res.status= 404;
res.content= "404 Not found";
}private voiddo_redirect(hi.request req, hi.response res) {
res.status= 302;
ArrayList h = new ArrayList();
h.add("/hello.java");
res.headers.put("Location", h);
}private voiddo_form(hi.request req, hi.response res) {
res.headers.get("Content-Type").set(0, "text/plain;charset=UTF-8");
res.status= 200;
StringBuilder buffer= newStringBuilder();
buffer.append("head data " + req.headers.size() + "n");
buffer.append(this.do_foreach(req.headers));
buffer.append("ncookie data " + req.cookies.size() + "n");
buffer.append(this.do_foreach(req.cookies));
buffer.append("nform data " + req.form.size() + "n");
buffer.append(this.do_foreach(req.form));
buffer.append(String.format("nclient= %snmethod= %snuser_agent= %snuri= %snparam= %sn", req.client,
req.method, req.user_agent, req.uri, req.param));
res.content=buffer.toString();
}private voiddo_session(hi.request req, hi.response res) {
res.headers.get("Content-Type").set(0, "text/plain;charset=UTF-8");
res.status= 200;
String key= "test";int value = 0;if(req.session.containsKey(key)) {
value= Integer.parseInt(req.session.get(key)) + 1;
}
res.session.put(key, String.valueOf(value));
res.content= String.format("hello,%d", value);
res.status= 200;
}private voiddo_md5(hi.request req, hi.response res) {
res.headers.get("Content-Type").set(0, "text/plain;charset=UTF-8");
String plaintext= "hello,md5!";
res.status= 200;
res.content= String.format("%snmd5= %s", plaintext, this.md5(plaintext));
}privateString md5(String str) {try{
java.security.MessageDigest md= java.security.MessageDigest.getInstance("MD5");byte[] array =md.digest(str.getBytes());
StringBuffer sb= newStringBuffer();for (int i = 0; i < array.length; ++i) {
sb.append(Integer.toHexString((array[i]& 0xFF) | 0x100).substring(1, 3));
}returnsb.toString();
}catch(java.security.NoSuchAlgorithmException e) {
}return null;
}private String do_foreach(HashMapm) {
StringBuffer buffer= newStringBuffer();for (HashMap.Entryitem : m.entrySet()) {
buffer.append(String.format("%st=t%sn", item.getKey(), item.getValue()));
}returnbuffer.toString();
}
}