J2EE与中间件 实践实记

来源:互联网 发布:河南省数据统计 编辑:程序博客网 时间:2024/06/03 18:28

2017年12月19日23:44:30

遇到了405问题

HTTP method POST is not supported by this URL错误的解决方案

这个说的就是,在doPost和doGet方法中,不要加super.doGet()/super.doPost()

累觉不爱


2017年12月20日16:50:50

问题:所有的错误在eclipse里打开都是默认的界面,而不是我设置了ErrorPage之后的界面。

解决方案:使用浏览器打开就好了。我也不知道为什么。

另外,对于错误处理的代码,可能会遇到error和exception同时发生的情况。我写的代码如下,写的不好,但是希望各位能注意一下这么坑爹的一点。。。

    // 处理 GET 方法请求的方法    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        System.out.println("ErrorHandler------Get");        System.out.println("被调用了错误处理代码");        Throwable throwable = (Throwable) request.getAttribute("javax.servlet.error.exception");        Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");        String servletName = (String) request.getAttribute("javax.servlet.error.servlet_name");        if (servletName == null) {            servletName = "Unknown";        }        String requestUri = (String) request.getAttribute("javax.servlet.error.request_uri");        if (requestUri == null) {            requestUri = "Unknown";        }        // 设置响应内容类型        response.setContentType("text/html;charset=UTF-8");        PrintWriter out = response.getWriter();        String title = "错误/异常 信息";        String docType = "<!DOCTYPE html>\n";        out.println(                docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" + "<body bgcolor=\"#f0f0f0\">\n");        out.println("<h1>错误/异常信息</h1>");        if (throwable == null && statusCode == null) {            System.out.println("错误信息丢失");            out.println("<h2>错误信息丢失</h2>");            out.println("请返回 <a href=\"" + response.encodeURL("http://localhost:8080/SmallHomework2/index.html")                    + "\">主页</a>。");        } else if (statusCode != null && throwable != null) {            System.out.println("存在错误, 错误代码为 " + statusCode);            System.out.println("存在异常,异常类型为 " + throwable.getClass().getName());            out.println("<h2>异常信息</h2>");            out.println("错误代码 : " + statusCode);            out.println("Servlet Name : " + servletName + "</br></br>");            out.println("异常类型 : " + throwable.getClass().getName() + "</br></br>");            out.println("请求 URI: " + requestUri + "<br><br>");            out.println("异常信息: " + throwable.getMessage());        } else {            System.out.println("存在错误, 错误代码为 " + statusCode);            out.println("<h2>异常信息</h2>");            out.println("错误代码 : " + statusCode);        }        out.println("</body>");        out.println("</html>");    }

2017年12月20日18:30:08

配置JNDI

问题:不知道怎么配置

解决方案:官方文档

但是光看官方那个文档解决不了我的所有问题,所以我又去找别的资料。

参考资料

注意里面的这么一句:

NOTES:

  • If you are using Tomcat inside Eclipse IDE, you need to modify the context.xml file under the Servers project. That is because Eclipse made a copy of Tomcat configuration:

参考资料2

只能说,还是要多去实验实验,你才能成功,我最后按照这三份资料,终于成功了。不要听别人说的全局变量不行,先不管,你先搞起来再说。。。

原创粉丝点击