在sql:param中访问c:var变量

时间:2014-03-25 05:22:30

标签: jsp jsp-tags

我在访问jsp"核心标签时遇到问题"在jsp" sql标签"查询。

代码:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %> 

<sql:setDataSource
    var="myDS"
    driver="org.postgresql.Driver"
    url="jdbc:postgresql://localhost:xxxx/xyz"
    user="xyz" password="test123"
/>
<% String user_Email = session.getAttribute("email").toString(); 
   out.println("Email = "+ user_Email); %>
  <c:set var="emailid" value= "${user_Email}"/>

<sql:query var="listUsers"   dataSource="${myDS}">

    SELECT * FROM developer_apikey_registration where email_id = ?  ;
    <sql:param value="${emailid}"/>


</sql:query>

我无法在此处获取电子邮件值。虽然我检查了它在会话属性中是否正确。

请在这方面帮助我。我是JSP的新手 在此先感谢。

2 个答案:

答案 0 :(得分:0)

试试此代码

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql" %> 
<sql:setDataSource
    var="myDS"
    driver="org.postgresql.Driver"
    url="jdbc:postgresql://localhost:xxxx/xyz"
    user="xyz" password="test123"
/>
  <c:set var="emailid" value= "${sessionScope[user_Email]}"/>
<sql:query var="listUsers"   dataSource="${myDS}">
    SELECT * FROM developer_apikey_registration where email_id = ?  ;
    <sql:param value="${emailid}"/>
</sql:query>

希望这会有所帮助!!

答案 1 :(得分:0)

    <%String pkgName=null;%>
    <%String apiKey=null;%>
    <% String user_Email = session.getAttribute("email").toString(); 
       String email = null;
     if(session.isNew())
        {
             RequestDispatcher rdp=request.getRequestDispatcher("index.jsp");
             rdp.forward(request, response);
        }
    %>
   <%= user_Email %> // To check if email is not null

    <div align="center">
    <table border="1" cellpadding="5">
        <caption><h2>List of users</h2></caption>
        <tr>
            <th>Email</th>
            <th>Package Name</th>
            <th>API Key</th>
            <th> Operation </th>
        </tr>


  <%
       try
       {
         Connection con=DbConn.getConnect();
         Statement st=con.createStatement(); 
         String q1="select * from  developer_apikey_registration where email_id ='"+user_Email+"'";
         ResultSet rs1=st.executeQuery(q1);

         while(rs1.next())
                {

                   email = rs1.getString("email_id");
                   pkgName = rs1.getString("package_name");
                   apiKey = rs1.getString("apikey");
                   %>

                   <tr>
                <td><%=email%></td>
                <td><%=pkgName%></td>
                <td><%=apiKey%></td>
                <td><input type ="button" name="editBtn" id ="edtBtn" value="Edit">
        <input type ="button" name="deleteBtn" id ="delBtn" value="Delete"> </td>
            </tr>
    <%               
                }

       }  
       catch(Exception e)
               {
                e.printStackTrace();
           }


    %>

   </table>
</div>