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

时间:2016-02-23 22:30:13

标签: java jsp servlets

我遇到了请求问题。它总是返回" null",但我不知道为什么。我想让它返回一个名字。

这是我的servlet:

public class MinServlet extends HttpServlet {


public void doGet(HttpServletRequest request, 
                  HttpServletResponse response) throws IOException
{
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    out.println("<html>");
    out.println("<head><title>Syvtabellen - fra en servlet</title></head>");
    out.println("<body>");
    out.println("<p>Her er syv-tabellen:<br>");

    for (int i=1; i<=10; i++)
    {
        out.println("Syv gange "+ i +" er: "+ 7*i +".<br>");
    } 
    out.println("</body>");
    out.println("</html>");
    String parameterværdi = request.getParameter("navn");
    out.print( "Værdien af parameteren 'navn' er: <br>" + parameterværdi );
}

}

这是index.xml:

<html>
<head>
    <title>TODO supply a title</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
    <div>TODO write content</div>
</body>

这是web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<servlet>
    <servlet-name>MinServlet</servlet-name>
    <servlet-class>konti.MinServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>MinServlet</servlet-name>
    <url-pattern>/MinServlet</url-pattern>
</servlet-mapping>
<session-config>
    <session-timeout>
        30
    </session-timeout>
</session-config>

我是否会以某种方式将其添加到index.xml中?我知道如果参数不存在,参数将返回null,但我不知道如何修复它:)

1 个答案:

答案 0 :(得分:2)

我相信你正在关注一个教程。正如Elliott在评论中所说,你需要一个名为&#34; navn&#34;在您查看从您的servlet捕获它否则您将获得null。否则应该有一个名为&#34; navn&#34;的查询字符串。在这里,我看不到任何名为&#34; navn&#34;在你的客户端。

例如:http://www.java4s.com/java-servlet-tutorials/example-of-request-getparameter-retrieve-parameters-from-html-form/

根据你的代码: 的index.html

<font face="verdana" size="2px">
       <form action="getVal" method="post">
           First way to pass request Param <input type="text" name="navn"><br>
           <input type="submit" value="Submit">
      </form>
</font>

TestApp.java

public class TestApp extends HttpServlet  
{
    protected void doPost(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException
    {
        PrintWriter pw=res.getWriter();
        res.setContentType("text/html");

        String n1=req.getParameter("navn");
        pw.println("Requested  Value" +n1);
        pw.close();

    }

}

的web.xml

<web-app>

    <servlet>
       <servlet-name>sumOfTwoNumbers</servlet-name>
       <servlet-class>java4s.OngetParameter</servlet-class>
    </servlet>

    <servlet-mapping>
       <servlet-name>TestApp</servlet-name>
       <url-pattern>/getVal</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
       <welcome-file>index.html</welcome-file>
    </welcome-file-list>

</web-app>

否则您可以按以下方式传递查询字符串

传递Request param的第二种方法: 的index.html Click here