简体   繁体   English

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

[英]Why does my servlet request.getParameter return null

I am trying to run following code 我正在尝试运行以下代码

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"));


}`

but getParameter always null. 但是getParameter始终为null。

The only reason request.getParameter("Seat") will return you null is: The parameter "Seat" is not a part of request. request.getParameter("Seat")返回null的唯一原因是:参数"Seat"不是请求的一部分。 Since it a GET request it should be a part of query string. 由于它是GET请求,因此应成为查询字符串的一部分。 Also the parmeter "Seat" is case sensitive. 参数“ Seat”也区分大小写。

If your servlet URL is http://server.com/context/myservlet , then you will need to add the " Seat " parameter to it. 如果您的servlet URL是http://server.com/context/myservlet ,那么您将需要在其中添加“ Seat ”参数。 Here is how the toy URL should look like after adding the parameter: 添加参数后,玩具URL如下所示:

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

So you have to add the expression Seat=foo , were Seat is the parameter name and foo it's value. 因此,您必须添加表达式Seat=foo ,其中Seat是参数名称,而foo是值。

If you want to know more abot URL's check this page here: 如果您想了解更多的bott URL,请在这里检查此页面:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM