【问题标题】:how to get a String from a string-array in a jsp and use it in a servlet?如何从jsp中的字符串数组中获取字符串并在servlet中使用它?
【发布时间】:2017-05-30 18:10:38
【问题描述】:

我正在尝试从 jsp 文件中获取字符串变量,以便可以在 servlet 中使用它。 我从数据库中获取了这个变量并将其放入数组列表中,然后将其重定向到 jsp 文件,现在我需要在同一个 servlet 的另一个操作中使用它。

             `PreparedStatement st4=(PreparedStatement) 
              cn.prepareStatement("select nom_module from module where 
              id_filiere='"+rs3.getInt("id_filiere")+"' and 
              id_semestre='"+rs3.getInt("id_semestre")+"' ");
              rs4=st4.executeQuery();
               while(rs4.next()){

                           String m = rs4.getString("nom_module");
                            list_module.add(m);
                        }
                        session.setAttribute("listmodule", list_module);

          request.getRequestDispatcher("/etudiant.jsp").forward(request, response); `

在jsp文件中你会看到变量在一个bar menu中,所以我需要的是能够提取我点击的按钮的值。

<li>
   <div class="dropdown">
   <a href="#" style="color:#fff;line-height:60px;margin:0 10px" id="dLabel" 
   data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Modules
   <span class="caret"></span>
    </a>
    <ul class="dropdown-menu" aria-labelledby="dLabel">
    <c:forEach items="${listmodule }" var="m">
      <li><a href="./PFEaction?action=documentetudiant">${m}</a></li>
    </c:forEach>
    </ul>
 </div>
</li>

现在 ${m} 变量是我想要获取的变量,然后在以下代码中使用它:

`if(request.getParameter("action").equals("commentaireetudiant")){

            int id1=0 , id2=0;

            String contenu = request.getParameter("contenu");

            PreparedStatement ps4 = (PreparedStatement) 
            cn.prepareStatement("select * from user where username = 
            '"+e.username+"' ");

            ResultSet rs8 = ps4.executeQuery();
            if(rs8.next()){
                id1=rs8.getInt("id_user");
            }

            PreparedStatement ps5 = (PreparedStatement) 
            cn.prepareStatement("select * from module where nom_module = 
            '"+m+"' ");

            ResultSet rs9 = ps5.executeQuery();
            if(rs9.next()){
                id2=rs9.getInt("id_module");
            }

            PreparedStatement ps6 = (PreparedStatement) 
            cn.prepareStatement("insert into commenter (id_user, id_module, 
            contenu) values ('"+id1+"', '"+id2+"', '"+contenu+"') ");
            ps6.executeUpdate();

            request.getRequestDispatcher("/document-
            etudiant.jsp").forward(request, response);
 }`

在这个 servlet 代码中,preparedstatement ps5 是我使用变量的地方。请帮帮我。

谢谢。

【问题讨论】:

    标签: java jsp servlets


    【解决方案1】:

    不确定为什么要将“documentetudiant”作为操作传递给 servlet,但您可以将其替换为“m”变量,让 servlet 将其值作为操作。这是更新的 JSP:

    <li>
       <div class="dropdown">
       <a href="#" style="color:#fff;line-height:60px;margin:0 10px" id="dLabel" 
       data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Modules
       <span class="caret"></span>
        </a>
        <ul class="dropdown-menu" aria-labelledby="dLabel">
        <c:forEach items="${listmodule }" var="m">
          <li><a href="./PFEaction?action=<%=m%>">${m}</a></li>
        </c:forEach>
        </ul>
     </div>
    </li>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-31
      • 2013-09-12
      • 1970-01-01
      • 2020-03-29
      • 2017-04-28
      • 1970-01-01
      • 1970-01-01
      • 2015-03-26
      相关资源
      最近更新 更多