请教关于Session问题

gebi0107 2014-03-25 02:04:04
小白问题。
编写ExS.html ExSession.java ExS01.html ExSession01.java
ExS.html驱动ExSession.class生成页面,创建一个session 这个页面包含一个链接B.html 然后ExS01.html驱动BExSession01.class 那么生成的页面中能获取到ExSession创建的session吗? 语言比较混乱,勿怪。
我的总是获取不到 是NULL.
ExS.html:
<form method = "post" action = "ExSession">
<p align = "left">
<font size = "5"><b>Font of Page</b></font>
</p>
<p></p>
<p align = "left">
<input type = "submit" value = "go to Sub Page">
</p>
</form>

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


public class ExSession extends HttpServlet {

public synchronized void doPost(HttpServletRequest request,HttpServletResponse response)
throws IOException,ServletException {
response.setContentType("text/html;charset=gb2312");
PrintWriter out = response.getWriter();
request.setCharacterEncoding("gb2312");

HttpSession session = request.getSession();
session.setAttribute("testName","test");
out.println("<html><head><title>ExSession</title></head><body>");
out.println("sessionAttribute:" + session.getAttribute("testName"));
out.println("<a href = 'http://127.0.0.1:8080/testSession/ExS01.html'>");
out.println("爱谁谁</a></body></html>");
out.close();
}
}


ExS01.html
<form method = "post" action = "ExSession01">
<p align = "left">
<font size = "5"><b>Font of Page</b></font>
</p>
<p></p>
<p align = "left">
<input type = "submit" value = "go to Sub Page">
</p>
</form>


ExSession01.java
public class ExSession01 extends HttpServlet {
public synchronized void doPost(HttpServletRequest request,HttpServletResponse response)
throws IOException,ServletException {
doGet(request,response);
}

public void doGet(HttpServletRequest request,HttpServletResponse response)
throws IOException,ServletException {
response.setContentType("text/html;charset=gb2312");
PrintWriter out = response.getWriter();
request.setCharacterEncoding("gb2312");

HttpSession session = request.getSession();

out.println("<html><head><title>Exsession01</title></head><body>");
out.println("sessionAttribute:" + session.getAttribute("testName") + "<br>");

out.println("</body></html>");
out.close();
}
}
...全文
102 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
ay转身遇 2014-03-25
  • 打赏
  • 举报
回复

public class ExSession01 extends HttpServlet {
    public synchronized void doPost(HttpServletRequest request,HttpServletResponse response)
            throws IOException,ServletException {
        doGet(request,response);
    }
     
    public void doGet(HttpServletRequest request,HttpServletResponse response)
            throws IOException,ServletException {
        response.setContentType("text/html;charset=gb2312");
        PrintWriter out = response.getWriter();
        request.setCharacterEncoding("gb2312");
         
        HttpSession session = request.getSession();
        System.out.println(session.getAttribute("testName") );//这样有值的对吧?
        out.println("<html><head><title>Exsession01</title></head><body>");
        out.println("sessionAttribute:" + session.getAttribute("testName") + "<br>");
         
        out.println("</body></html>");
        out.close();
    }
}

81,094

社区成员

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

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