Html 在JSP中连续提交表单后保留变量值

Html 在JSP中连续提交表单后保留变量值,html,forms,jsp,Html,Forms,Jsp,我已经编写了以下两个jsp文件。第一个文件,即submit.jsp有一个表单,其中用户提交了她的查询,记录了她的城市、纬度和经度,并将她转发到另一个jsp页面,该页面也有一个表单,在该表单中,她可以根据自己的需要多次提交查询,我希望保留城市、纬度和经度的值,但这些值会被重置。这两个文件的代码如下: submit.jsp <%@ page contentType="text/html; charset=iso-8859-1" language="java" %> <html>

我已经编写了以下两个jsp文件。第一个文件,即submit.jsp有一个表单,其中用户提交了她的查询,记录了她的城市、纬度和经度,并将她转发到另一个jsp页面,该页面也有一个表单,在该表单中,她可以根据自己的需要多次提交查询,我希望保留城市、纬度和经度的值,但这些值会被重置。这两个文件的代码如下:

submit.jsp

<%@ page contentType="text/html; charset=iso-8859-1" language="java" %>
<html>
<head>
 <script type="text/javascript">
  function getLocation(){
  if (navigator.geolocation){
      navigator.geolocation.getCurrentPosition(showPosition);
    }
  }
  function displayLocation(latitude,longitude){
    var request = new XMLHttpRequest();

    var method = 'GET';
    var url = 'http://maps.googleapis.com/maps/api/geocode/json?latlng='+latitude+','+longitude+'&sensor=true';
    var async = true;

    request.open(method, url, async);
    request.onreadystatechange = function(){
      if(request.readyState == 4 && request.status == 200){
        var data = JSON.parse(request.responseText);
        var address = data.results[0];
        //document.write(address.formatted_address);
        var city=document.getElementById("city");
        var n = address.formatted_address.split(",");
        city.value = n[n.length-3];
      }
    };
    request.send();
  };

function showPosition(position){
//var x = document.getElementById("demo");
var latitude=document.getElementById("latitude"),longitude=document.getElementById("longitude");
//x.innerHTML="+" + position.coords.latitude + "+" + position.coords.longitude;
latitude.value = position.coords.latitude;
longitude.value = position.coords.longitude;
displayLocation(latitude.value,longitude.value);
}

</script>

</head>
<body onload="getLocation()">
<form name="frm" method="post" action="process.jsp">
<input type="text" name="myQuery" placeholder="Type here">
<input name="latitude" id="latitude" type="hidden">
<input name="longitude" id="longitude" type="hidden">
<input name="city" id="city" type="hidden">
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>

函数getLocation(){
if(导航器.地理位置){
navigator.geolocation.getCurrentPosition(showPosition);
}
}
功能显示位置(纬度、经度){
var request=new XMLHttpRequest();
var方法='GET';
var url='1〕http://maps.googleapis.com/maps/api/geocode/json?latlng=“+纬度+”、“+经度+”&传感器=真”;
var async=true;
open(方法、url、异步);
request.onreadystatechange=函数(){
if(request.readyState==4&&request.status==200){
var data=JSON.parse(request.responseText);
var地址=数据。结果[0];
//文件。写入(地址。格式化的地址);
var city=document.getElementById(“城市”);
var n=地址。格式化的地址。拆分(“,”);
city.value=n[n.length-3];
}
};
request.send();
};
功能显示位置(位置){
//var x=document.getElementById(“演示”);
var纬度=document.getElementById(“纬度”),经度=document.getElementById(“经度”);
//x、 innerHTML=“+”+位置.坐标.纬度+“+”+位置.坐标.经度;
latitude.value=位置.coords.latitude;
longitude.value=position.coords.longitude;
显示位置(纬度值、经度值);
}
process.jsp

<%@ page contentType="text/html; charset=iso-8859-1" language="java" %>
<%! String city; String latitude; String longitude; %>
<form name="frm" method="post" action="process.jsp">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="22%">&nbsp;</td>
<td width="78%">&nbsp;</td>
</tr>
<tr>
<td>&nbsp; </td>
<td><input type="text" name="myQuery" placeholder="Type here"></td>
</tr>
<tr>
<td>&nbsp;</td>
<input name="latitude" id="latitude" type="hidden" value="${latitude}" >
<input name="longitude" id="longitude" type="hidden" value="${longitude}" >
<input name="city" id="city" type="hidden" value="${city}"  >
<td><input type="submit" name="submit" value="Submit"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
</table>
</form>

