In the GetQuote example we provide a list of stocks in a drop down. When a particular stock is selected we want to automatically display the stock description.
we extend the logic further to connect the struts application to the mysql database and fetch the quote from the database.
In the below post, we provide the list of stocks in a dropdown menu by selecting the list from the stocks table by connecting to the stocks database in mysql. Here we go one step further and display the name of the stock along with the stock code selected. For this we using the javascipt AJAX functionality for which the details are given below. The detailed code changes for implementing the AJAX functionality can be found at Jakarta Struts : Using AJAX - The source code
In the "Stock Symbol" html tag, we add a drop down which will connect to the mysql database and select the id and the stock symbol from the stocks table using JSP expression tags <% .. java code %> to embed Java Code to connect to the database. This will give a drop down of Stock Symbols. Once the user will select a particular Stock Symbol, the "onchange" event will get triggered. On this onchange event we call a Javascript function, which is nothing but your AJAX call.
This Ajax function retrieveURL will take 3 parameters, the Action class to be called, the form which is to be posted and the HTML element tag (symbol) whose value we will retrieve in the Javascript function using the selectElement.value.
<td>Stock Symbol:</td> <select name="symbol" onchange="retrieveURL('/getquoteAjax/stockname.do?','lookupForm',this);"> <option value="-1">Select</option> <% Connection conn = null; String url = "jdbc:mysql://localhost:3306/stocks?useSSL=false&allowPublicKeyRetrieval=true"; String user = "root"; String pass = "mysql"; int sumcount = 0; Statement st; try { Class.forName("com.mysql.jdbc.Driver").newInstance(); conn = DriverManager.getConnection(url, user, pass); String query = "select symbol from stocks"; st = conn.createStatement(); ResultSet rs = st.executeQuery(query); %> <% while (rs.next()) { %> <option value="<%=rs.getString(1)%>"><%=rs.getString(1)%></option> <% } %> <% } catch (Exception e) { e.printStackTrace(); } %> </select>
var symbolValue = selectElement.value; // extract the symbol value selected from the selectElement parameter.
console.log("retrieveURL called", selectElement); console.log("Symbol value:", symbolValue); console.log("Base URL:", url);
retrieveURL called:
<select name="symbol" onchange="retrieveURL('/getquoteAjax/stockname.do?','lookupForm',this);"> <option value="-1">Select</option> <option value="GOGL">GOGL</option> <option value="MSFT">MSFT</option> <option value="SUNW">SUNW</option> <option value="YHOO">YHOO</option> </select> Symbol value: GOGL Base URL: /getquoteAjax/stockname.do?
We create the final url as below
var finalUrl = url + "symbol=" + encodeURIComponent(symbolValue);
console.log("Final URL:", finalUrl); Final URL: /getquoteAjax/stockname.do?symbol=GOGL
// Create XMLHttpRequest var req; if (window.XMLHttpRequest) { // Non-IE browsers req = new XMLHttpRequest(); req.onreadystatechange = function() { processStateChange(req); }; try { req.open("GET", finalUrl, true); req.send(null); } catch (e) { alert("Problem Communicating with Server\n" + e); }
<span id="stockName"><input type="text" name="stock_name" id="sname" value=""></span>
<span id="stockName"><input type="text" name="stock_name" id="sname" value="Google any Alphabet" readonly></span>
if (req.readyState == 4) { // Complete if (req.status == 200) { // OK response console.log("Processing response.........."); console.log("Ajax response:", req.responseText); } }
Second AJAX application with JSP
No comments:
Post a Comment