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. poj1568 Find the Winning Move[极大极小搜索+alpha-beta剪枝]

    Find the Winning Move Time Limit: 3000MS   Memory Limit: 32768K Total Submissions: 1286   Accepted:  ...

  2. [MongoDB]安装MongoDB遇到问题

    1. 首先,当然是下载 MongoDB MongoDB的官方网站是:http://www.mongodb.org/, 最新版本下载在:http://www.mongodb.org/downloads  ...

  3. Activity的LaunchMode应用场景思考

    本文链接:http://blog.csdn.net/xiaodongrush/article/details/28597855   1. 有哪几种类型?分别有什么用? http://developer ...

  4. Python 使用 Matplotlib 做图时,如何画竖直和水平的分割线或者点画线或者直线?

    作者:看看链接:https://www.zhihu.com/question/21929761/answer/164975814 可以使用: vlines(x, ymin, ymax) hlines( ...

  5. Oracle之rman命令的使用(51CTO风哥rman课程)

    看rman的连接串的帮助 连接数据库 rman target/ rman的版本要和目标数据库一致(一般大版本可以往下兼容小版本) 运行操作系统命令 run {host "pwd"; ...

  6. 介绍一种android的裸刷机方法(fastboot刷机实质)

      fastboot刷机的前提是你的开发板uboot良好并能正常启动进入fastboot模式,你的开发版的nand分区已存在.对于Android的uboot而言, 已经实现了fastboot命令,当你 ...

  7. mongdb ---shard

    http://blog.fens.me/mongodb-shard/ https://segmentfault.com/a/1190000004263332 1. 和用户管理相关的操作基本都要在adm ...

  8. Python开发【模块】:Urllib(二)

    Urllib实战 1.爬取糗事百科中段子和用户名: 代码实例: # 爬取网站页面内容 import re import urllib.request url = 'https://www.qiushi ...

  9. Mysql EXPLAIN 相关疑问: Using temporary ; Using filesort

    一.什么是Using temporary ; Using filesort 1. using filesort filesort主要用于查询数据结果集的排序操作,首先MySQL会使用sort_buff ...

  10. Mirror--镜像断开的解决办法

    如果镜像在搭建一段时候后出现问题,可能存在以下原因: 1. 因为主库或镜像库存在内存压力,导致无法完成镜像日志传送和重做 解决办法:设置数据库最小内存,保证数据库有足够内存完成镜像操作 2. 因为主库 ...