你不用试了!
我在jsp中调serverlet 的session值 也是null 的
也就是说: 在 jsp 中 和 servelet 中定义的session 不可以相互调用!
我在jsp中调serverlet 的session值 也是null 的
也就是说: 在 jsp 中 和 servelet 中定义的session 不可以相互调用!
解决方案 »
- org.apache.jasper.JasperException: Cannot find any information on property 'qpag
- 在Eclipse中,用 <jsp:plugin> 调用Applet,总是没有反应。请高手指教,多谢!
- 循环的问题
- netbeans 开发 struts,没有所见即所得功能。
- 关于取出的数据是乱码的问题
- 马上就要写OA了,希望大家给点意见啊?
- 纯java驱动连数据库,单是jsp能调用,但做成javabean就不能了!麻烦各位大哥帮忙
- 新手提问之六:大小写的问题?马上送分
- 请看看这个iframe的问题
- 怎样将*.war中的角色和j2EE容器(如tomcat)中的用户相关联?
- 请教哪为知道这个异常:com.caucho.vfs.ClientdisconnectException
- 寻找jsp的例子
胡闹。
再说,JSp也是最后要编译成Sevlet运行的啊!!!
import java.util.*;
import com.sun.image.codec.jpeg.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.awt.*;
import java.awt.image.*;public class getImg extends HttpServlet {private String s;
private Font mFont=new Font("宋体", Font.PLAIN,20);//设置字体
//处理post
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException {doGet(request,response);
}
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException {
//取得一个1000-9999的随机数HttpSession session=request.getSession (true);s=(String)session.getAttribute("getImg");response.setContentType("image/gif");ServletOutputStream out=response.getOutputStream();BufferedImage image=new BufferedImage(75,32,BufferedImage.TYPE_INT_RGB);Graphics gra=image.getGraphics();
//设置背景色
gra.setColor(Color.orange);gra.fillRect(1,1,73,30);
//设置字体色
gra.setColor(Color.black);gra.setFont(mFont);
//输出数字
char c;for(int i=0;i<4;i++) {c=s.charAt(i);gra.drawString(c+"",i*17+4,28);}JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(out);encoder.encode(image);out.close();}
public String getS(){
return s;
}}
这是一个调用的,你看看吧,希望对你有帮助
有意义么?
HttpSession session = Request.getSession();
我把代码全贴出来了,高手帮我瞧瞧。
jsp 代码: String serveletAddress="http://1it888888:8080/qbean/servlet/smic.QBeanExcelServlet";
String name="ComingLot";
QQueryBean.reset();
QQueryBean.setConnection(ConBean.getConnection("mestprd"));
QQueryBean.setQueryInfo("D:\\QBean\\QBean.xml", "ComingLots");
if (eqpgrp!=null)
{QQueryBean.setParamByName("eqpgrp",eqpgrp);}
System.out.println(QQueryBean.getSqlString());
vs=QQueryBean.getResultVector();
session.setAttribute("data",vs);
session.setAttribute("name","Hello");
ConBean.release("mestprd",QQueryBean.getUsingConnection());
response.sendRedirect(serveletAddress); sevlet代码:
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException { // HSSFSheet sheet = null;
HSSFWorkbook wb = null;
HttpSession session=req.getSession (true);
if (session!=null)
System.out.println("session is not null .");
Vector data = (Vector) session.getAttribute("data");
sheetName = (String) session.getAttribute("name");
System.out.println("name is "+sheetName);
if (data != null && data.size() > 0) {
wb = new HSSFWorkbook(); // Define sheet name
Date dt = new java.util.Date();
sheetName = sheetName + " " + dt.toLocaleString(); this.createSheet(data, wb, sheetName); ServletOutputStream stream = res.getOutputStream();
res.setContentType("application/vnd.ms-excel");
wb.write(stream);
stream.flush();
stream.close(); } else{
System.out.println("Data is null .");
} }
HttpSession session=req.getSession (true);是指如果session不存在,则创建一个新的session,那么
if (session!=null)
System.out.println("session is not null .");是永远执行不到的.public HttpSession getSession(boolean create)
Returns the current HttpSession associated with this request or, if if there is no current session and create is true, returns a new session. If create is false and the request has no valid HttpSession, this method returns null.
jsp1.jsp里<%
session.setAttribute("name","Hello");
response.sendRedirect("/servlet1");
%>然后servlet1.java里 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>Servlet1</title></head>");
out.println("<body bgcolor=\"#ffffff\">");
HttpSession session=request.getSession ();
out.println("name的值是:"+session.getAttribute("name"));
out.println("<p>The servlet has received a GET. This is the reply.</p>");
out.println("</body></html>");
}是可以取得session里的东西的.
...=hs.getAttribute(name)