0

I want to send multiple data values that has been retrieved from database to another servlet or jsp.

Here is my code

PreparedStatement st= conn.prepareStatement("select name from users where uname=? and pword=?");

    st.setString(1, uname);
    st.setString(2, pword);

    ResultSet rs= st.executeQuery();

    if(rs.next())
    {
        if(uname.equals(uname) && pword.equals(pword))
        {
            request.setAttribute("name", rs.getString("name"));
            request.setAttribute("uname", uname);

            HttpSession ss= request.getSession();
            ss.setAttribute("name",rs.getString("name"));
            ss.setAttribute("uname",rs.getString("uname"));
            response.sendRedirect("Dash.jsp");
        }
    }

I want to send both name as well as uname(username) to another jsp file called "Dash.jsp". But it is not working. if i comment any one of the following code :

ss.setAttribute("name",rs.getString("name"));
ss.setAttribute("uname",uname));

then the servlet is passing the data to Dash.jsp

I also tried to use RequestDispatcher by applying the following code :

request.setAttribute("name", rs.getString("name");
request.setAttribute("uname",uname);
String destination = "Dash.jsp";
RequestDispatcher rd = 
getServletContext().getRequestDispatcher(destination);
rd.forward(request, response);

Even if i give destination="/WEB-INF/pages/Dash.jsp" . then also it is not going to Dash.jsp page.

Can anybody please tell me how to send multiple values or data in this case name,uname from one servlet or jsp to another.

Rohit
  • 1
  • 4

1 Answers1

0

When you store anything as request attribute or session attribute you can access them in jsp with Expression Language.

suppose you stored your name and uname as session attribute as below

ss.setAttribute("name",rs.getString("name"));
ss.setAttribute("uname",rs.getString("uname"));

the you can access them inside jsp as

<p>${name}</p>
<p>${uname}</p>

I mentioned p tag for example,you can use them inside jsp wherever you want using ${Attribute Name}

Hope this helps, Post if you need anything else.

raviraja
  • 676
  • 10
  • 27