package com.ykmimi.order.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import com.ykmimi.order.entity.Customers;
import com.ykmimi.order.service.AuthService; /**
* Servlet implementation class ShowUserServlet
*/
@WebServlet("/ShowUserServlet")
public class ShowUserServlet extends HttpServlet {
private static final long serialVersionUID = 1L; /**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { ///// * 字符编码设置
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8"); /////* requestURL
StringBuffer requestURL = request.getRequestURL();
System.out.println(requestURL);
String clientIP = request.getRemoteHost();
System.out.println(clientIP); /////* 获取cookie
Cookie[] cs = request.getCookies();
if(cs.length>0){
for(Cookie c : cs){
//获取Cookie的name,key
String name = c.getName();
request.setAttribute("name",name); //在这里,如果设置(name,name) 则会在下面cid获取attribute时出现String转long的异常.所以要明确key值必须是""String类型
String value = c.getValue();
request.setAttribute("value",value);
}
} ///// * 获取/login传送过来的long类型值 cid (customer_id)
long cid = 0;
cid = (long)request.getAttribute("cid");
System.out.println(cid);
// cid =Long.parseLong(cidStr);
/////* 获取用户id后进行判定,如果不为0的话,将其再进行查询,返回用户的整个实例,再次进行传值
/////* 并在最终用户登陆后的显示界面上显示用户信息及登陆后的服务信息.
if (cid > 0) {
AuthService as = new AuthService();
/////* 如果这个id比较符合id的规则 即 大于 0, 那么用它去数据库检索这个id的归属者的实例
Customers customer = null;
customer = as.getCustomersInstanceByID(cid);
request.setAttribute("customerInstance", customer);
RequestDispatcher rd = request.getRequestDispatcher("/userInfo.jsp");
rd.forward(request, response); // response.sendRedirect("/userInfo.jsp?cid="+cid+"&cname="+customer.getCustomer_name());
} else if (cid == 0) {
RequestDispatcher rd = request.getRequestDispatcher("/error.jsp");
rd.forward(request, response);
} } /**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
} }

而且在前一个页面,也就是写入Cookie的Servlet转发过来后,cid是在写如Cookie前的那一个Servlet转发过来的,而写Cookie的那个Servlet不用再重复设置setAttribute("cid",cid)

但如果你在中间转发的那个Servlet又设置"cid"为别的值,那么最后接受时也会改为别的值.

记一次Servlet中getAttribute的错误.的更多相关文章

  1. servlet中避免405错误的产生

    父类Parent(相当于HttpServlet):service方法,用于处理任务分发,doGet.doPost方法用于报错  关注的是子类Son(servlet)     目的:杜绝错误的产生 方式 ...

  2. Java第三阶段学习(十一、Servlet基础、servlet中的方法、servlet的配置、ServletContext对象)

    一.Servlet简介  1.什么是servlet: sun公司提供的一套规范(接口),用来处理客户端请求.响应给浏览器的动态资源.但servlet的实质就是java代码,通过java的API动态的向 ...

  3. JavaWeb之Servlet中ServletConfig和ServletContext

    [声明] 欢迎转载,但请保留文章原始出处→_→ 文章来源:http://www.cnblogs.com/smyhvae/p/4140877.html [正文] 一.ServletConfig:代表当前 ...

  4. JSP Servlet中Request与Response所有成员方法的研究

    HttpServletRequest与HttpServletResponse作为Servlet中doGet.doPost等方法中传递的参数,承接了Http请求与响应中的大部分功能,请求的解析与响应的返 ...

  5. JSP Servlet中的Request和Response的简单研究

    本文参考了几篇文章所得,参考目录如下: 1.http://www.cnblogs.com/guangshan/p/4198418.html 2.http://www.iteye.com/problem ...

  6. jsp九大内置对象与servlet中java对象

    jsp九大内置对象 request对象 :  getParameter(String name)获取表单提交的数据 getParamegerNames() 获取客户端提交的所有参数名 getAttri ...

  7. Servlet 异常处理( 配置错误页面)

    当一个 Servlet 抛出一个异常时,Web 容器在使用了 exception-type 元素的 web.xml 中搜索与抛出异常类型相匹配的配置. 您必须在 web.xml 中使用 error-p ...

  8. 【Spring注解驱动开发】面试官:如何将Service注入到Servlet中?朋友又栽了!!

    写在前面 最近,一位读者出去面试前准备了很久,信心满满的去面试.没想到面试官的一个问题把他难住了.面试官的问题是这样的:如何使用Spring将Service注入到Servlet中呢?这位读者平时也是很 ...

  9. Servlet规范简介——web框架是如何注入到Servlet中的

    Servlet规范简介--web框架是如何注入到Servlet中的 引言 Web框架一般是通过一个Servlet提供统一的请求入口,将指定的资源映射到这个servlet,在这个servlet中进行框架 ...

随机推荐

  1. CVE-2018-2628 weblogic WLS反序列化漏洞--RCE学习笔记

    weblogic WLS 反序列化漏洞学习 鸣谢 感谢POC和分析文档的作者-绿盟大佬=>liaoxinxi:感谢群内各位大佬及时传播了分析文档,我才有幸能看到. 漏洞简介 漏洞威胁:RCE-- ...

  2. 高中生的IT之路-1.1自序

        近几年来越来越多的人问我关于 高中生要不要读大学.大学选择专业.毕业后的择业问题,索性我不如把我对这几方面的理解写出来,如果有幸能帮助到更多的人,那也算是个人对社会做出了一点贡献.       ...

  3. 【BZOJ3939】[Usaco2015 Feb]Cow Hopscotch 动态规划+线段树

    [BZOJ3939][Usaco2015 Feb]Cow Hopscotch Description Just like humans enjoy playing the game of Hopsco ...

  4. 【BZOJ3551】[ONTAK2010]Peaks加强版 最小生成树+DFS序+主席树

    [BZOJ3545][ONTAK2010]Peaks Description 在Bytemountains有N座山峰,每座山峰有他的高度h_i.有些山峰之间有双向道路相连,共M条路径,每条路径有一个困 ...

  5. TC/IP协议簇

    TCP/IP: 数据链路层:ARP,RARP网络层: IP,ICMP,IGMP传输层:TCP ,UDP,UGP应用层:Telnet,FTP,SMTP,SNMP. OSI:物理层:EIA/TIA-232 ...

  6. centos7 iptables/firewalld docker open port

    here are multiple "hackish" ways to do it: scan kernel logs, as mentioned by Jiri (but you ...

  7. wireshark, loopback

    swapondd if=/dev/zero of=/data/mnt/swap bs=1024 count=8024000 sudo apt-get install wireshark sudo gr ...

  8. Apache 2.4 编码GB2312中文乱码的问题

    今天部署了一个项目,代码和数据库都是gb2312的,本地和服务器都是apache2.4的版本,本地编码没问题,response的content-type是空的.按html的mete解析的,查看源码也是 ...

  9. Python性能分析指南(未完成)

    英文原文:http://www.huyng.com/posts/python-performance-analysis/ 译文:http://www.oschina.net/translate/pyt ...

  10. Kubernetes实战(二):k8s v1.11.1 prometheus traefik组件安装及集群测试

    1.traefik traefik:HTTP层路由,官网:http://traefik.cn/,文档:https://docs.traefik.io/user-guide/kubernetes/ 功能 ...