-1

我对 servlet 有点陌生,我正在使用NetBeans,我有一个 servlet,它接受来自用户的数字并打印它的乘法表。
我有TableServlet.java如下-->

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class TableServlet extends HttpServlet
{
    @Override
    public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
    {
        PrintWriter out=response.getWriter();
        response.setContentType("text/html");
        String s=request.getParameter("t1");
        int x= Integer.parseInt(s);
        for(int i=1;i<=10;i++)
        {
            out.println(x+"x"+i+"="+i*x);
        }
        out.close();
    }    
}

我有newhtml.html如下-->

<html>
    <body>
        <form action="TableServlet" method="get">
            Enter any number
            <input type="text" name="t1" />
            <input type="submit" value="GetTable"/>
        </form>
    </body>
</html>

它已成功部署,但运行此项目后在浏览器上找不到输出。相反,我只能在浏览器上看到这个-->

HTTP Status 500 -

type Exception report

message

descriptionThe server encountered an internal error () that prevented it from fulfilling this request.

exception

java.lang.NumberFormatException: null
note The full stack traces of the exception and its root causes are available in the GlassFish Server Open Source Edition 3.1.2.2 logs.

GlassFish Server Open Source Edition 3.1.2.2

可能是什么原因 ?

4

1 回答 1

3

The exception is most likely thrown by this line:

int x = Integer.parseInt(s);

and the cause is that the value of s is not a valid integer.

Indeed, this message:

java.lang.NumberFormatException: null

tells us that the value of s is null! And that implies that the request being processed does not have a "t1" parameter. I can't immediately see what is causing that, but the prime suspect is the HTML form.

Here are a couple of ideas:

  • Try composing a request URL in your browser's URL bar.
  • Use your browser's web debugger to see what request URL is being sent to the server when you use your form.
于 2013-06-29T15:29:38.947 回答