<%
String query=request.getParameter("myQuery");
String city=request.getParameter("city");
String latitude=request.getParameter("latitude");
String longitude=request.getParameter("longitude");
%>
<html>
<body>
<p>Query phrase is : <%=query%></p>
<p>User's city : <%=city%></p>
</body>
</html>

查询短语为:

用户所在城市:


我不熟悉JSP。因此,任何人都可以帮助我修复给定的代码。

尝试获取文件开头的参数

<%
String query=request.getParameter("myQuery");
String city=request.getParameter("city");
String latitude=request.getParameter("latitude");
String longitude=request.getParameter("longitude");
%>

尝试获取文件开头的参数

<%
String query=request.getParameter("myQuery");
String city=request.getParameter("city");
String latitude=request.getParameter("latitude");
String longitude=request.getParameter("longitude");
%>

像这样

<%@ page contentType="text/html; charset=iso-8859-1" language="java" %>
    <html>
    <head>
     <script type="text/javascript">
      function getLocation(){
      if (navigator.geolocation){
          navigator.geolocation.getCurrentPosition(showPosition);
        }
      }
      function displayLocation(latitude,longitude){
        var request = new XMLHttpRequest();

    var method = 'GET';
    var url = 'http://maps.googleapis.com/maps/api/geocode/json?latlng='+latitude+','+longitude+'&sensor=true';
    var async = true;

    request.open(method, url, async);
    request.onreadystatechange = function(){
      if(request.readyState == 4 && request.status == 200){
        var data = JSON.parse(request.responseText);
        var address = data.results[0];
        //document.write(address.formatted_address);
        var city=document.getElementById("city");
        var n = address.formatted_address.split(",");
        city.value = n[n.length-3];
      }
    };
    request.send();
  };

function showPosition(position){
//var x = document.getElementById("demo");
var latitude=document.getElementById("latitude"),longitude=document.getElementById("longitude");
//x.innerHTML="+" + position.coords.latitude + "+" + position.coords.longitude;
latitude.value = position.coords.latitude;
longitude.value = position.coords.longitude;
displayLocation(latitude.value,longitude.value);
}

</script>

</head>
<body onload="getLocation()">
<%! String city; String latitude; String longitude; %>
<%
String query=request.getParameter("myQuery");
String city=request.getParameter("city");
String latitude=request.getParameter("latitude");
String longitude=request.getParameter("longitude");
%>
<form name="frm" method="post" action="submit.jsp">
<input type="text" name="myQuery" placeholder="Type here">
<input name="latitude" id="latitude" type="hidden">
<input name="longitude" id="longitude" type="hidden">
<input name="city" id="city" type="hidden">
<input type="submit" name="submit" value="Submit">
</form>
<p>Query phrase is : <%=query%></p>
<p>User's city : <%=city%></p>
</body>
</html>

函数getLocation(){
if(导航器.地理位置){
navigator.geolocation.getCurrentPosition(showPosition);
}
}
功能显示位置(纬度、经度){
var request=new XMLHttpRequest();
var方法='GET';
var url='1〕http://maps.googleapis.com/maps/api/geocode/json?latlng=“+纬度+”、“+经度+”&传感器=真”;
var async=true;
open(方法、url、异步);
request.onreadystatechange=函数(){
if(request.readyState==4&&request.status==200){
var data=JSON.parse(request.responseText);
var地址=数据。结果[0];
//文件。写入(地址。格式化的地址);
var city=document.getElementById(“城市”);
var n=地址。格式化的地址。拆分(“,”);
city.value=n[n.length-3];
}
};
request.send();
};
功能显示位置(位置){
//var x=document.getElementById(“演示”);
var纬度=document.getElementById(“纬度”),经度=document.getElementById(“经度”);
//x、 innerHTML=“+”+位置.坐标.纬度+“+”+位置.坐标.经度;
latitude.value=位置.coords.latitude;
longitude.value=position.coords.longitude;
显示位置(纬度值、经度值);
}
查询短语为:

