I have form with a button submit and i want that when the button submit is clicked make a POST through a servlet to database (mariadb) and then GET the values posted again through the same servlet to a HTML page on the same event (the clicked button).
When a run the servlet it gets the new values posted through the submit button on the HTML..But only when i ran the servlet by itself(on Netbeans right click on the code and run..). It work separately.
Here it is the doPost method:
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Gson convertir = new Gson();
PrintWriter out = response.getWriter(); // PrintWriter imprime texto en un objeto. getWriter() devuelve un objeto PrintWriter.
String texto = request.getReader().readLine();//getReader() devuelve el contenido de la respuesta. readLine() lee el contenido de la variable "texto" y lo retorna.
TreeMap<String, String> objetoSabor = convertir.fromJson(texto, TreeMap.class);// Crea una lista treemap y convierte la respuesta de JSON a treemap..
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conMDB = DriverManager.getConnection("jdbc:mysql://localhost/cremo", "root", "root");
PreparedStatement sentencia = conMDB.prepareStatement("insert into inventarios (inv_hel_id, inv_sab_id, inv_cantidad ) values (?,?,?)");
sentencia.setString(1, objetoSabor.get("heladerias"));//"nombre" lo saca del objeto "sabor" en AJAX (sabor.nombre)
sentencia.setString(2, objetoSabor.get("sabor"));//"calorias" lo saca del objeto "calorias" en AJAX (sabor.calorias)
sentencia.setString(3, objetoSabor.get("calorias"));//"calorias" lo saca del objeto "calorias" en AJAX (sabor.calorias)
sentencia.execute();
And here the doGet method:
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conMDB = DriverManager.getConnection("jdbc:mysql://localhost/cremo", "root", "root");
PreparedStatement sentencia = conMDB.prepareStatement( "select * from inventarios");
ResultSet resultado = sentencia.executeQuery();//ResulSet pone el puntero en la primera fila de la tabla de la Base de datos.
while( resultado.next() ){
out.println("[OK] " + request.getParameter("heladerias") + ", " + resultado.getString(2) + ", " + resultado.getString(3));
}
The html form:
<form>
<fieldset>
<p>
<select id="heladerias" name="heladerias">
<option value="1">Palermo</option>
<option value="2">Recoleta</option>
<option value="3">Belgrano</option>
</select>
<select id="sabores" name="sabores" >
<option value="1">Sambayon italiano</option>
<option value="3">Sambayon Frances</option>
<option value="5">Mousse de Limon</option>
<option value="6">Pistacho</option>
</select>
<input id="sabor_calorias" type="number" name="quantity" min="1" max="6000">
</p>
<input id="fecha" type="date" name="fecha" step="1" min="2016-01-01" max="2016-12-31">
<input type="submit" value="Submit" onclick="envioCorrecto()"/>
</fieldset>
</form>