我想通过Sessions获得输入数量。为了得到精确的输入数量,到目前为止,我有代码
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String pro=request.getParameter("inputs");
HttpSession session=request.getSession(true);
Integer Counter =
(Integer)session.getAttribute("Counter");
session.setAttribute("input", inp);
if(Counter==null){
Counter=0;
println((String)session.getAttribute("input") +""+Counter);
}
if((session.getAttribute("input").equals(inp))){
Counter++;
println(session.getAttribute("input")+"" +Counter);
}
答案 0 :(得分:0)
我会做更多的事情:
public void doPost(HttpServletRequest request, HttpServletResponse response) {
String word = request.getParameter("word");
HttpSession httpSession = request.getSession();
Integer numRequestsForWord = (Integer)httpSession.getAttribute(word);
if( numRequestsForWord == null )
numRequestsForWord = 1;
else
numRequestsForWord += 1;
httpSession.setAttribute(word, numRequestsForWord);
if( numRequestsForWord == 1 )
System.out.println( word );
else
System.out.println( word + "(" + numRequestsForWord + ")");
}
这需要发帖 - 我不确定您是如何调用此方法的。