我有一個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);
}
}
'request.getSes sion(true)'如果會話尚不存在,則創建一個新會話。會話cookie是否被設置爲下一次提交? –
我該如何設置? – thedude865