记一次Servlet中getAttribute的错误.
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的错误.的更多相关文章
- servlet中避免405错误的产生
父类Parent(相当于HttpServlet):service方法,用于处理任务分发,doGet.doPost方法用于报错 关注的是子类Son(servlet) 目的:杜绝错误的产生 方式 ...
- Java第三阶段学习(十一、Servlet基础、servlet中的方法、servlet的配置、ServletContext对象)
一.Servlet简介 1.什么是servlet: sun公司提供的一套规范(接口),用来处理客户端请求.响应给浏览器的动态资源.但servlet的实质就是java代码,通过java的API动态的向 ...
- JavaWeb之Servlet中ServletConfig和ServletContext
[声明] 欢迎转载,但请保留文章原始出处→_→ 文章来源:http://www.cnblogs.com/smyhvae/p/4140877.html [正文] 一.ServletConfig:代表当前 ...
- JSP Servlet中Request与Response所有成员方法的研究
HttpServletRequest与HttpServletResponse作为Servlet中doGet.doPost等方法中传递的参数,承接了Http请求与响应中的大部分功能,请求的解析与响应的返 ...
- JSP Servlet中的Request和Response的简单研究
本文参考了几篇文章所得,参考目录如下: 1.http://www.cnblogs.com/guangshan/p/4198418.html 2.http://www.iteye.com/problem ...
- jsp九大内置对象与servlet中java对象
jsp九大内置对象 request对象 : getParameter(String name)获取表单提交的数据 getParamegerNames() 获取客户端提交的所有参数名 getAttri ...
- Servlet 异常处理( 配置错误页面)
当一个 Servlet 抛出一个异常时,Web 容器在使用了 exception-type 元素的 web.xml 中搜索与抛出异常类型相匹配的配置. 您必须在 web.xml 中使用 error-p ...
- 【Spring注解驱动开发】面试官:如何将Service注入到Servlet中?朋友又栽了!!
写在前面 最近,一位读者出去面试前准备了很久,信心满满的去面试.没想到面试官的一个问题把他难住了.面试官的问题是这样的:如何使用Spring将Service注入到Servlet中呢?这位读者平时也是很 ...
- Servlet规范简介——web框架是如何注入到Servlet中的
Servlet规范简介--web框架是如何注入到Servlet中的 引言 Web框架一般是通过一个Servlet提供统一的请求入口,将指定的资源映射到这个servlet,在这个servlet中进行框架 ...
随机推荐
- 使用commons-email解析 eml文件
在对eml文件进行索引的时候需要先对其进行解析,提取出其中的收件人.发件人.文件内容和附件等信息 下边是解析eml文件的一个demo(在运行之前需要先导入mail.jar 和commons-email ...
- iOS CoreMotion 纪录步数
- (void)startUpdateAccelerometer{ /* 设置采样的频率,单位是秒 */ NSTimeInterval updateInterval = 0.05; // ...
- Windows系统下做定时任务为Oracle数据库每天自动备份
1.创建备份目录d:\backup, 创建批处理命令Bak.bat,编写备份脚本 ? 1 2 exp user/passwd@orcl DIRECT=Y BUFFER=100000 FILE=D:\b ...
- mysql 数据操作 单表查询 使用正则表达式查询
SELECT * FROM employee WHERE name REGEXP '^ale'; SELECT * FROM employee WHERE name REGEXP 'on$'; SEL ...
- centos shell脚本编程2 if 判断 case判断 shell脚本中的循环 for while shell中的函数 break continue test 命令 第三十六节课
centos shell脚本编程2 if 判断 case判断 shell脚本中的循环 for while shell中的函数 break continue test 命令 ...
- layer官方演示与讲解(jQuery弹出层插件)
1. 使用layer遇到困难?Fly社区虔诚为您解惑 2. layer 2.0 发布,以独立形式呈现的最后一个版本 3. Fork layer on Github,爱她,就给她加个星啵 当前版本:2. ...
- zlib编译安装
从http://www.zlib.net/下载了最新的源代码 zlib提供了vs sln文件,在zlib-1.2.8\contrib\vstudio\目录有 其中的zlibstat是编译为静态库zli ...
- 本地blast的安装
1 下载程序 在ftp://ftp.ncbi.nlm.nih.gov/blast/executables/blast+/LATEST/下载 ncbi-blast-2.2.25+-x64-linux.t ...
- django需要了解的
搞得差不多就去看看类和对象,看看oop是什么,多用lambda,学学md5加密. 你应当了解,django不是服务器,只提供服务.因此,学习nginx.或是apache是必要的.(IIS不了解,不过题 ...
- Singapore retailer will release this adidas NMD R1
Select spots are restocking the adidas NMD Singapore this Friday, Feb 24th featuring three different ...