用户所在城市:

像这样

<%@ page contentType="text/html; charset=iso-8859-1" language="java" %>
    <html>
    <head>
     <script type="text/javascript">
      function getLocation(){
      if (navigator.geolocation){
          navigator.geolocation.getCurrentPosition(showPosition);
        }
      }
      function displayLocation(latitude,longitude){
        var request = new XMLHttpRequest();

    var method = 'GET';
    var url = 'http://maps.googleapis.com/maps/api/geocode/json?latlng='+latitude+','+longitude+'&sensor=true';
    var async = true;

    request.open(method, url, async);
    request.onreadystatechange = function(){
      if(request.readyState == 4 && request.status == 200){
        var data = JSON.parse(request.responseText);
        var address = data.results[0];
        //document.write(address.formatted_address);
        var city=document.getElementById("city");
        var n = address.formatted_address.split(",");
        city.value = n[n.length-3];
      }
    };
    request.send();
  };

function showPosition(position){
//var x = document.getElementById("demo");
var latitude=document.getElementById("latitude"),longitude=document.getElementById("longitude");
//x.innerHTML="+" + position.coords.latitude + "+" + position.coords.longitude;
latitude.value = position.coords.latitude;
longitude.value = position.coords.longitude;
displayLocation(latitude.value,longitude.value);
}

</script>

</head>
<body onload="getLocation()">
<%! String city; String latitude; String longitude; %>
<%
String query=request.getParameter("myQuery");
String city=request.getParameter("city");
String latitude=request.getParameter("latitude");
String longitude=request.getParameter("longitude");
%>
<form name="frm" method="post" action="submit.jsp">
<input type="text" name="myQuery" placeholder="Type here">
<input name="latitude" id="latitude" type="hidden">
<input name="longitude" id="longitude" type="hidden">
<input name="city" id="city" type="hidden">
<input type="submit" name="submit" value="Submit">
</form>
<p>Query phrase is : <%=query%></p>
<p>User's city : <%=city%></p>
</body>
</html>

函数getLocation(){
if(导航器.地理位置){
navigator.geolocation.getCurrentPosition(showPosition);
}
}
功能显示位置(纬度、经度){
var request=new XMLHttpRequest();
var方法='GET';
var url='1〕http://maps.googleapis.com/maps/api/geocode/json?latlng=“+纬度+”、“+经度+”&传感器=真”;
var async=true;
open(方法、url、异步);
request.onreadystatechange=函数(){
if(request.readyState==4&&request.status==200){
var data=JSON.parse(request.responseText);
var地址=数据。结果[0];
//文件。写入(地址。格式化的地址);
var city=document.getElementById(“城市”);
var n=地址。格式化的地址。拆分(“,”);
city.value=n[n.length-3];
}
};
request.send();
};
功能显示位置(位置){
//var x=document.getElementById(“演示”);
var纬度=document.getElementById(“纬度”),经度=document.getElementById(“经度”);
//x、 innerHTML=“+”+位置.坐标.纬度+“+”+位置.坐标.经度;
latitude.value=位置.coords.latitude;
longitude.value=position.coords.longitude;
显示位置(纬度值、经度值);
}
查询短语为:

用户所在城市:


查询短语为:

用户所在城市:


查询短语为:

用户所在城市:


不,先生,它不工作。在您的代码中,在submit.jsp中,您将获得纬度、经度和位置的值。当表单提交时,您正在提交的值。所以当表单第一次提交时,它将获得这些值。但是在process.jsp页面中,当您单击submit按钮时,它将没有纬度n经度值(您的javascript代码为您提供了submit.jsp上的位置)这就是为什么我要问,即使表单被多次提交,是否有任何方法可以保留这些变量的值。可以只使用一页提交重定向到同一页。但是,先生,如果我重定向到另一页,像上面我所做的那样,是没有办法做到这一点的??因为我需要将表单提交重定向到另一个页面。不,先生,它不工作。在您的代码中,在submit.jsp中,您将获得纬度、经度和位置的值。当表单提交时,您正在提交的值。所以当表单第一次提交时,它将获得这些值。但是在process.jsp页面中