【问题标题】:Getting inputs num Sessions获取输入 num Sessions
【发布时间】:2017-09-09 21:23:18
【问题描述】:

我想通过 Sessions 获取输入 num。为了获得确切输入的数量,到目前为止我有代码

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);
    }

【问题讨论】:

  • 输出应该去哪里?回到浏览器还是别的什么?
  • 只是 servlet 显示输出
  • 向用户展示无处可去
  • @stdunbar 如果你能帮助我,我将不胜感激

标签: javascript java jsp servlets jakarta-ee


【解决方案1】:

我会做一些类似的事情:

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 + ")");
}

这需要一个 POST - 我不确定你是如何调用这个方法的。

【讨论】:

  • 谢谢@stdunbar,但我需要它在同一个会话中显示其他值。例如,苹果(2)、果汁(3)、豆类(5)之类的
  • 你在吗?
猜你喜欢
  • 2016-11-07
  • 1970-01-01
  • 1970-01-01
  • 2012-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多