我要求我需要保存变量的值。我的问题是我需要从一个网页发送一个值到servlet,其中变量的值是第一次为null但是当我从选择框中选择一个值时它会调整到servlet并且它会带有值,但是我的问题在这里我需要在选择值后重新页面。所以现在当我这样做时,值再次变为零并且操作没有发生,我可以在从选择中选择一些值后保存变量的值吗?
这是我的代码..
<body>
Select Country:
<select id="country">
<option>Select Country</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<input type="button" value="Reload page" onclick="reloadPage()">
</body>
<script>
function reloadPage(){
location.reload();
}
</script>
<script>
$(document).ready(function() {
$('#country').change(function(event) {
var $country=$("select#country").val();
$.get('JsonServlet',{countryname:$country},function(responseJson) {
var $select = $('#states');
$select.find('option').remove();
$.each(responseJson, function(key, value) {
$('<option>').val(key).text(value).appendTo($select);
});
});
});
});
</script>
这是我的servlet
public class JsonServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
String value = request.getParameter("countryname");
System.out.println("comes from ajax" + value);
JsonGenerator generator = new JsonGenerator();
Entry entry = null;
if (value != null) {
HttpSession session = request.getSession();
session.setAttribute("value", value);
entry = generator.aMethod2Json(value);
Gson g = new Gson();
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(g.toJson(entry));
} else {
entry = generator.aMethod2Json("1");
Gson g = new Gson();
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(g.toJson(entry));
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP
* <code>GET</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP
* <code>POST</code> method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
答案 0 :(得分:6)
我希望一些代码示例可以帮助您:
一个简单的计数器
为了演示servlet的生命周期,我们将从一个简单的例子开始。例3-1显示了一个servlet,它计算并显示它被访问的次数。为简单起见,它输出纯文本。
例3-1。一个简单的计数器
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class SimpleCounter extends HttpServlet {
int count = 0;
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/plain");
PrintWriter out = res.getWriter();
count++;
out.println("Since loading, this servlet has been accessed " +
count + " times.");
}
}
否则,如果你想要更高级的东西,一个好的选择是使用A整体计数器:
例3-2。整体计数器
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HolisticCounter extends HttpServlet {
static int classCount = 0; // shared by all instances
int count = 0; // separate for each servlet
static Hashtable instances = new Hashtable(); // also shared
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/plain");
PrintWriter out = res.getWriter();
count++;
out.println("Since loading, this servlet instance has been accessed " +
count + " times.");
// Keep track of the instance count by putting a reference to this
// instance in a Hashtable. Duplicate entries are ignored.
// The size() method returns the number of unique instances stored.
instances.put(this, this);
out.println("There are currently " +
instances.size() + " instances.");
classCount++;
out.println("Across all instances, this servlet class has been " +
"accessed " + classCount + " times.");
}
}
此HolisticCounter使用count实例变量跟踪自己的访问计数,使用classCount类变量的共享计数,以及具有实例哈希表的实例数(另一个必须是类变量的共享资源)。
答案 1 :(得分:1)
如何将变量存储到servlet会话对象中。 因此,它在请求范围内的更改期间也保持活动状态
此外,您可以在每次请求期间检查变量的值和 以适当的方式行事。
答案 2 :(得分:0)
在选择值之后和刷新页面之前,您可以对servlet进行ajax调用。
或
通过调用java脚本函数在会话中设置值。
<script>
function setMyVar(){
<% session.setAttribute( "username", Value ); %>
}
</script>
获取servlet端的session属性
session.getAttribute("userName")