Hi, I have a problem with jsp, I have a form that makes a query to the database and should return the contents of the database in a lightbox that is in the same jsp file,
I'm trying with json
<div class="form-group form-float">
<div class="form-line">
<input type="text" id="isbn" class="form-control" name="buscar">
<label class="form-label">Ingrese Nombre o ISBN</label>
</div>
</div>
<button id="ver"data-toggle="modal" data-target="#largeModal">CONSULTAR</button>
<script>
$(function(){
$("#ver").click(function(){
var isbn = $("#isbn").val();
$.ajax({
type:"get",
url:"libro.do",
dataType:"json",//json
data:{isbn:isbn,accion:"b"},
success:function(data){//respuesta ok
alert(data.titulo)
un = $.parseJSON(data);
alert(un.editorial);
}
})
})
})
and the servlet is this way
public class LibrosServlet extends HttpServlet {
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
JSONObject json = new JSONObject();
LibroDao libro = new LibroDao();
String accion = request.getParameter("accion");
String isbnTxt = request.getParameter("isbn");
if(accion != null && accion.equals("b") && isbnTxt != null){
PrintWriter out = response.getWriter();
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
int isbn = Integer.parseInt(isbnTxt);
Libro l = libro.findById(isbn);
json.put("titulo", l.getTitulo());
json.put("editorial", l.getEditorial());
json.put("año", l.getAnio());
json.put("paginas", l.getPaginas());
json.put("precio", l.getPrecio());
//PrintWriter out = response.getWriter();
//out.print("nombre "+l.getTitulo());
//out.print(" - "+l.getEditorial());
out.print("["+json.toString()+"]");
}else{
request.setAttribute("libros", libro.Mostrar());
request.getRequestDispatcher("biblioteca.jsp").forward(request, response);
}
}
and show the dynamic content in the modal that should be capturing the id in javascript and add html code
<div class="modal fade" id="largeModal" tabindex="-1" role="dialog" style="display: none;">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<h2 class="modal-title" id="largeModalLabel"> </h2>
</div>
<div class="modal-body">
<div class="row">
<div class="col-xs-6 col-md-6">
<a href="javascript:void(0);" class="thumbnail">
<img src="images/libro/lordofthering.jpg" class="img-responsive">
</a>
</div>
<div class="col-xs-6 col-md-6">
<p>Los Anillos de Poder fueron forjados en antiguos tiempos por los herreros Elfos, y Sauron, el Señor Oscuro, forjó el Anillo Único. Pero en una ocasión se lo quitaron, y aunque lo buscó por toda la Tierra Media nunca pudo encontrarlo. Al cabo de muchos años fue a caer casualmente en manos del hobbit Bilbo Bolsón. Sauron llegó a reunir todos los Grandes Anillos, pero continuaba buscando el Anillo Único que completaría el dominio de Mordor. Bilbo desapareció durante la celebración de su centesimodecimoprimer cumpleaños, y dejó a Frodo a cargo del Anillo, y con una peligrosa misión por delante: atravesar la Tierra Media, internarse en las sombras del País Oscuro y destruir el Anillo arrojándolo en las Grietas del Destino.</p>
</div>
<div class="col-md-6">
<p>valor<strong> $5000</strong></p>
<p>unidades disponibles:<strong>3</strong></p>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-link waves-effect" data-dismiss="modal">Cerrar</button>
</div>
</div>
</div>
</div>
Thank you very much for your help !!!