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是怎么会事啊,请问怎样改才正确?多谢!
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是怎么会事啊,请问怎样改才正确?多谢!
解决方案 »
- 超链接问题
- 我下载了Sun Java System Active Server Pages
- 大虾们来看看我的程序是什么地方有问题?总是只显示第一页的内容,不会翻页
- 关于var的问题
- 不要笑话偶 偶不知道JSP和JavaScript的区别,他们和JAVA都是啥关系?
- 有没有办法在网叶上得到图形文件解码后的buffer,从而对此buffer进行操作再显示?
- JSP论坛里最傻的问题,会的来看,很急
- 一个网页提示错误,各位帮忙,在线等待
- Hibernate的反射机制?
- spring boot +mybits 启动报的错org.springframework.beans.factory.UnsatisfiedDependency
- 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("/");
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();
%>
<%@ 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>
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()
{
}
}
<%@ 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>
是没有取到,我这么取不对么,正确应该怎么取啊?
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么
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
Indicates to the browser whether the cookie should only be sent using a secure protocol, such as HTTPS or SSL
如果你设置了这一句,除非登陆的时候采用安全认证的方式登陆否则是得不到Cookie的