在servlet中,getParameter返回null

3
我正在尝试通过 href 将字符串从我的 JSP 传递到 Servlet。我使用以下代码:
<a href="MyServlet?select=title1"> Title1 </a>

然后在servlet中,我尝试使用以下代码获取select的值:

String s = request.getParameter("select");

但它返回了 null。为什么呢?谢谢!

编辑: 我发布了servlet的代码

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        String userOption = request.getAttribute("select");
        System.out.println("Select is:" + userOption);
        //in the console i get: Select is:null

你的Servlet被执行了吗? - Diogo Moreira
你的Servlet被正确调用了吗?请展示URL模式和你的Servlet方法。 - Prasad Kharkar
你确定你已经将这段代码放在了doGet方法中吗?因为任何链接都会导致调用doGet方法。 - Prasad Kharkar
是的,servlet 正常执行。我正在尝试获取以下代码块中的值: protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - yaylitzis
please post your full code - Prasad Kharkar
好的,我已经在问题@PrasadKharkar中发布了它。 - yaylitzis
1个回答

0

这不应该是 String userOption = request.getAttribute("select");

请理解属性不是参数

应该是 String userOption = request.getParameter("select");


好的,那真是我太傻了...我写成了getAttribute(),但我以为我写成了getParameter()...不管怎样,还是谢谢你! - yaylitzis
不用担心...我也遇到过这种情况 :) - Prasad Kharkar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接