Jitendra Manjani
Jitendra Manjani

Reputation: 3

getAttribute returning null

This is my servlet and i have sent lasti to the JSP but it returns null

Servlet

public void service(HttpServletRequest request,HttpServletResponse response)
{
    PrintWriter out=null;
    Connection con=null;
    response.setContentType("text/html");
    try
    {

        out=response.getWriter();//



        DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());//java.sql.*;
        con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","xweb","abc");

        String query="select * from inbox";
        System.out.println(query);

        PreparedStatement pstmt=con.prepareStatement(query);

        ResultSet rs=pstmt.executeQuery();


        int i=0;
        while(rs.next())
        {
            String str=rs.getString(2);
            System.out.println(str);
            i++;
            request.setAttribute("k"+i,str);    
        }
        request.setAttribute("lasti",i);
        RequestDispatcher rd=request.getRequestDispatcher("inboxSuccess.jsp");
        rd.forward(request,response);

    } catch(Exception e)
    {
        try{
            RequestDispatcher rd=request.getRequestDispatcher("customerLoginError.jsp");
            rd.forward(request,response);
        }catch(Exception e1){}

        e.printStackTrace();
    }
    finally
    {
        try
        {               
            con.close();
        }catch(Exception e){}
    }


}

JSP

<%
int x=(Integer)request.getAttribute("lasti");
out.println(x);
%>

It returns 0 and when i am using x in for loop the loop works for 5 times but i can't print the value of x. I am new to Java so i don't know how to use JSTL

Upvotes: 0

Views: 401

Answers (1)

Kunal Kakkad
Kunal Kakkad

Reputation: 653

Do the rs variable contains the record?

Does request.getAttribute("k"+i) conatins any record?

Upvotes: 1

Related Questions