Forum Stats

  • 3,770,393 Users
  • 2,253,101 Discussions
  • 7,875,437 Comments

Discussions

Need help with servlets and HttpSession code

3070697
3070697 Member Posts: 2
edited Nov 13, 2015 11:04AM in Java Servlet

I am still new to servlets and JDBC stuff and would like some help on the following code:

<span class="kwd" style="color: #00008b;">try</span><span class="pun" style="color: #000000;">{</span><span class="pln" style="color: #000000;"> </span>
<span class="pln" style="color: #000000;"></span><span class="typ" style="color: #2b91af;">String</span><span class="pln" style="color: #000000;"> selectSQL </span><span class="pun" style="color: #000000;">=</span><span class="pln" style="color: #000000;"> </span><span class="str" style="color: #800000;">"select * from product_list where category = '"</span><span class="pun" style="color: #000000;">+</span><span class="pln" style="color: #000000;">category</span><span class="pun" style="color: #000000;">+</span><span class="str" style="color: #800000;">"'"</span><span class="pun" style="color: #000000;">;</span><span class="pln" style="color: #000000;"> </span>
<span class="pln" style="color: #000000;"></span><span class="typ" style="color: #2b91af;">Statement</span><span class="pln" style="color: #000000;"> stmt </span><span class="pun" style="color: #000000;">=</span><span class="pln" style="color: #000000;"> conn</span><span class="pun" style="color: #000000;">.</span><span class="pln" style="color: #000000;">createStatement</span><span class="pun" style="color: #000000;">();</span><span class="pln" style="color: #000000;"> </span>
<span class="pln" style="color: #000000;"></span><span class="typ" style="color: #2b91af;">ResultSet</span><span class="pln" style="color: #000000;"> rs1 </span><span class="pun" style="color: #000000;">=</span><span class="pln" style="color: #000000;"> stmt</span><span class="pun" style="color: #000000;">.</span><span class="pln" style="color: #000000;">executeQuery</span><span class="pun" style="color: #000000;">(</span><span class="pln" style="color: #000000;">selectSQL</span><span class="pun" style="color: #000000;">);</span><span class="pln" style="color: #000000;">  </span>
HttpSession session = request.getSession(true); 
<span class="pln" style="color: #000000;"></span><span class="typ" style="color: #2b91af;">ArrayList</span><span class="pun" style="color: #000000;"><</span><span class="typ" style="color: #2b91af;">Float</span><span class="pun" style="color: #000000;">></span><span class="pln" style="color: #000000;"> idList </span><span class="pun" style="color: #000000;">=</span><span class="pln" style="color: #000000;"> </span><span class="kwd" style="color: #00008b;">new</span><span class="pln" style="color: #000000;"> </span><span class="typ" style="color: #2b91af;">ArrayList</span><span class="pun" style="color: #000000;"><</span><span class="typ" style="color: #2b91af;">Float</span><span class="pun" style="color: #000000;">>();</span><span class="pln" style="color: #000000;">  </span>
<span class="pln" style="color: #000000;">out</span><span class="pun" style="color: #000000;">.</span><span class="pln" style="color: #000000;">println</span><span class="pun" style="color: #000000;">(</span><span class="str" style="color: #800000;">"<table border=\"1\"><tr><th>Item_ID</th><th>Item_name</th><th>Title</th><th>Category</th><th>Image_name</th><th>Price</th><th>Stock_Count</th></tr>"</span><span class="pun" style="color: #000000;">);</span><span class="pln" style="color: #000000;">  </span>
<span class="pln" style="color: #000000;"></span><span class="kwd" style="color: #00008b;">while</span><span class="pun" style="color: #000000;">(</span><span class="pln" style="color: #000000;">rs1</span><span class="pun" style="color: #000000;">.</span><span class="pln" style="color: #000000;">next</span><span class="pun" style="color: #000000;">()){</span><span class="pln" style="color: #000000;">  </span>
<span class="pln" style="color: #000000;">out</span><span class="pun" style="color: #000000;">.</span><span class="pln" style="color: #000000;">println</span><span class="pun" style="color: #000000;">(</span><span class="str" style="color: #800000;">"<tr><td>"</span><span class="pun" style="color: #000000;">+</span><span class="pln" style="color: #000000;"> rs1</span><span class="pun" style="color: #000000;">.</span><span class="pln" style="color: #000000;">getFloat</span><span class="pun" style="color: #000000;">(</span><span class="str" style="color: #800000;">"item_id"</span><span class="pun" style="color: #000000;">)</span><span class="pln" style="color: #000000;"> </span><span class="pun" style="color: #000000;">+</span><span class="pln" style="color: #000000;"> </span><span class="str" style="color: #800000;">"</td>"</span><span class="pun" style="color: #000000;">);</span><span class="pln" style="color: #000000;"> </span>
<span class="pln" style="color: #000000;">out</span><span class="pun" style="color: #000000;">.</span><span class="pln" style="color: #000000;">println</span><span class="pun" style="color: #000000;">(</span><span class="str" style="color: #800000;">"<td>"</span><span class="pln" style="color: #000000;"> </span><span class="pun" style="color: #000000;">+</span><span class="pln" style="color: #000000;"> rs1</span><span class="pun" style="color: #000000;">.</span><span class="pln" style="color: #000000;">getString</span><span class="pun" style="color: #000000;">(</span><span class="str" style="color: #800000;">"item_name"</span><span class="pun" style="color: #000000;">)</span><span class="pln" style="color: #000000;"> </span><span class="pun" style="color: #000000;">+</span><span class="pln" style="color: #000000;"> </span><span class="str" style="color: #800000;">"</td>"</span><span class="pun" style="color: #000000;">);</span><span class="pln" style="color: #000000;"> </span>
<span class="pln" style="color: #000000;">out</span><span class="pun" style="color: #000000;">.</span><span class="pln" style="color: #000000;">println</span><span class="pun" style="color: #000000;">(</span><span class="str" style="color: #800000;">"<td>"</span><span class="pun" style="color: #000000;">+</span><span class="str" style="color: #800000;">"<a href =\"ItemDetail\">"</span><span class="pln" style="color: #000000;"> </span><span class="pun" style="color: #000000;">+</span><span class="pln" style="color: #000000;"> rs1</span><span class="pun" style="color: #000000;">.</span><span class="pln" style="color: #000000;">getString</span><span class="pun" style="color: #000000;">(</span><span class="str" style="color: #800000;">"title"</span><span class="pun" style="color: #000000;">)+</span><span class="str" style="color: #800000;">"</a>"</span><span class="pln" style="color: #000000;"> </span><span class="pun" style="color: #000000;">+</span><span class="pln" style="color: #000000;"> </span><span class="str" style="color: #800000;">"</td>"</span><span class="pun" style="color: #000000;">);</span><span class="pln" style="color: #000000;"> </span>
<span class="pln" style="color: #000000;">out</span><span class="pun" style="color: #000000;">.</span><span class="pln" style="color: #000000;">println</span><span class="pun" style="color: #000000;">(</span><span class="str" style="color: #800000;">"<td>"</span><span class="pln" style="color: #000000;"> </span><span class="pun" style="color: #000000;">+</span><span class="pln" style="color: #000000;"> rs1</span><span class="pun" style="color: #000000;">.</span><span class="pln" style="color: #000000;">getString</span><span class="pun" style="color: #000000;">(</span><span class="str" style="color: #800000;">"category"</span><span class="pun" style="color: #000000;">)</span><span class="pln" style="color: #000000;"> </span><span class="pun" style="color: #000000;">+</span><span class="pln" style="color: #000000;"> </span><span class="str" style="color: #800000;">"</td>"</span><span class="pun" style="color: #000000;">);</span><span class="pln" style="color: #000000;"> </span>
<span class="pln" style="color: #000000;">out</span><span class="pun" style="color: #000000;">.</span><span class="pln" style="color: #000000;">println</span><span class="pun" style="color: #000000;">(</span><span class="str" style="color: #800000;">"<td>"</span><span class="pln" style="color: #000000;"> </span><span class="pun" style="color: #000000;">+</span><span class="pln" style="color: #000000;"> rs1</span><span class="pun" style="color: #000000;">.</span><span class="pln" style="color: #000000;">getString</span><span class="pun" style="color: #000000;">(</span><span class="str" style="color: #800000;">"image_name"</span><span class="pun" style="color: #000000;">)</span><span class="pln" style="color: #000000;"> </span><span class="pun" style="color: #000000;">+</span><span class="pln" style="color: #000000;"> </span><span class="str" style="color: #800000;">"</td>"</span><span class="pun" style="color: #000000;">);</span><span class="pln" style="color: #000000;"> </span>
<span class="pln" style="color: #000000;">out</span><span class="pun" style="color: #000000;">.</span><span class="pln" style="color: #000000;">println</span><span class="pun" style="color: #000000;">(</span><span class="str" style="color: #800000;">"<td> "</span><span class="pln" style="color: #000000;"> </span><span class="pun" style="color: #000000;">+</span><span class="pln" style="color: #000000;"> rs1</span><span class="pun" style="color: #000000;">.</span><span class="pln" style="color: #000000;">getFloat</span><span class="pun" style="color: #000000;">(</span><span class="str" style="color: #800000;">"price"</span><span class="pun" style="color: #000000;">)</span><span class="pln" style="color: #000000;"> </span><span class="pun" style="color: #000000;">+</span><span class="pln" style="color: #000000;"> </span><span class="str" style="color: #800000;">"</td>"</span><span class="pun" style="color: #000000;">);</span><span class="pln" style="color: #000000;"> </span>
<span class="pln" style="color: #000000;">out</span><span class="pun" style="color: #000000;">.</span><span class="pln" style="color: #000000;">println</span><span class="pun" style="color: #000000;">(</span><span class="str" style="color: #800000;">"<td> "</span><span class="pln" style="color: #000000;"> </span><span class="pun" style="color: #000000;">+</span><span class="pln" style="color: #000000;"> rs1</span><span class="pun" style="color: #000000;">.</span><span class="pln" style="color: #000000;">getFloat</span><span class="pun" style="color: #000000;">(</span><span class="str" style="color: #800000;">"stock_count"</span><span class="pun" style="color: #000000;">)</span><span class="pln" style="color: #000000;"> </span><span class="pun" style="color: #000000;">+</span><span class="pln" style="color: #000000;"> </span><span class="str" style="color: #800000;">"</td>"</span><span class="pun" style="color: #000000;">);</span><span class="pln" style="color: #000000;"> </span>
<span class="pln" style="color: #000000;">out</span><span class="pun" style="color: #000000;">.</span><span class="pln" style="color: #000000;">println</span><span class="pun" style="color: #000000;">(</span><span class="str" style="color: #800000;">"</tr>"</span><span class="pun" style="color: #000000;">);</span><span class="pln" style="color: #000000;"> </span>

<span class="pln" style="color: #000000;">idList</span><span class="pun" style="color: #000000;">.</span><span class="pln" style="color: #000000;">add</span><span class="pun" style="color: #000000;">(</span><span class="pln" style="color: #000000;">rs1</span><span class="pun" style="color: #000000;">.</span><span class="pln" style="color: #000000;">getFloat</span><span class="pun" style="color: #000000;">(</span><span class="str" style="color: #800000;">"item_id"</span><span class="pun" style="color: #000000;">));</span><span class="pln" style="color: #000000;"> </span>
<span class="pln" style="color: #000000;">session</span><span class="pun" style="color: #000000;">.</span><span class="pln" style="color: #000000;">setAttribute</span><span class="pun" style="color: #000000;">(</span><span class="str" style="color: #800000;">"id"</span><span class="pun" style="color: #000000;">,</span><span class="pln" style="color: #000000;"> idList</span><span class="pun" style="color: #000000;">);</span><span class="pln" style="color: #000000;"> </span>
<span class="pln" style="color: #000000;"></span><span class="pun" style="color: #000000;">}</span><span class="pln" style="color: #000000;"> </span>
<span class="pln" style="color: #000000;">out</span><span class="pun" style="color: #000000;">.</span><span class="pln" style="color: #000000;">println</span><span class="pun" style="color: #000000;">(</span><span class="str" style="color: #800000;">"</table>"</span><span class="pun" style="color: #000000;">);</span><span class="pln" style="color: #000000;">  </span>
<span class="pln" style="color: #000000;">conn</span><span class="pun" style="color: #000000;">.</span><span class="pln" style="color: #000000;">close</span><span class="pun" style="color: #000000;">();</span><span class="pln" style="color: #000000;"> </span>
<span class="pln" style="color: #000000;"></span><span class="pun" style="color: #000000;">}</span><span class="pln" style="color: #000000;"> </span>
<span class="pln" style="color: #000000;"></span><span class="kwd" style="color: #00008b;">catch</span><span class="pun" style="color: #000000;">(</span><span class="typ" style="color: #2b91af;">SQLException</span><span class="pln" style="color: #000000;"> se</span><span class="pun" style="color: #000000;">)</span>
<span class="pun" style="color: #000000;">{</span><span class="pln" style="color: #000000;"> </span>
<span class="pln" style="color: #000000;"></span><span class="typ" style="color: #2b91af;">System</span><span class="pun" style="color: #000000;">.</span><span class="pln" style="color: #000000;">err</span><span class="pun" style="color: #000000;">.</span><span class="pln" style="color: #000000;">println</span><span class="pun" style="color: #000000;">(</span><span class="pln" style="color: #000000;">se</span><span class="pun" style="color: #000000;">);</span><span class="pln" style="color: #000000;"> </span>
<span class="pln" style="color: #000000;"></span><span class="pun" style="color: #000000;">}</span>

