Note that there are some explanatory texts on larger screens.

plurals
  1. POPassing data between jQuery and Servlet
    primarykey
    data
    text
    <p>I have a HTML form:</p> <pre><code>&lt;head&gt; &lt;script src="//ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.js"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; $(document).ready(function() { $('#convert').click(function(){ //pairno tis times ap tin forma var amount = $('#amount').val(); var from = $('#from').val(); var to = $('#to').val(); //kano ta dedomena ena koino string var dataString = "amount=" + amount + "&amp;from=" + from + "&amp;to=" + to; $.ajax({ type: "POST", url: "CurrencyConverter", success: function(data){ //pairno ta dedomena $('#results').show(); //vazo ta dedomena sto results div tag. $('#results').html(data); } }); }); $('#swap').click(function() {   s1=$('#to').val();     s0=$('#from').val();     $('#to').val(s0);     $('#from').val(s1); }); }); &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;div class="data"&gt; &lt;label for="from"&gt;Μετάτρεψε:&lt;/label&gt; &lt;input type="text" name="amount" id="amount" value="1" /&gt; &lt;/div&gt; &lt;div class="data"&gt; &lt;label for="fromCurrency"&gt;από:&lt;/label&gt; &lt;select name="from" id="from"&gt; &lt;option selected="" value="EUR"&gt;Euro - EUR&lt;/option&gt; &lt;option value="USD"&gt;United States Dollars - USD&lt;/option&gt; &lt;option value="GBP"&gt;United Kingdom Pounds - GBP&lt;/option&gt; &lt;option value="CAD"&gt;Canada Dollars - CAD&lt;/option&gt; &lt;/select&gt; &lt;/div&gt; &lt;div class="data"&gt; &lt;label for="to"&gt;σε:&lt;/label&gt; &lt;select name="to" id="to"&gt; &lt;option value="USD"&gt;United States Dollars - USD&lt;/option&gt; &lt;option value="GBP"&gt;United Kingdom Pounds - GBP&lt;/option&gt; &lt;option value="CAD"&gt;Canada Dollars - CAD&lt;/option&gt; &lt;option value="AUD"&gt;Australia Dollars - AUD&lt;/option&gt; &lt;option value="JPY"&gt;Japan Yen - JPY&lt;/option&gt; &lt;/select&gt; &lt;/div&gt; &lt;div class="data"&gt; &lt;input type="submit" value="Μετατροπή"&gt; &lt;input type="submit" name="swap" id="swap" value="αντάλλαξέ τα!"&gt; &lt;/div&gt; &lt;/div&gt; &lt;div id="results"&gt;&lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>I want to extract the data from that form using the script on the top and send them to my servlet.</p> <p>Here is my servlet code:</p> <pre><code>package com.example.web; import com.example.model.*; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.URL; import java.lang.*; import java.util.*; public class CurrencySelect extends HttpServlet{ public void doPost(HttpServletRequest request,HttpServletResponse response)throws IOException, ServletException{ //response.setContentType("text/html;charset=UTF-8"); String from = request.getParameter("from"); String to = request.getParameter("to"); String amount = request.getParameter("amount"); CurrencyGenerator curr = new CurrencyGenerator(); String res = curr.GetCurrency(from,to,amount); out.println(res); } } </code></pre> <p>And here is my <code>web.xml</code> file:</p> <pre><code> &lt;?xml version="1.0" encoding="ISO-8859-1"?&gt; &lt;web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" Version="2.4"&gt; &lt;servlet&gt; &lt;servlet-name&gt;CurrencyConverter&lt;/servlet-name&gt; &lt;servlet-class&gt;com.example.web.CurrencySelect&lt;/servlet-class&gt; &lt;/servlet&gt; &lt;servlet&gt; &lt;servlet-name&gt;CodeReturn&lt;/servlet-name&gt; &lt;servlet-class&gt;com.example.web.CodeReturn&lt;/servlet-class&gt; &lt;/servlet&gt; &lt;servlet&gt; &lt;servlet-name&gt;Redirect&lt;/servlet-name&gt; &lt;servlet-class&gt;com.example.web.Redirect&lt;/servlet-class&gt; &lt;/servlet&gt; &lt;servlet&gt; &lt;servlet-name&gt;ListenerTester&lt;/servlet-name&gt; &lt;servlet-class&gt;com.example.web.ListenerTester&lt;/servlet-class&gt; &lt;/servlet&gt; &lt;servlet-mapping&gt; &lt;servlet-name&gt;CurrencyConverter&lt;/servlet-name&gt; &lt;url-pattern&gt;/CurrencyConverter.do&lt;/url-pattern&gt; &lt;/servlet-mapping&gt; &lt;servlet-mapping&gt; &lt;servlet-name&gt;CodeReturn&lt;/servlet-name&gt; &lt;url-pattern&gt;/CodeReturn.do&lt;/url-pattern&gt; &lt;/servlet-mapping&gt; &lt;servlet-mapping&gt; &lt;servlet-name&gt;Redirect&lt;/servlet-name&gt; &lt;url-pattern&gt;/Redirect.do&lt;/url-pattern&gt; &lt;/servlet-mapping&gt; &lt;servlet-mapping&gt; &lt;servlet-name&gt;ListenerTester&lt;/servlet-name&gt; &lt;url-pattern&gt;/ListenTest.do&lt;/url-pattern&gt; &lt;/servlet-mapping&gt; &lt;context-param&gt; &lt;param-name&gt;report&lt;/param-name&gt; &lt;param-value&gt;report.html&lt;/param-value&gt; &lt;/context-param&gt; &lt;listener&gt; &lt;listener-class&gt;com.example.model.MyServletContextListener&lt;/listener-class&gt; &lt;/listener&gt; &lt;/web-app&gt; </code></pre> <p>I want to print the result in my html form page in a div I have in the end called results. I had the same script made with php and everything was working fine but with servlets. I can get my results in a new page, but I cannot take them in the same HTML page. How can I solve it?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload