servlet里是这样写的
if(request.getParameter("user_id") != null)
user_id = request.getParameter("user_id");                                  //获得用户登录名
if(request.getParameter("user_pwd") != null)
user_pwd = request.getParameter("user_pwd"); 
String cookieName="Sender";
Cookie cookie=new Cookie(user_id, user_id);
cookie.setValue(user_pwd);
cookie.setMaxAge(365*24*60*60);
cookie.setSecure(true);
cookie.setDomain("127.0.0.1");
cookie.setPath("/");
response.addCookie(cookie);
dispatchForward(request, response, "/success.jsp");success.jsp页面里是这样写的
    <%
Cookie cookies[]=request.getCookies();
Cookie sCookie=null;
String svalue=null;
String sname=null;
String strDomain = "";
String strComment = "";
String strPath = "";
int i = 0;
System.out.println(request.getRequestedSessionId());
System.out.println(cookies);
for(i=0;i<cookies.length;i++)
{
sCookie=cookies[i];svalue=sCookie.getValue();
if(sCookie.getName().equals("username"))
sname=sCookie.getValue();
sname=sCookie.getName();
strDomain = sCookie.getDomain();
strComment = sCookie.getComment();
strPath = sCookie.getPath();
%>
我想取出user_id和user_pwd,结果取出是SessionId是怎么会事啊,请问怎样改才正确?多谢!

解决方案 »

  1.   

    你该不会把System.out.println(request.getRequestedSessionId());打印出来的东西当做是user_id吧。
    还有你的程序也有问题,应该改成
    Cookie cookie=new Cookie("user_id", user_id);
    cookie.setMaxAge(365*24*60*60);
    cookie.setSecure(true);
    cookie.setDomain("127.0.0.1");
    cookie.setPath("/");
    response.addCookie(cookie);
    cookie=new Cookie("user_pwd", user_pwd);
    cookie.setMaxAge(365*24*60*60);
    cookie.setSecure(true);
    cookie.setDomain("127.0.0.1");
    cookie.setPath("/");
    dispatchForward(request, response, "/success.jsp");
    取的时候改成
     <%
    Cookie cookies[]=request.getCookies();
    Cookie sCookie=null;
    String spwd=null;
    String sname=null;
    String strDomain = "";
    String strComment = "";
    String strPath = "";
    int i = 0;
    System.out.println(request.getRequestedSessionId());
    System.out.println(cookies);
    for(i=0;i<cookies.length;i++)
    {
    sCookie=cookies[i];svalue=sCookie.getValue();
    if(sCookie.getName().equals("user_id"))
    sname=sCookie.getValue();//这里取到user_id
    else if(sCookie.getName().equals("user_pwd"))
    spwd = sCookie.getValue();//这里取到密码
    strDomain = sCookie.getDomain();
    strComment = sCookie.getComment();
    strPath = sCookie.getPath();
    %>
      

  2.   

    它的取值为null啊,spwd取的是sessionid啊
      

  3.   

    我是在servlet跳转的dispatchForward(request, response, "/success.jsp");
      

  4.   

    能取到cookie没理由取不到值的,你的程序改了么
      

  5.   

    页面为<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%@ page import="javax.servlet.http.Cookie"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    <base href="<%=basePath%>">
        
        <title>My JSP 'success.jsp' starting page</title>
      <%
    Cookie cookies[]=request.getCookies();
    Cookie sCookie=null;
    String svalue=null;
    String spwd=null;
    String sname=null;
    String strDomain = "";
    String strComment = "";
    String strPath = "";
    int i = 0;
    System.out.println(request.getRequestedSessionId());
    System.out.println(cookies);
    for(i=0;i<cookies.length;i++)
    {
    sCookie=cookies[i];svalue=sCookie.getValue();
    if(sCookie.getName().equals("user_id"))
    sname=sCookie.getValue();//这里取到user_id
    else if(sCookie.getName().equals("user_pwd"))
    spwd = sCookie.getValue();//这里取到密码
    strDomain = sCookie.getDomain();
    strComment = sCookie.getComment();
    strPath = sCookie.getPath();
    %>   <tr><td>sname=<%=sname%></td></tr>
    <tr><td>svalue=<%=spwd%></td></tr>
    <tr><td>strDomain=<%=strDomain%></td></tr>
    <tr><td>strComment=<%=strComment%></td></tr>
    <tr><td>strPath=<%=strPath%></td></tr>
    <%
    }
    %>  </head>
      
      <body>
        This is my JSP page. <br>
      </body>
    </html>
      

  6.   

    servlet为
    package com.servlet;import java.io.PrintWriter;
    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.SQLException;import javax.servlet.RequestDispatcher;
    import javax.servlet.ServletException;
    import javax.servlet.http.Cookie;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;/**
     * 功能说明  
     * @version 1.0, 2005-10-8
     * @author lvbo
     */
    public class loginServlet extends HttpServlet {

    /**
     * 跳转控制器
     * @param request
     * @param response
     * @param url  跳转的绝对路径
     */
    public void dispatchForward(HttpServletRequest request, HttpServletResponse response, String url)
    {
    try
    {
    RequestDispatcher dispatcher = request.getRequestDispatcher(url);
    dispatcher.forward(request, response);
    }
    catch(Exception e){
    e.printStackTrace();
    }
    } /**
     * 
     */
    public void doPost(HttpServletRequest request,HttpServletResponse response) 
        throws java.io.IOException,ServletException 
        {
    //初始化变量

    Connection telConn = null;

    request.setCharacterEncoding("GBK");
    response.setContentType("text/html;charset=GBK");
    PrintWriter out = response.getWriter();
    ResultSet rsResult = null;

    RequestDispatcher dispatch = null;
    String user_id = "",user = "",user_pwd = "",user_department = "",is_admin = "",user_job = "",ip_Addr = "";
    String user_tacheright = "",user_projectright = "",user_dep_areano = "",user_departmentno = "",user_dep_cordno = "",financeCode = "",user_operation_index = "";
    String user_BranchName = "",user_BranchNo = "",user_departmentName = "",user_dep_type = "";

    try
         {
        
         HttpSession session = request.getSession();

    if (session.getAttribute("openInformation")!=null) 
    session.removeAttribute("openInformation");

    ip_Addr = request.getRemoteAddr();
    if(request.getParameter("user_id") != null)
    user_id = request.getParameter("user_id");                                  //获得用户登录名
    if(request.getParameter("user_pwd") != null)
    user_pwd = request.getParameter("user_pwd");                                //获得用户密码
    if(session.getAttribute("user_id") != null) {
    user_id = (String)session.getAttribute("user_id");
    session.removeAttribute("user_id");
    }
    if(session.getAttribute("user_pwd") != null) {
    user_pwd = (String)session.getAttribute("user_pwd");
    session.removeAttribute("user_pwd");
    }
    String cookieName="Sender";
    /*
    Cookie cookie=new Cookie("user_id", user_id);
    cookie.setValue(user_pwd);
    cookie.setMaxAge(365*24*60*60);
    cookie.setSecure(true);
    cookie.setDomain("127.0.0.1");
    cookie.setPath("/");
    response.addCookie(cookie);
    System.out.println(cookie.getName());
    System.out.println(cookie.getValue());
    dispatchForward(request, response, "/success.jsp");   //进入省公司计划管理导航环节部分界面
    */
    Cookie cookie=new Cookie("user_id", user_id);
    cookie.setMaxAge(365*24*60*60);
    cookie.setSecure(true);
    cookie.setDomain("127.0.0.1");
    cookie.setPath("/");
    response.addCookie(cookie);
    cookie=new Cookie("user_pwd", user_pwd);
    cookie.setMaxAge(365*24*60*60);
    cookie.setSecure(true);
    cookie.setDomain("127.0.0.1");
    cookie.setPath("/");
    response.addCookie(cookie);
    dispatchForward(request, response, "/success.jsp"); }
         catch(Exception e)
         {
         e.printStackTrace();
         }
         finally
         {
         try
         {
    if(telConn!=null)
    telConn.close();
         }catch(SQLException sqle)
         {
         System.out.println("has a error!");
         }
         }
        } public void doGet(HttpServletRequest request,HttpServletResponse response) 
    throws java.io.IOException,ServletException 
    {
    doPost(request,response);
    } public void service(HttpServletRequest request,HttpServletResponse response) 
    throws java.io.IOException,ServletException 
    {
         doPost(request,response);
    }

    public void init()
    {

    }

    public void destroy()
    {
    }
    }
      

  7.   

    这是因为用户名和密码是两个不同的Cookie,你必须分两次取出来然后再输出,你改成
    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%@ page import="javax.servlet.http.Cookie"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    <base href="<%=basePath%>">
        
        <title>My JSP 'success.jsp' starting page</title>
    %>  </head>
      
      <body>
         <%
    Cookie cookies[]=request.getCookies();
    Cookie sCookie=null;
    String svalue=null;
    String spwd=null;
    String sname=null;
    String strDomain = "";
    String strComment = "";
    String strPath = "";
    int i = 0;
    System.out.println(request.getRequestedSessionId());
    System.out.println(cookies);
    for(i=0;i<cookies.length;i++)
    {
    sCookie=cookies[i];
    if(sCookie.getName().equals("user_id")){
    sname=sCookie.getValue();//这里取到user_id
    strDomain = sCookie.getDomain();
    strComment = sCookie.getComment();
    strPath = sCookie.getPath();
    break;
    }
    }
    for(i=0;i<cookies.length;i++)
    {
    sCookie=cookies[i];
    if(sCookie.getName().equals("user_pwd")){
    spwd = sCookie.getValue();//这里取到密码
    break;
    }
    }%>   <tr><td>sname=<%=sname%></td></tr>
    <tr><td>svalue=<%=spwd%></td></tr>
    <tr><td>strDomain=<%=strDomain%></td></tr>
    <tr><td>strComment=<%=strComment%></td></tr>
    <tr><td>strPath=<%=strPath%></td></tr>
      </body>
    </html>
      

  8.   

    结果为:sname=null svalue=null strDomain= strComment= strPath= 是怎么会事啊
      

  9.   

    你在if里面打印点什么东西,也许根本没有取到user_id,user_pwd这两个Cookie
      

  10.   

    页面输出为:sname=null svalue=null strDomain= strComment= strPath=
    是没有取到,我这么取不对么,正确应该怎么取啊?
      

  11.   

    你先在打印出所有的Cookie看看
      

  12.   

    <%
    Cookie cookies[]=request.getCookies();
    Cookie sCookie=null;
    String svalue=null;
    String spwd=null;
    String sname=null;
    String strDomain = "";
    String strComment = "";
    String strPath = "";
    int i = 0;
    System.out.println(request.getRequestedSessionId());
    System.out.println(cookies);
    for(i=0;i<cookies.length;i++)
    {
    sCookie=cookies[i];
    System.out.println(sCookie);
    if(sCookie.getName().equals("user_id")){
    sname=sCookie.getValue();//这里取到user_id
    strDomain = sCookie.getDomain();
    strComment = sCookie.getComment();
    strPath = sCookie.getPath();
    out.println("sname="+sname);
    break;
    }
    }
    for(i=0;i<cookies.length;i++)
    {
    sCookie=cookies[i];
    System.out.println(sCookie);
    if(sCookie.getName().equals("user_pwd")){
    spwd = sCookie.getValue();//这里取到密码
    out.println("spwd="+spwd);
    break;
    }
    }%>
    输出结果为:040C7732C392A74FE84367386C11F2CF
    [Ljavax.servlet.http.Cookie;@9133f6
    javax.servlet.http.Cookie@ea7776
    javax.servlet.http.Cookie@ea7776
    请问有MSN么
      

  13.   

    这说明Cookie传到success.jsp后已经没了,我想问一下你为什么设置Cookie,用户的登陆信息应该保存在session里的
      

  14.   

    Cookie传到success.jsp为什么会没啊?
      

  15.   

    你在servlet里测试一下,看是不是取参数的时候就出问题了
      

  16.   

    我在servlet这样测试的:
    System.out.println("user_id="+user_id+";user_pwd="+user_pwd);
    Cookie cookie=new Cookie("user_id", user_id);
    System.out.println(cookie.getName());
    cookie.setMaxAge(365*24*60*60);
    cookie.setSecure(true);
    cookie.setDomain("127.0.0.1");
    cookie.setPath("/");
    response.addCookie(cookie);
    cookie=new Cookie("user_pwd", user_pwd);
    System.out.println(cookie.getName());
    cookie.setMaxAge(365*24*60*60);
    cookie.setSecure(true);
    cookie.setDomain("127.0.0.1");
    cookie.setPath("/");
    response.addCookie(cookie);

    dispatchForward(request, response, "/success.jsp");
    输出结果为:
    user_id=lvbo;user_pwd=232
    user_id
    user_pwd
      

  17.   

    在servlet里把cookie.setSecure(true);这句去掉
    Indicates to the browser whether the cookie should only be sent using a secure protocol, such as HTTPS or SSL
    如果你设置了这一句,除非登陆的时候采用安全认证的方式登陆否则是得不到Cookie的