登录成功 ,登录数据已经存入session ,经验证取证正常。但是向数据库添加数据的过程中 session取值竟然为空。费解!!
1.登录servlet 登录成功并转向,session成功写入。
- Java code
package ser;import impl.AccountImpl;import it.shopping.dao.IAccount;import it.shopping.pojo.Account;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class AccountSer extends HttpServlet{ private IAccount accountImpl = new AccountImpl(); private static final long serialVersionUID = -3708568559174453120L; public AccountSer() { super(); } public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String status = request.getParameter("status"); if(status.equals("login")) { Account account = new Account(); account.setAlogin(request.getParameter("alogin")); account.setApass(request.getParameter("apass")); account =accountImpl.queryAccount(account); if(account == null) { request.setAttribute("error", "登录失败"); request.getRequestDispatcher("/alogin.jsp").forward(request, response); // response.sendRedirect("alogin.jsp"); } { request.getSession().setAttribute("account", account); // response.sendRedirect("/t31/admin/index.jsp"); // 重定向和请求装啊session值都不会失效! request.getRequestDispatcher("/admin/index.jsp").forward(request, response); } } } public void init() throws ServletException { // Put your code here }}
2.跳转至index.jsp成功,然后添加数据成功,跳转至servlet 即CategorySer.java成功
但是在category.setAccount((Account) req.getSession().getAttribute("account"));
session 取值竟然为空!!求解释!!
- Java code
package ser;import impl.CategoryImpl;import it.shopping.dao.ICategory;import it.shopping.pojo.Account;import it.shopping.pojo.Category;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class CategorySer extends HttpServlet{ private ICategory categoryImpl = new CategoryImpl(); public void destroy() { super.destroy(); }@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{ this.doPost(req,resp);}@Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String status = req.getParameter("status"); if(status.equals("savaCategory")); { Category category = new Category(); category.setCtype(req.getParameter("ctype")); category.setChot(Boolean.parseBoolean(req.getParameter("chot"))); category.setAccount((Account) req.getSession().getAttribute("account")); System.out.println(category.getAccount()); categoryImpl.queryCategory(category); resp.sendRedirect("/t31/admin/main.jsp"); } } public void init() throws ServletException { }}