0

I have a servlet that gets parameters from an HTML dropdown page. On button click the data is sent to the servlet. It works the first time the data is sent, but if I stay on the page and select a different value from the dropdown and click the submit button, the new data is not set into the session variable.

My servlet is below. Do I need to modify the DoGet method? Again, it works the first time but the session variable doesn't change afterwards.

@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);
    }
}
CDspace
  • 2,639
  • 18
  • 30
  • 36
thedude865
  • 31
  • 5

2 Answers2

0

Your code is a little bit "dirty". First of all: why you are writing this sql query like this?:

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

and not like this?:

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

Second: Always try to use prepareStatement instead of createStatement (for explanation of what i am telling please see this question:prepareStatement vs executeStatement)

And for the answer now: Ithink you must use session.getAttribute("sessid", sessid);

Fotis Grigorakis
  • 363
  • 1
  • 3
  • 16
  • you are incorrect. That's not how you apply session.getAttribute. It doesn't take two variables as parameters. – thedude865 Jul 22 '17 at 13:18
-1

Check the jsessionid values you are getting on server side. If both are different then try to get session by passing false instead of true.

request.getSession(false);

Also goto tomcat manager application and monitor active sessions.

Hope this will help.

ap91
  • 78
  • 1
  • 7