【问题标题】:getParameter returns null from Method getgetParameter 从方法 get 返回 null
【发布时间】:2017-05-20 23:22:35
【问题描述】:

我正在尝试从 url 获取 id 但 getParameter 返回 null 这就是我在 url (tool.jsp) 中发送 id 的方式:

<a href="http://localhost:8080/Projectpfeweb/executetool.jsp?id=${l.tool_id}" class="btn btn-info" role="button">Execute</a>

这是我想要 id 值的 doGet 方法

 protected void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
{
      ToolDAO dao=new ToolDAO();

    String id= req.getParameter("id");

    Tool t=dao.getToolById(Integer.parseInt(id));
      String first = req.getParameter("first");
     byte[] bytes = first.getBytes(StandardCharsets.ISO_8859_1);
     first= new String(bytes, StandardCharsets.UTF_8);
   if(first!=null)
   {
    String [] input=first.split(" ");
      System.out.println("input"+input[0]);
      EXEJAVA exe=new EXEJAVA();
      FileRead f=new FileRead();

      f.writeinputfile(input);
       ArrayList l=exe.executetool(t.getTool_path_exe());

       req.setAttribute("l",l);
       req.setAttribute("first", first);
       req.getRequestDispatcher("executetool.jsp").forward(req, res);
}

这是表单(executetool.jsp)

 <form accept-charset="UTF-8" name="form" action="executetool" method="get">

<div class="centre">
<div class="row">
<div class="inline-div">
  <label for="ta1">Text in Latin script</label>
<textarea cols="60" rows="15" id="first" name="first" class="inline-
txtarea">${first}</textarea>
</div>
&nbsp;
<div class="inline-div">
<input type="button" value="Clear" onclick="javascript:eraseText();"> 
  </div>
  &nbsp;
  <div class="inline-div">
    <label for="ta2" >Text in Arabic script</label>
    <textarea cols="60" rows="15" id="second" name="second" class="inline-
    txtarea"><c:forEach items="${l}" var="s">${s}&nbsp;</c:forEach>
    </textarea>
     </div>
     </div>
   </div>

     </form>

由于它的方法 get 每次刷新页面时 url 都会不断变化,因此“id=something”部分被替换为我在表单中拥有的两个文本区域的值在网址中?

【问题讨论】:

  • 对ID属性使用隐藏输入或将ID设置为会话,您可以从任何地方访问参数。
  • 隐藏输入解决方案不起作用,因为如果再次刷新页面,值会丢失,至于我不熟悉的会话,请您给我更多详细信息吗?跨度>
  • IN JSP :
  • 在 SERVLET 或 ETC 中。 public void doGet(HttpServletRequest request, HttpServletResponse response) { HttpSession session = request.getSession(); String username = (String)request.getAttribute("un"); session.setAttribute("用户名", 用户名); }
  • 没用,我得到了 null

标签: java jsp servlets get


【解决方案1】:

改为放置隐藏字段

<input type='hidden' name='id' value='${l.tool_id}'>

然后对按钮使用输入类型 submit,而不是通用的 &lt;a&gt; 标记,因为除非您在某处有 JavaScript 代码可以为您提交表单,否则它不会提交表单。

您也可以将 id 放在表单的action 属性中。

<form accept-charset="UTF-8" name="form" action="executetool?id=${l.tool_id}" method="get">

【讨论】:

  • 我编辑了我的问题;我用get方法发送的id和表单不是同一个jsp页面
  • @OumaymaBouzidi 我明白了。如果是这样的话,也许 ID 一开始就真的是空的。您是否尝试过检查 &lt;a&gt; 元素以查看它是否真的有 id?
  • 当我点击按钮时,我可以在 url 中看到它,所以不它不是 null
猜你喜欢
  • 2019-05-21
  • 1970-01-01
  • 2012-07-10
  • 1970-01-01
  • 2016-07-10
  • 1970-01-01
  • 1970-01-01
  • 2017-02-07
  • 2016-11-25
相关资源
最近更新 更多