2017-07-19 43 views
0

我有一个servlet从HTML下拉页面获取参数。点击按钮时,数据被发送到servlet。它首次发送数据,但如果我留在页面上并从下拉菜单 中选择不同的值并单击提交按钮,则新数据未设置到会话变量中。会话变量不会通过servlet提交

我的servlet位于下面。我需要修改DoGet方法吗?同样,它第一次工作,但会话变量之后不会改变。

@WebServlet("/ListStudentServlet") 
public class ListStudentServlet extends HttpServlet { 
    private static final long serialVersionUID = 1L; 

    public ListStudentServlet() { 
     super(); 
     // TODO Auto-generated constructor stub 
    } 

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
     // TODO Auto-generated method stub 
    } 

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 

     String sessid = request.getParameter("studentid"); 
     ArrayList<FactStudentDataBean> result = new ArrayList<>(); 
     try (Connection con = JdbcUtil.getConnection()) { 

      String sql= "select F.Sessionid " 
        + "from FACT_STUDENT F " 
        + "where studentid = '"+sessid+"';"; 
      try (Statement st = con.createStatement()) { 

       ResultSet rs = st.executeQuery(sql); 
       while (rs.next()){ 
        result.add(new FactStudentDataBean(rs.getString(1))); 
       } 
       for (FactStudentDataBean factStudentDataBean : result) {  
        sessid = factStudentDataBean.getSessid();  
       }   
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
     catch (Exception e) { 
      e.printStackTrace(); 
     } 

     //Trying to set the session variable below, works the first time but anything after doesn't change 
     HttpSession session = request.getSession(true); 
     session.setAttribute("sessid", sessid); 
    } 
} 
+0

'request.getSes sion(true)'如果会话尚不存在,则创建一个新会话。会话cookie是否被设置为下一次提交? –

+0

我该如何设置? – thedude865

回答

-1

检查您在服务器端获得的jsessionid值。如果两者都不同,则通过传递false来代替true来尝试获取会话。

request.getSession(false); 

还转到tomcat管理器应用程序和监视活动会话。

希望这会有所帮助。

+0

我尝试将它设置为(false),但仍然没有任何结果。新数据不会发送回HTML – thedude865

0

你的代码有点“脏”。首先:您为什么要写这样这个sql查询?:

String sql= "select F.Sessionid " 
       + "from FACT_STUDENT F " 
       + "where studentid = '"+sessid+"';"; 

,不喜欢这个?:

String sql= "select F.Sessionid from FACT_STUDENT F where studentid = '"+sessid+"';"; 

二:请尽量使用prepareStatement而不是createStatement(为了解释什么我告诉请看这样一个问题:prepareStatement vs executeStatement

而对于现在的答案:我思,你必须使用session.getAttribute("sessid", sessid);

+0

,您是错误的。这不是你如何应用session.getAttribute。它不需要两个变量作为参数。 – thedude865