javaee,网页一直显示400错误 5C

代码:
package servletDataPass.url;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//@WebServlet(name = "Top10Servlet", urlPatterns = { "/top10" })
public class Top10Servlet extends HttpServlet {
private static final long serialVersionUID = 987654321L;

private List<String> chengduAttractions;
private List<String> yichangAttractions;
private List<String> beijingAttractions;

@Override
public void init() throws ServletException {
    chengduAttractions = new ArrayList<String>(10);
    chengduAttractions.add("武侯祠");
    chengduAttractions.add("杜甫草堂");
    chengduAttractions.add("人民公园");
    chengduAttractions.add("天府广场");
    chengduAttractions.add("熊猫基地");
    chengduAttractions.add("动物园");
    chengduAttractions.add("洋海公园");
    chengduAttractions.add("望江公园");
    chengduAttractions.add("双流湿地公园");
    chengduAttractions.add("南湖");

    yichangAttractions = new ArrayList<String>(10);
    yichangAttractions.add("长江大桥");
    yichangAttractions.add("滨江公园");
    yichangAttractions.add("五一广场");
    yichangAttractions.add("夷陵广场");
    yichangAttractions.add("长江画廊");
    yichangAttractions.add("镇江阁");
    yichangAttractions.add("葛洲坝");
    yichangAttractions.add("清江画廊");
    yichangAttractions.add("东山公园");
    yichangAttractions.add("儿童公园");

    beijingAttractions = new ArrayList<String>(10);
    beijingAttractions.add("天安门");
    beijingAttractions.add("毛主席纪念堂");
    beijingAttractions.add("天坛");
    beijingAttractions.add("故宫");
    beijingAttractions.add("永定门");
    beijingAttractions.add("西直门");
    beijingAttractions.add("园明园");
    beijingAttractions.add("八达岭");
    beijingAttractions.add("北京西站");
    beijingAttractions.add("南宛机场");
}

@Override
public void doGet(
        HttpServletRequest request,
        HttpServletResponse response) throws ServletException,
        IOException {
    request.setCharacterEncoding("utf-8");
    String city = request.getParameter("city");
    System.out.println(city);
    if (city != null && 
            (city.equals("成都") 
                    || city.equals("宜昌")
                    || city.equals("北京"))) {
        // show attractions
        showAttractions(request, response, city);
    } else {
        // show main page
        showMainPage(request, response);
    }
}

private void showMainPage(
        HttpServletRequest request,
        HttpServletResponse response) throws ServletException,
        IOException {
    response.setContentType("text/html;charset=utf-8");
    PrintWriter writer = response.getWriter();
    writer.print("<html><head>" +
            "<title>十佳旅游景点</title>" +
            "</head><body>" + 
            "请选择一个城市:" +
            "<br/><a href='?city=成都'>成都</a>" +
            "<br/><a href='?city=宜昌'>宜昌</a>" +
            "<br/><a href='?city=北京'>北京</a>" +
            "<hr/>学生任务:"
            + "<br/>请选一个自己感兴趣的城市,列举十个地点,加入到页面中"
            + "</body></html>");
}

private void showAttractions(
        HttpServletRequest request,
        HttpServletResponse response, String city) 
        throws ServletException, IOException {

    response.setContentType("text/html;charset=utf-8");
    int page = 1;
    String pageParameter = request.getParameter("page");
    if (pageParameter != null) {
        try {
            page = Integer.parseInt(pageParameter);
        } catch (NumberFormatException e) {
            // do nothing and retain default value for page
        }
        if (page > 2) {
            page = 1;
        }            
    }
    List<String> attractions = null;
    if (city.equals("成都")) {
        attractions = chengduAttractions;
    } else if (city.equals("宜昌")) {
        attractions = yichangAttractions;
    } else if (city.equals("北京")) {
        attractions = beijingAttractions;
    }
    response.setContentType("text/html");
    PrintWriter writer = response.getWriter();
    writer.println("<html><head>" +
            "<title>十佳旅游景点</title>" +
            "</head><body>");
    writer.println("<a href='top10'>选择城市</a> ");
    writer.println("<hr/>Page " + page + "<hr/>");

    int start = page * 5 - 5;
    for (int i = start; i < start + 5; i++) {
        writer.println(attractions.get(i) + "<br/>");
    }
    writer.print("<hr style='color:blue'/>" +
            "<a href='?city=" + city + 
            "&page=1'>Page 1</a>");
    writer.println("&nbsp; <a href='?city=" + city + 
            "&page=2'>Page 2</a>");
    writer.println("</body></html>");
}

}
图片说明

1个回答

tomcat8.0以上版本遵从RFC规范添加了对Url的特殊字符的限制,url中只允许包含英文字母(a-zA-Z)、数字(0-9)、-_.~四个特殊字符以及保留字符( ! * ’ ( ) ; : @ & = + $ , / ? # [ ] ) (26*2+10+4+18=84)这84个字符,请求中出现了{}大括号或者[],所以tomcat报错。设置RelaxedQueryChars允许此字符(建议),设置requestTargetAllows选项(Tomcat 8.5中不推荐)。您可以降级为旧版本之一(不推荐-安全性)。根据 https://tomcat.apache.org/tomcat-8.5-doc/config/systemprops.html, requestTargetAllowis设置允许不受欢迎字符。对我来说,这里提出的其他解决办法也不起作用。根据Tomcat文档,我找到了一种方法来设置松弛的QueryChars属性

解决方法:
1、去除URL中的特殊字符;

3、使用 Post 方法提交数据

4、更换低版本的Tomcat来规避这种问题。

5、在 conf/catalina.properties 添加或者修改:

  5.1 添加 tomcat.util.http.parser.HttpParser.requestTargetAllow=|{}

5.2 修改tomcat/conf/catalina.properties的配置文件
Tomcat在 7.0.73, 8.0.39, 8.5.7 版本后,添加了对于http头的验证。
具体来说,就是添加了些规则去限制HTTP头的规范性
org.apache.tomcat.util.http.parser.HttpParser#IS_NOT_REQUEST_TARGET[]中定义了一堆not request target
if(IS_CONTROL[i] || i > 127 || i == 32 || i == 34 || i == 35 || i == 60 || i == 62 || i == 92 || i == 94 || i == 96 || i == 123 || i == 124 || i == 125) {
IS_NOT_REQUEST_TARGET[i] = true;
}
转换过来就是以下字符(对应10进制ASCII看):
键盘上那些控制键:( 非英文字符(>127)
空格(32)
双引号(34)
#(35)
<(60)

(62)
反斜杠(92)
^(94)
TAB上面那个键,我也不晓得嫩个读(96)
{(123)
}(124)
|(125)

重启服务器后,解决问题。

https://www.cnblogs.com/dygrkf/p/9088370.html

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问