登录成功 ,登录数据已经存入session ,经验证取证正常。但是向数据库添加数据的过程中 session取值竟然为空。费解!!1.登录servlet 登录成功并转向,session成功写入。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 取值竟然为空!!求解释!!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();
}@Override
protected 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
{
}
}———————————————————————————————————————————————————————PS:accountpackage it.shopping.pojo;public class Account
{ private int aid;
private String alogin;
private String apass;
public int getAid()
{
return aid;
}
public void setAid(int aid)
{
this.aid = aid;
}
public String getAlogin()
{
return alogin;
}
public void setAlogin(String alogin)
{
this.alogin = alogin;
}
public String getApass()
{
return apass;
}
public void setApass(String apass)
{
this.apass = apass;
}
}
Category.java
package it.shopping.pojo;public class Category
{ private int cid;
private String ctype;
private Boolean chot;
private Account account;
public int getCid()
{
return cid;
}
public void setCid(int cid)
{
this.cid = cid;
}
public String getCtype()
{
return ctype;
}
public void setCtype(String ctype)
{
this.ctype = ctype;
}
public Boolean getChot()
{
return chot;
}
public void setChot(Boolean chot)
{
this.chot = chot;
}
public Account getAccount()
{
return account;
}
public void setAccount(Account account)
{
this.account = account;
}
}
CategoryImp.java
package impl;import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;import utils.JdbcUtils;import it.shopping.dao.ICategory;
import it.shopping.pojo.Category;public class CategoryImpl implements ICategory{ @Override
public Category queryCategory(Category category)
{
Connection conn = null;
PreparedStatement pre = null;
String sql =" INSERT INTO category {?,?,?}";
ResultSet rs = null;
try
{
System.out.println("hello3.0");
conn = JdbcUtils.getConnection();
pre = conn.prepareStatement(sql);
System.out.println("hello4.0");
System.out.println(category.getAccount().getAid());
pre.setInt(1,category.getAccount().getAid());
pre.setString(2, category.getCtype());
pre.setBoolean(3,category.getChot());
System.out.println("hello6.0");
pre.executeQuery();
} catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}finally
{
JdbcUtils.free(rs, pre, conn);
}
return category;
}}
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 取值竟然为空!!求解释!!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();
}@Override
protected 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
{
}
}———————————————————————————————————————————————————————PS:accountpackage it.shopping.pojo;public class Account
{ private int aid;
private String alogin;
private String apass;
public int getAid()
{
return aid;
}
public void setAid(int aid)
{
this.aid = aid;
}
public String getAlogin()
{
return alogin;
}
public void setAlogin(String alogin)
{
this.alogin = alogin;
}
public String getApass()
{
return apass;
}
public void setApass(String apass)
{
this.apass = apass;
}
}
Category.java
package it.shopping.pojo;public class Category
{ private int cid;
private String ctype;
private Boolean chot;
private Account account;
public int getCid()
{
return cid;
}
public void setCid(int cid)
{
this.cid = cid;
}
public String getCtype()
{
return ctype;
}
public void setCtype(String ctype)
{
this.ctype = ctype;
}
public Boolean getChot()
{
return chot;
}
public void setChot(Boolean chot)
{
this.chot = chot;
}
public Account getAccount()
{
return account;
}
public void setAccount(Account account)
{
this.account = account;
}
}
CategoryImp.java
package impl;import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;import utils.JdbcUtils;import it.shopping.dao.ICategory;
import it.shopping.pojo.Category;public class CategoryImpl implements ICategory{ @Override
public Category queryCategory(Category category)
{
Connection conn = null;
PreparedStatement pre = null;
String sql =" INSERT INTO category {?,?,?}";
ResultSet rs = null;
try
{
System.out.println("hello3.0");
conn = JdbcUtils.getConnection();
pre = conn.prepareStatement(sql);
System.out.println("hello4.0");
System.out.println(category.getAccount().getAid());
pre.setInt(1,category.getAccount().getAid());
pre.setString(2, category.getCtype());
pre.setBoolean(3,category.getChot());
System.out.println("hello6.0");
pre.executeQuery();
} catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}finally
{
JdbcUtils.free(rs, pre, conn);
}
return category;
}}
解决方案 »
- fckeditor提交大量数据到后台问题
- 求js去掉页面浏览器(IE或者FF都可以)的边栏实例
- [90分在线等] 实现子页列表框向父页列表框传值,父页列表框无刷新增加
- 那种向导式的表单的数据如何保存?类似上一步、下一步的表单
- FCKeditor的确是个十分强大的编辑器,为什么我的不可上传图片等其它文件到服务器?
- 一个棘手的问题,请各位高手指教。
- 用jexcelapi将数据导入到EXCEL表中问题
- 最简单的JSP+javabean 应用,可是,就是出错,为什么呢,大家来看看咯!
- 怎样来解析域名呀?
- 谁帮我搞定这个文件,50分送他/她
- 分享 对request,session,application作用域形象理解
- 图片上传服务器
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);
}
中间是漏了else么?
但是现在出现了一个问题:java.sql.SQLException: Can not issue data manipulation statements with executeQuery().
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:988)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:974)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:919)
at com.mysql.jdbc.StatementImpl.checkForDml(StatementImpl.java:497)
at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:2223)
at impl.CategoryImpl.queryCategory(CategoryImpl.java:39)
at ser.CategorySer.doPost(CategorySer.java:58)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at it.shopping.filter.EnCodingFilter.doFilter(EnCodingFilter.java:29)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:999)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:565)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:309)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)
这个错误的意思是:你企图用 executeQuery() 去执行 INSERT、DELETE、UPDATE 之类的语句而不是 SELECT所以是错误的,请改用 execute()
有啥无法找到的,我都提示的这么明显,就是这句话:
pre.setString(2, category.getCtype());
pre.setBoolean(3,category.getChot());
System.out.println("hello6.0");
pre.executeQuery();你执行的SQL是: String sql ="INSERT INTO category {?,?,?}"; 显然不是SELECT语句,不能用 executeQuery
这个有点麻烦,因为init方法中,还没有request;所以要用中间件提供的JMX来查询。
http://topic.csdn.net/u/20120829/12/ece1799a-37fb-46a2-abf0-e98568a6102e.html