Currently the code above will print out a table with all the 'title' being hyperlinks and HttpSession is used to capture all the item_ids and store them in an arraylist, up to this part seems to be fine but what I want to do now is for it to only send the specific 'item_id' based on which title the user click on, to a different servlet which will perform a SQL query using that id. Any help/example code would be appreciated and Thank you in advance.

Here are the code I have tried in a different servlet to receive the data from the above table but none of them have done anything.

<span class="typ" style="color: #2b91af;">HttpSession</span><span class="pln" style="color: #000000;"> session </span><span class="pun" style="color: #000000;">=</span><span class="pln" style="color: #000000;"> request</span><span class="pun" style="color: #000000;">.</span><span class="pln" style="color: #000000;">getSession</span><span class="pun" style="color: #000000;">(</span><span class="kwd" style="color: #00008b;">true</span><span class="pun" style="color: #000000;">);</span><span class="pln" style="color: #000000;"> </span>
<span class="pln" style="color: #000000;"></span><span class="typ" style="color: #2b91af;">ArrayList</span><span class="pun" style="color: #000000;"><</span><span class="typ" style="color: #2b91af;">Float</span><span class="pun" style="color: #000000;">></span><span class="pln" style="color: #000000;"> id </span><span class="pun" style="color: #000000;">=</span><span class="pln" style="color: #000000;"> </span><span class="pun" style="color: #000000;">(</span><span class="typ" style="color: #2b91af;">ArrayList</span><span class="pun" style="color: #000000;"><</span><span class="typ" style="color: #2b91af;">Float</span><span class="pun" style="color: #000000;">>)</span><span class="pln" style="color: #000000;"> session</span><span class="pun" style="color: #000000;">.</span><span class="pln" style="color: #000000;">getAttribute</span><span class="pun" style="color: #000000;">(</span><span class="str" style="color: #800000;">"id"</span><span class="pun" style="color: #000000;">);</span>


HttpSession session = request.getSession(true); List<Float> id = new ArrayList<Float>((ArrayList<Float>)session.getAttribute("id"));
Tagged:

Answers

This discussion has been closed.