为什么我的servlet request.getParameter返回null

时间:2018-05-16 10:01:41

标签: java servlets

我正在尝试运行以下代码

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub

    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    int i = 1, j = 1;
    String title = "Web Movie";
    out.println("<html>");
    out.println("<head>");
    out.println("<title>" + title + "</title>");
    out.println("</head>");
    out.println("<body");
    out.println("<h1>Seat Information</h1>");
    out.println("<center>");
    out.println("<form method='get' action='Theatre'> ");
    out.println("<table border=1 cellpadding=0 cellspacing=0 width=70%>");
    out.println("<tr>");
    out.println("<th>Row</th>");
    out.println("<th colspan = 8>Seat</th>");
    out.println("</tr>");
    //Table hien thi thong tin ghe
    for(i = 0; i < 8; i++) {
        out.println("<tr>");
        out.println("<td style='text-align:center;'>" + (char)(i + 'A') + "</td>"); 
        for(j = 1; j < 9; j++) {                    
            out.println("<td  id = " + (char)(i + 'A') + j + " onclick='showResult(this.id);' style='text-align:center;'>" +  j + "</td>");      
        }   
        out.println("</tr>");   
    }
    out.println("</table>");
    out.println("</form>");
    out.println("<p>Vi tri ngoi:</p>");
    out.println("<input id = 'text2' name='Seat'><br>");
    out.println("<script>");
    out.println("function showResult(clicked_id){"); 
    out.println("document.getElementById('text2').value = clicked_id"); 
    out.println("}");
    out.println("</script>"); 
    out.println("</body>");
    out.println("</html>");
    out.println(request.getParameter("Seat"));


}`

但getParameter始终为null。

2 个答案:

答案 0 :(得分:0)

request.getParameter("Seat")返回null的唯一原因是:参数"Seat"不是请求的一部分。由于它是一个GET请求,它应该是查询字符串的一部分。参数“Seat”也区分大小写。

答案 1 :(得分:0)

如果您的servlet网址为http://server.com/context/myservlet,则需要添加&#34; Seat&#34;它的参数。以下是添加参数后玩具URL的外观:

http://server.com/context/myservlet?Seat=foo

所以你必须添加表达式Seat=fooSeat是参数名称,foo是它的值。

如果您想了解更多abot网址,请点击此处查看此页:

https://developer.mozilla.org/en-US/docs/Learn/Common_questions/What_is_a_URL