【问题标题】:JSP Cannot get value from inputJSP 无法从输入中获取值
【发布时间】:2014-01-16 01:08:50
【问题描述】:

编辑:现在可以使用!谢谢!所以锚不是提交。

为什么我无法从使用 doGet/doPost 的输入中获取 'opr' 的值?我得到了一些空值。

表格:

<form method="post" role="form" name="frm">
    <div class="form-group">
        <input type="text" class="form-control" required="" placeholder="Title" id="newsboxTitle" name="title">
    </div>
    <textarea class="form-control" rows="3" id="newsbox" name="content"></textarea>

    <input type="hidden" name="page_action">
    <div>
        <a href="addNews" type="button" name="opr" class="btn btn-danger" value="1">Submit</a>
    </div>
</form>

Servlet(newsUpdate.java):

    public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{
    String opr = req.getParameter("title");
    JOptionPane.showMessageDialog(null, opr);
    getServletContext().getRequestDispatcher("/news.jsp").forward(req, res);
}

   public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{
    String opr = req.getParameter("title");
    JOptionPane.showMessageDialog(null, opr);
    getServletContext().getRequestDispatcher("/admin/news.jsp").forward(req, res);
}

如果我不放 doGet 我得到错误 405。

这是我的 web.xml:

<servlet>
    <servlet-name>addNews</servlet-name>
    <servlet-class>processes.newsUpdates</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>addNews</servlet-name>
    <url-pattern>/admin/addNews</url-pattern>
</servlet-mapping>

【问题讨论】:

  • 点击锚点和提交表单是两件不同的事情。
  • @SotiriosDelimanolis 您的评论就是答案。考虑将其移至答案。
  • 如何将其标记为已回答?
  • @user1971735 阅读此link

标签: java jsp scriptlet


【解决方案1】:

点击锚点和提交表单是两件不同的事情。 – Sotirios Delimanolis

直到该评论成为答案,我们开始...

这也解释了 405 错误。单击链接执行“获取”请求。因此调用doGet,而不是doPost。此表单需要一个“提交”按钮或一个 JavaScript 回调,用于将表单提交到 Web 服务器。

【讨论】:

    【解决方案2】:

    为什么代码使用JOptionPane.showMessageDialog(null, opr);?像任何其他参数一样获取值。代码不应该在 servlet 中混合 Java Swing,用户永远不会看到它。

    req.getParameter("opr")
    

    表单也应该使用按钮提交:

    <button type="submit" name="opr" class="btn btn-danger" value="1">Submit</button>
    

    【讨论】:

    • 我使用 JOptionPane 来检查我是否从 String opr = req.getParameter("title");
    猜你喜欢
    • 2022-12-14
    • 2021-10-20
    • 2020-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多