session取值为空

chensiwo 2012-08-29 11:23:21
登录成功 ,登录数据已经存入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:account

package 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;


}

}
...全文
496 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
MiceRice 2012-08-29
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 的回复:]
问个问题哦,Servlet init方法中能够获取服务器IP、端口等信息?
[/Quote]

这个有点麻烦,因为init方法中,还没有request;所以要用中间件提供的JMX来查询。
chensiwo 2012-08-29
  • 打赏
  • 举报
回复
谢谢你 这个问题 已已经解决!!是我Out了 才进来!![Quote=引用 10 楼 的回复:]

String sql ="INSERT INTO category {?,?,?}"; 这条sql文没有values也能正常执行吗?如果真好用那就out了,我学习了。
[/Quote]
chensiwo 2012-08-29
  • 打赏
  • 举报
回复
谢谢 已经查出来了!!
漆黑之勺 2012-08-29
  • 打赏
  • 举报
回复
String sql ="INSERT INTO category {?,?,?}"; 这条sql文没有values也能正常执行吗?如果真好用那就out了,我学习了。
小赛赛top 2012-08-29
  • 打赏
  • 举报
回复
问个问题哦,Servlet init方法中能够获取服务器IP、端口等信息?
MiceRice 2012-08-29
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 的回复:]
已经修改了 无法找到了!
[/Quote]

有啥无法找到的,我都提示的这么明显,就是这句话:
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
chensiwo 2012-08-29
  • 打赏
  • 举报
回复
已经修改了 无法找到了!
谢谢帮助!
[Quote=引用 5 楼 的回复:]

sorry,是CategoryImpl.java:39行。。。
[/Quote]
MiceRice 2012-08-29
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]
java.sql.SQLException: Can not issue data manipulation statements with executeQuery().
[/Quote]

这个错误的意思是:你企图用 executeQuery() 去执行 INSERT、DELETE、UPDATE 之类的语句而不是 SELECT

所以是错误的,请改用 execute()
cstur4 2012-08-29
  • 打赏
  • 举报
回复
sorry,是CategoryImpl.java:39行。。。
cstur4 2012-08-29
  • 打赏
  • 举报
回复
StatementImpl.java:497行的sql语句有问题,发出来
chensiwo 2012-08-29
  • 打赏
  • 举报
回复
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)
chensiwo 2012-08-29
  • 打赏
  • 举报
回复
是的 !谢谢你的指点!
但是现在出现了一个问题:

java.sql.SQLException: Can not issue data manipulation statements with executeQuery().
MiceRice 2012-08-29
  • 打赏
  • 举报
回复
你的代码:
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么?
MiceRice 2012-08-29
  • 打赏
  • 举报
回复
在你发的另一个帖子里面回复你了:
http://topic.csdn.net/u/20120829/12/ece1799a-37fb-46a2-abf0-e98568a6102e.html
小赛赛top 2012-08-29
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 的回复:]
引用 9 楼 的回复:
问个问题哦,Servlet init方法中能够获取服务器IP、端口等信息?


这个有点麻烦,因为init方法中,还没有request;所以要用中间件提供的JMX来查询。
[/Quote]

能说一下思路吗?

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