简体   繁体   中英

I am having trouble forwarding data from servlet to my jsp

i am new to the servlet and jsp scene and was having trouble with getting my data from my servlet out to my jsp. I followed examples i found online and have done everything i know how to do in order to get data to my jsp.

Here is the code that i have for my servlet. I tried to pull all the data from the database and then forward that to jsp.

Another problem i had was that if i try to print any of the info to the console with system.out nothing shows.

I commented some stuff out to try and eliminate possible problem areas.

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException 
{
try
    {
    String query = "select * from PRODUCT_TABLE";
    // connect to DB
    Class.forName("oracle.jdbc.OracleDriver");
    conn = DriverManager.getConnection("took this out for security");
    Statement stmt = conn.createStatement();
    // get info from DB 
    ResultSet rs = stmt.executeQuery(query);
            while (rs.next())
        {
         // itemName = rs.getString("ITEM_NAME");
          itemNum = rs.getString("ITEM_NUM");
          itemPrice = rs.getString("ITEM_PRICE");
          // compile all the data
          /*itemNameComp[i] = itemName;
          itemNumComp[i] = itemNum;
          itemPriceComp[i] = itemPrice;
          i++;*/
        }

    req.setAttribute("itemName", 567);
    req.getRequestDispatcher("/StartShopping.jsp").forward(req, resp);
    //String test = req.getParameter("itemName");

    //rd.forward(req, resp);

    //store info in the session object

    // get selection from user and do calculation for total purchase

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

in the code below i tried to hard code some value to try and see if i could at least get something to the jsp even if it wasnt from the data that i wanted.

req.setAttribute("itemName", 567);
    req.getRequestDispatcher("/StartShopping.jsp").forward(req, resp);

The code that i used to call the hard set data in the JSP is below

<%=(String)request.getAttribute("itemName")%>

The code above prints "null" in the .jsp. i wanted to try and make it print just as a test to see if i could force it to work with hard data.

i tried changing it to req.getAttribute but the jsp wasnt happy with it

Any insight would be greatly appreciated. Thank you very much!!! i apologize if there is some obvious error but i can't find it.

Are you trying to just run the jsp file?

In that case the attribute is never forwarded from the servlet and the jsp page always prints null.

try to create a html file which invokes the post method in servlet:

<html>
<body>
<form action="/yourServletURL" method="post">
<input type="submit">
</form>
</body>
</html>

May be because your are setting an integer and trying to typecast it to String Use the following code and check once

 req.setAttribute("itemName", "567");
 //instead of  req.setAttribute("itemName", 567);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM