记一次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中进行框架 ...
随机推荐
- Fluent Nhibernate Mapping for Sql Views
Views are mapped the same way tables are mapped except that you should put Readonly() in the mapping ...
- thinkphp---数据表更新字段开发模式可更新生产模式不能更新!
这里认为是坑的主要原因:这个问题我调试了一天,才发现是缓存的问题. 问题原因:在做一thinkphp的项目,在后期要进行修改.修改的时候,数据表里面添加了两个字段,然后前台修改模板,将添加的字段提交上 ...
- Jquery Uploadify使用参数详解
开始上传 $('#uploadify_1').uploadifyUpload(); 1 uploader uploadify.swf文件的相对路径,该swf文件是一个带有文字BROWSE的按钮,点击 ...
- Code Forces 652D Nested Segments(离散化+树状数组)
Nested Segments time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- poj1961 & hdu1358 Period【KMP】
Period Time Limit: 3000MS Memory Limit: 30000K Total Submissions: 20436 Accepted: 9961 Descripti ...
- docker网络部分源码分析
daemon初始化network controller daemon的配置,网络部分的内容在cmd/dockerd/config_common_unix.go中指定,默认设置一般都为空 // daem ...
- Disruptor的伪共享解决方案
1.术语 术语 英文单词 描述 内存屏障 Memory Barriers 是一组处理器指令,用于实现对内存操作的顺序限制. In the Java Memory Model a volatile fi ...
- orcle中如何使用动态游标来对变量进行赋值
在oracle中动态游标的概念一般不常用,但有时根据客户的特殊业务,需要使用到动态游标来解决问题!在对于一条动态SQL语句而产生多条记录时,动态游标的使用将是一个很好的选择,具体参见如下在工作流项目中 ...
- SLAM for Dummies SLAM初学者教程 中文翻译 1到4章
SLAM for Dummies SLAM初学者教程A Tutorial Approach to Simultaneous Localization and Mapping 一本关于实时定位及绘图 ...
- weiwo.wxmmd.com将您重定向的次数过多。尝试清除 Cookie.
折腾了很久,最后更换PHP版本解决了,我的项目用的tp3.1.2,出现上图问题时的php版本是7.1,换回5.6就没有这个问题.希望能为大家提供一个思路.