如何使用sendirect()方法将搜索查询从servlet传递到jsp [英] How to use sendirect() method to pass search query from servlet to jsp

查看:199
本文介绍了如何使用sendirect()方法将搜索查询从servlet传递到jsp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在servlet中进行搜索查询,并将其加载到jsp表的原始文件中.

I'm trying to do the search query in servlet and load it to jsp table's raw.


    Connection dbCon = null;
    PreparedStatement state = null;
    ResultSet rs = null;
    String insert_sql = "insert into item(name,unit_price) values(?,?)";
    String search_sql = "select * from item";
    try {
        String item_name = request.getParameter("item_name");
        String up = request.getParameter("unit_price");
        try {
            dbCon = DB.JDBC.getConnection();
            state = dbCon.prepareStatement(insert_sql);
            dbCon.setAutoCommit(false);

            state.setString(1, item_name);
            state.setString(2, up);
            state.executeUpdate();

            dbCon.commit();


            rs = state.executeQuery(search_sql);
            while (rs.next()) {
                String item_id = rs.getString("item_id");
                String name = rs.getString("name");
                String unit_price = rs.getString("unit_price");
                String qty = rs.getString("qty");
                try {
                    response.sendRedirect("index.jsp?item_id=" + item_id + "&name=" + name + "&unit_price=" + unit_price + "&qty" + qty);
                } catch (Exception e) {
                    e.printStackTrace();
                }


            }
            response.sendRedirect("index.jsp?msg=New Item added successfully");
        } catch (Exception e) {
            e.printStackTrace();
            dbCon.rollback();
            out.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }


插入工作正常.搜索的第一条记录也将加载到浏览器的搜索栏中.这是我的jsp查询.


Insertion is working fine. Also first record of the search also load in search bar of the browser. And this is my jsp query.

               <tr>
                    <td><% request.getParameter("item_id");%></td>
                    <td><% request.getParameter("name");%></td>
                    <td><% request.getParameter("unit_price");%></td>
                    <td><% request.getParameter("qty");%></td>
                </tr>

我认为问题出在参数传递中.我对jsp有点新颖.所以不知道所有语法.请帮我.

I think problem is in parameter passing. I'm little bit novel to jsp. So don't know all syntax. Please help me.


编辑1 我这样改变了jsp.


Edit 1 I changed jsp like this.

                <%
                    ResultSet rs = (ResultSet) session.getAttribute("rs");
                    while (rs.next()) {
                        String item_id = rs.getString("item_id");
                        String name = rs.getString("name");
                        String unit_price = rs.getString("unit_price");
                        String qty = rs.getString("qty");
                %>
                <tr>
                    <td><%=item_id%></td>
                    <td><%=name%></td>
                    <td><%=unit_price%></td>
                    <td><%=qty%></td>
                </tr>
                <%}%>
            </tbody>


也像这样的servlet


Also servlet like this

   rs = state.executeQuery(search_sql);

                session.setAttribute("rs", rs);
                response.sendRedirect("index.jsp"); //redirect to jsp without any params 

现在我在jsp中开始的行中有org.apache.jasper.JasperException.

Now I'm having org.apache.jasper.JasperException in the line where while start in jsp.

这是错误.

推荐答案

您正尝试使用表达式标签,其用法如下:

You are trying to use the expression tag which is used as follows:

<td><%=request.getParameter("item_id")%></td>
<td><%=request.getParameter("name")%></td>
<td><%=request.getParameter("unit_price")%></td>
<td><%=request.getParameter("qty")%></td>

更新:

将结果集对象会话保存在servlet中,如下所示:

Save the resultset object session in your servlet like this:

    HttpSession session = request.getSession();
    rs = state.executeQuery(search_sql);  //after executing the query just write these two lines instead of while loop
   HashMap<String, ArrayList<String>> hm = new HashMap<String, ArrayList<String>>(); //change here
ArrayList<String> ls=null; //change here
int i=0;
    while (rs.next()) {
                ls=new ArrayList<String>(); //change here
                ls.add(rs.getString("item_id"));
                ls.add(rs.getString("name"));
                ls.add(rs.getString("unit_price"));
                ls.add(rs.getString("qty"));
                hm.put("row"+(++i), ls);
            }
    session.setAttribute("hm",hm);
    response.sendRedirect("index.jsp"); //redirect to jsp without any params

并在您的jsp上:

<%
HashMap hm=(HashMap)session.getAttribute("hm");
Set<String> keyset=hm.keySet();
Iterator itr = keyset.iterator();
while (itr.hasNext()) {
    String key = itr.next();
    ArrayList<String> ls = (ArrayList)hm.get(key); 
    String item_id = ls.get(0);
    String name = ls.get(1);
    String unit_price = ls.get(2);
    String qty = ls.get(3);
    %>
    <tr>
      <td><%=item_id%></td>
      <td><%=name%></td>
      <td><%=unit_price%></td>
      <td><%=qty%></td>
    </tr>
<%
    } catch (Exception e) {
        e.printStackTrace();
 }
}

这篇关于如何使用sendirect()方法将搜索查询从servlet传递到jsp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