为了验证BinCat是否真的具备了Servlet处理能力,我们写两个测试用例:TestServletCMDServlet
    TestServlet示例代码:

    1. package com.anbai.sec.server.test.servlet;
    2. import javax.servlet.annotation.WebServlet;
    3. import javax.servlet.http.HttpServlet;
    4. import javax.servlet.http.HttpServletRequest;
    5. import javax.servlet.http.HttpServletResponse;
    6. import java.io.IOException;
    7. import java.io.OutputStream;
    8. @WebServlet(name = "TestServlet", urlPatterns = "/TestServlet/")
    9. public class TestServlet extends HttpServlet {
    10. @Override
    11. public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    12. doPost(request, response);
    13. }
    14. @Override
    15. public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
    16. OutputStream out = response.getOutputStream();
    17. out.write(("Hello....<br/>Request Method:" + request.getMethod() + "<br/>Class:" + this.getClass()).getBytes());
    18. }
    19. }

    浏览器请求http://localhost:8080/TestServlet/:
    7. Servlet功能测试 - 图1
    CMDServlet示例代码:

    1. package com.anbai.sec.server.test.servlet;
    2. import org.javaweb.utils.IOUtils;
    3. import javax.servlet.annotation.WebServlet;
    4. import javax.servlet.http.HttpServlet;
    5. import javax.servlet.http.HttpServletRequest;
    6. import javax.servlet.http.HttpServletResponse;
    7. import java.io.IOException;
    8. import java.io.OutputStream;
    9. @WebServlet(name = "CMDServlet", urlPatterns = "/CMD/")
    10. public class CMDServlet extends HttpServlet {
    11. @Override
    12. public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    13. doPost(request, response);
    14. }
    15. @Override
    16. public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
    17. String cmd = request.getParameter("cmd");
    18. byte[] bytes = IOUtils.toByteArray(Runtime.getRuntime().exec(cmd).getInputStream());
    19. OutputStream out = response.getOutputStream();
    20. out.write(bytes);
    21. out.flush();
    22. out.close();
    23. }
    24. }

    浏览器请求http://localhost:8080/CMD/?cmd=whoami:
    7. Servlet功能测试 - 图2
    使用curl发送POST请求:curl -i localhost:8080/CMD/ -d "cmd=pwd",服务器可以正常接收POST参数,处理结果如图:
    7. Servlet功能测试 - 图3
    请求一个错误服务:
    7. Servlet功能测试 - 图4
    至此,我们已经实现了一个非常初级的Servlet容器了。
    Back