为什么调用SetSessionServlet,ip地址是存在的,可是调用ShowSessionServlet时ip地址显示的却是null(代码如下)

小冷不冷 发布于 2012/06/01 14:38
阅读 183
收藏 0

//SetSessionServlet :

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

public class SetSessionServlet extends HttpServlet{
 public void doGet(HttpServletRequest request,HttpServletResponse response)
 throws ServletException,IOException{
  HttpSession session = request.getSession(true);
  session.setAttribute("ip",request.getRemoteAddr());
  System.out.println(request.getRemoteAddr());
  
  //String ip = (String)session.getAttribute("ip");
  //System.out.println(ip);
  
  response.getWriter().println("SetSesion OK!");
 }
}

//ShowSessionServlet :

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

public class ShowSessionServlet extends HttpServlet{
 public void doGet(HttpServletRequest request,HttpServletResponse response)
 throws ServletException,IOException{
  HttpSession session=request.getSession(true);
  String ip=(String)session.getAttribute("ip");
  System.out.println(ip);
  response.getWriter().println("ip = " + ip);
 }
}

加载中
返回顶部
顶部