关于session的问题!!sos 在线恭候!

flyshp 2003-08-18 05:02:59
jsp 中的session 和 servelet 中定义的session 到底可不可以相互调用??

如果可以 有什么特别的要求么??
我试了很多次?? 都是失败??希望高手指点!!
...全文
45 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
flyshp 2003-08-19
  • 打赏
  • 举报
回复
结贴..........
flyshp 2003-08-19
  • 打赏
  • 举报
回复
是 WEB-INF 文件下的!!

我打包的servlet 的war 文件 和 jsp 都应该在自己固定的目录下编译!!
你说的应该有道理!!但我该怎么解决呢?
wellsoon 2003-08-19
  • 打赏
  • 举报
回复

这个web.xml是哪个文件夹下的啊,

看来应该是因为jsp和servlet不在同一个webapp造成的.

上班时间,不敢QQ的:)



flyshp 2003-08-19
  • 打赏
  • 举报
回复
我用的是weblogic7

web.xml 是
<?xml version="1.0" ?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
flyshp 2003-08-19
  • 打赏
  • 举报
回复
wellsoon(wellsoon)
你有QQ 么??

我想知道细节问题@@
wellsoon 2003-08-19
  • 打赏
  • 举报
回复
代码是没有问题的,

你的servlet和jsp好象不在一个webapp里吧???
("../jspweb/servlet1");是转向上层目录的的servlet了。

假设jsp的URL为:
http://localhost:8080/test/jsp1.jsp,

那么你的servlet的Url为:
http://localhost:8080/jspweb/servlet1

这个时候,如果http://localhost:8080/test/
和http://localhost:8080/jspweb/下都有web.xml

那么它们是不在同一个webapp里的,

那样,它们的session 是不一样的。

你可以试着在jsp和servlet里分别写上:

System.out.println("新创建的session Id 为:"+se.getSession().getId());


注意观察session还是一样的么???


顺便可以把你的web.xml贴出来么???

flyshp 2003-08-19
  • 打赏
  • 举报
回复
weblogic7.0

你的QQ 是多少?

现在可以上QQ 么?
kevincom 2003-08-19
  • 打赏
  • 举报
回复
不可能啊
我这边都是正确的啊
你用的是什么服务器软件啊?
flyshp 2003-08-19
  • 打赏
  • 举报
回复
?????????????????????????????????

还是null] 啊!!!
kevincom 2003-08-19
  • 打赏
  • 举报
回复
试试下面代码,我在resin中调试过
test.jsp:
<%
session.setAttribute("name","Hello");
String pass = (String)session.getAttribute("name");
out.println(pass);
%>
<input type="button" value="go to servlet" onclick="document.location.href='servlet/Servlet1'">

Servlet1.java:
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class Servlet1 extends HttpServlet {
static final private String CONTENT_TYPE = "text/html; charset=GBK";
public void init() throws ServletException {
}
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(true);

out.println("name的值是:"+(String)session.getAttribute("name"));
out.println("<p>The servlet has received a GET. This is the reply.</p>");
out.println("</body></html>");
}
public void destroy() {
}
}
flyshp 2003-08-19
  • 打赏
  • 举报
回复
这是ejbpackage jsp;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class Servlet1 extends HttpServlet {
static final private String CONTENT_TYPE = "text/html; charset=GBK";
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
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 ();
session.setAttribute("pass","12345") ;
out.println("name的值是:"+(String)session.getAttribute("name"));
out.println("<p>The servlet has received a GET. This is the reply.</p>");
out.println("</body></html>");
}
//Clean up resources
public void destroy() {
}
}
flyshp 2003-08-19
  • 打赏
  • 举报
回复
我找过你的 : wellsoon(wellsoon)

首先 对你的参与我十分感谢!

我用你多的代码试过了!@@ 结果总是null的!

这是我的jsp

<%
session.setAttribute("name","Hello");
String pass = (String)session.getAttribute("pass");

response.sendRedirect("../jspweb/servlet1");
System.out.println(pass);
%>
wellsoon 2003-08-18
  • 打赏
  • 举报
回复
当然可以用,

jsp里因为session是内置对象,不需要单独声明,
但是在servlet里, 是需要自己声明的,

HttpSession session=request.getSession ();

例子如下:

jsp1.jsp:

<%@ page contentType="text/html; charset=GBK" %>
<html>
<head>
<title>
jsp1
</title>
</head>
<body bgcolor="#ffffff">
<h1>
JBuilder Generated JSP
</h1>
<%
session.setAttribute("name","Hello");
response.sendRedirect("/servlet1");

%>
</body>
</html>

servlet1.java servlet里写:

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 ();//获取request里的session对象
out.println("name的值是:"+session.getAttribute("name"));
out.println("<p>The servlet has received a GET. This is the reply.</p>");
out.println("</body></html>");

StevenWSF 2003-08-18
  • 打赏
  • 举报
回复
当然可以用,不可能失效
KillAllError 2003-08-18
  • 打赏
  • 举报
回复
jsp的本质就是servelet,怎么会不行呢?
你再试验一下。
不行把代码贴出来
stonewang 2003-08-18
  • 打赏
  • 举报
回复
大家都是在服务器的session,应该可以互调

81,092

社区成员

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

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