2017-07-19 47 views
0

私は、HTMLドロップダウンページからパラメータを取得するサーブレットを持っています。ボタンをクリックすると、データがサーブレットに送信されます。データが初めて送信されたときに動作しますが、ページ上に残り、ドロップダウンメニューから別の値を選択して送信ボタンをクリックすると、新しいデータはセッション変数に設定されません。サーブレットによるセッション変数の変更がありません

私のサーブレットは以下の通りです。 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) 'は、セッションがまだ存在しない場合に新しいセッションを作成します。セッションクッキーが次のサブミットに設定されていますか? –

+0

どうすれば設定できますか? – thedude865

答えて

-1

サーバー側で取得しているjsessionidの値を確認してください。両方が異なる場合は、trueではなくfalseを渡してセッションを取得してください。

request.getSession(false); 

また、tomcat managerアプリケーションにアクセスし、アクティブなセッションを監視します。

希望すると、これが役立ちます。

+0

私はそれを(偽)に設定しようとしましたが、何もありませんでした。新しいデータはHTML – thedude865

0

あなたのコードはちょっと汚いです。まず第一に:常に何の説明についてはprepareStatementの代わりcreateStatementを(使用しよう:なぜあなたはこの?:

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

はなく、この?:

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

セカンドが好きなように、このSQLクエリを書いていますprepareStatement vs executeStatement

そして今答え:Ithinkあなたがsession.getAttribute("sessid", sessid);

+0

に送り返されません。間違っています。これはsession.getAttributeの適用方法ではありません。パラメータとして2つの変数を取るわけではありません。 – thedude865

関連する問題