Pessoal, não sei porque mas não estou conseguindo passar valores de uma Javascript para um Servlet através de uma URL “href”.
alguem pode me ajudar?
JSP
<%@ page language="java" isELIgnored="false" contentType="text/html" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Busca de Farmacias</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="themes/themeA.css" />
<link rel="stylesheet" href="themes/jquery.mobile.structure-1.3.2.min.css" />
<script src="themes/jquery-1.9.1.min.js"></script>
<script src="themes/jquery.mobile-1.3.2.min.js"></script>
<script src=http://code.jquery.com/jquery-1.10.2.min.js></script>
<script src=http://code.jquery.com/jquery-1.9.1.js></script>
<script type="text/javascript">
navigator.geolocation.getCurrentPosition(show_map);
function show_map(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
// let's show a map or do something interesting!
document.getElementById("latin").value = latitude;
document.getElementById("lngin").value = longitude;
}
</script>
</head>
<body style="background-image:url(themes/images/background.jpg); background-repeat:no-repeat; background-size:100%;">
<!-- HEADER -->
<div data-role="footer">
<div data-role="navbar" data-iconpos="left">
<ul>
<li><a href="busca.jsp" target="_parent" data-icon="search">Busca</a></li>
<li><a href="config.jsp" target="_parent" data-icon="bars">Sobre</a></li>
</ul>
</div><!-- /navbar -->
</div><!-- /footer -->
<!-- Tipo de Busca por farmacia -->
<!-- VAI PARA A SERVLET CIDADE -->
<div style="margin-top:10%; margin-right:5px; margin-left:5px;">
<a href="BuscaCidade?tipoBusca=cidadePop" target="_parent" data-role="button" data-theme="a" name="tipoBusca">
<div style="left:-120px;alignment-adjust:baseline; position:absolute; margin: .3em 0;">
<img src="themes/images/cidade.png" />
</div>
<div style="text-align:center; position:relative; margin: .3em 0;">
Busca por:<br/>Cidade
</div>
</a>
<!-- VAI PARA A SERVLET ESTADO -->
<a href="BuscaEstado?" data-role="button" data-theme="a" name="tipoBusca">
<div style="left:-120px;alignment-adjust:baseline; position:absolute; margin: .3em 0;">
<img src="themes/images/estado.png" />
</div>
<div style="text-align:center; position:relative; margin: .3em 0;">
Busca por:<br/>Estado
</div>
</a>
<!-- VAI PARA A SERVLET GPS -->
<a href="BuscaGPS?lat=latin&lng=lngin" data-role="button" data-theme="a" name="tipoBusca">
<div style="left:-120px;alignment-adjust:baseline; position:absolute; margin: .3em 0;">
<img src="themes/images/gps.png" />
</div>
<div style="text-align:center; position:relative; margin: .3em 0;">
Busca por:<br/>GPS
</div>
</a>
</div>
<!-- TESTE QUE EXIBE A LATITUDE E LONGITUDE -->
<input type="text" id="latin" />
<input type="text" id="lngin" />
</body>
</html>
como teste, eu usei esse input e ele está mostrando na tela os valores da geolocalização.
Servlet: o parametro está vindo do JSP. mas vem com o que estiver depois do sinal “=” do href, no exemplo acima, os requests da Servlet são preenchidos com o texto “latin” e “lngin” e não com os valores da geolocalização
protected void tratarRequisicao(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String lat = request.getParameter("lat");
String lng = request.getParameter("lng");
if(lat == null){
}if (lng == null){
}
String endereco = "gps_header.jsp";
RequestDispatcher dispatcher = request.getRequestDispatcher(endereco);
dispatcher.forward(request, response);
}