发表于: 2017-08-21 22:23:30
1 1218
今天做的事:
今天在弄cookie
使用cookie之前,发现实现cookie需要使用request和response对象,因为之前没怎么使用,所以今天又学习了一下
这里参考了程远的日报
以及下面这篇blog是好文
http://www.cnblogs.com/xdp-gacl/p/3798347.html
首先贴一下代码
@RequestMapping(value = "/test",method = RequestMethod.GET)
public void getLog(HttpServletRequest request, HttpServletResponse response)throws ServletException,IOException{
// 得到请求的url地址
String reqUrl = request.getRequestURL().toString();
// 得到请求资源
String reqUri = request.getRequestURI().toString();
// 得到url中附带的参数
String queryStr = request.getQueryString();
// 得到来访者ip地址
String remoteAddr = request.getRemoteAddr();
//返回发出请求的客户机完整主机名
String remoteHost = request.getRemoteHost();
//返回客户机使用的网络端口号
int remotePort = request.getRemotePort();
//返回用户名
String remoteUser = request.getRemoteUser();
//得到请求url地址时使用的方法
String method = request.getMethod();
//返回请求url中的额外路径信息
//额外路径信息是请求URL中的位于Servlet的路径之后和查询参数之前的内容,它以“/”开头。
String pathInfo = request.getPathInfo();
//获取web服务器的ip地址
String localAddr = request.getLocalAddr();
//获取web服务器的主机名
String localName = request.getLocalName();
//这两句不好使
//设置字符以UTF-8编码输出到客户浏览器
// response.setCharacterEncoding("UTF-8");
//通过设置响应头控制浏览器以UTF-8的编码显示数据,
// response.setHeader("contentType""text/html;charset=UTF-8",);
// 如果不加这句话,那么浏览器显示的将是乱码
response.setContentType("text/html;charset=UTF-8");
System.out.println("请求的URL地址:"+reqUrl);
System.out.println("请求的资源:"+reqUri);
System.out.println("请求的URL地址中附带的参数:"+queryStr);
System.out.println("来访者的IP地址:"+remoteAddr);
System.out.println("来访者的主机名:"+remoteHost);
System.out.println("使用的端口号:"+remotePort);
System.out.println("remoteUser:"+remoteUser);
System.out.println("请求使用的方法:"+method);
System.out.println("pathInfo:"+pathInfo);
System.out.println("localAddr:"+localAddr);
System.out.println("localName:"+localName);
PrintWriter out = response.getWriter();
out.write("获取到的客户信息如下");
out.write("<hr/>");
out.write("请求的URL地址:"+reqUrl);
out.write("<br/>");
out.write("请求的资源:"+reqUri);
out.write("<br/>");
out.write("请求的URL地址中附带的参数:"+queryStr);
out.write("<br/>");
out.write("来访者的IP地址:"+remoteAddr);
out.write("<br/>");
out.write("来访者的主机名:"+remoteHost);
out.write("<br/>");
out.write("使用的端口号:"+remotePort);
out.write("<br/>");
out.write("remoteUser:"+remoteUser);
out.write("<br/>");
out.write("请求使用的方法:"+method);
out.write("<br/>");
out.write("pathInfo:"+pathInfo);
out.write("<br/>");
out.write("localAddr:"+localAddr);
out.write("<br/>");
out.write("localName:"+localName);
//还是迭代,和下面的一样,可以用方法调用解决迭代问题,看下面的代码
response.setHeader("refresh","0;url=/test");
// getLog(request,response);
// return "test";
}
这里会遇到迭代的问题,程序会不断运行,页面也是会打印出重复块
然后解决办法是
通过调用方法来解决迭代问题
package Controller;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
* Created by Administrator on 2017/08/21.
*/
public class RequestM extends HttpServlet {
public void getLog(HttpServletRequest request, HttpServletResponse response)throws ServletException,IOException{
// 得到请求的url地址
String reqUrl = request.getRequestURL().toString();
// 得到请求资源
String reqUri = request.getRequestURI().toString();
// 得到url中附带的参数
String queryStr = request.getQueryString();
// 得到来访者ip地址
String remoteAddr = request.getRemoteAddr();
//返回发出请求的客户机完整主机名
String remoteHost = request.getRemoteHost();
//返回客户机使用的网络端口号
int remotePort = request.getRemotePort();
//返回用户名
String remoteUser = request.getRemoteUser();
//得到请求url地址时使用的方法
String method = request.getMethod();
//返回请求url中的额外路径信息
//额外路径信息是请求URL中的位于Servlet的路径之后和查询参数之前的内容,它以“/”开头。
String pathInfo = request.getPathInfo();
//获取web服务器的ip地址
String localAddr = request.getLocalAddr();
//获取web服务器的主机名
String localName = request.getLocalName();
//这两句不好使
//设置字符以UTF-8编码输出到客户浏览器
// response.setCharacterEncoding("UTF-8");
//通过设置响应头控制浏览器以UTF-8的编码显示数据,
// response.setHeader("contentType""text/html;charset=UTF-8",);
// 如果不加这句话,那么浏览器显示的将是乱码
response.setContentType("text/html;charset=UTF-8");
System.out.println("请求的URL地址:"+reqUrl);
System.out.println("请求的资源:"+reqUri);
System.out.println("请求的URL地址中附带的参数:"+queryStr);
System.out.println("来访者的IP地址:"+remoteAddr);
System.out.println("来访者的主机名:"+remoteHost);
System.out.println("使用的端口号:"+remotePort);
System.out.println("remoteUser:"+remoteUser);
System.out.println("请求使用的方法:"+method);
System.out.println("pathInfo:"+pathInfo);
System.out.println("localAddr:"+localAddr);
System.out.println("localName:"+localName);
PrintWriter out = response.getWriter();
out.write("获取到的客户信息如下");
out.write("<hr/>");
out.write("请求的URL地址:"+reqUrl);
out.write("<br/>");
out.write("请求的资源:"+reqUri);
out.write("<br/>");
out.write("请求的URL地址中附带的参数:"+queryStr);
out.write("<br/>");
out.write("来访者的IP地址:"+remoteAddr);
out.write("<br/>");
out.write("来访者的主机名:"+remoteHost);
out.write("<br/>");
out.write("使用的端口号:"+remotePort);
out.write("<br/>");
out.write("remoteUser:"+remoteUser);
out.write("<br/>");
out.write("请求使用的方法:"+method);
out.write("<br/>");
out.write("pathInfo:"+pathInfo);
out.write("<br/>");
out.write("localAddr:"+localAddr);
out.write("<br/>");
out.write("localName:"+localName);
}
}
@RequestMapping(value = "/testRM",method = RequestMethod.GET)
public void getTest(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
RequestM rm = new RequestM();
rm.getLog(request,response);
//这里又是迭代了
// getTest(request,response);
}
这里就只调用一次方法,自然就没有迭代问题了
这里讲一下
输出流
PrintWriter是一种过滤流,也叫处理流。也就是能对字节流和字符流进行处理
两种构造方法:
PrintWriter(File file) 使用指定文件创建不具有自动行刷新的新 PrintWriter。
PrintWriter(String fileName) 创建具有指定文件名称且不带自动行刷新的新 PrintWriter。
返回类型为PrintWriter的方法
append(char c)
format(String regex,Object args)以指定格式的字符串和参数写入PrintWriter,我个人认为等同于printf
printf()
返回类型为void的方法
println(Object obj)打印obj,可以是基本数据类型或对象,并换行
print(Object obj)同上,但不换行
write(int i) 写入单个字符i
write(char[] buf) 写入字符数组。
write(char[] buf, int off, int len) 写入字符数组的某一部分。
write(String s) 写入字符串
write(String s, int off, int len)写入字符串的某一部分
然后是开始学习cookie,一开始对cookie的理解有偏差,后来咨询了师兄
cookie是存入到客户端的,是一个键值对,虽然有多条记录,但是每个网页,或者说项目只有一个cookie,如果每次登陆新建cookie的话,相当于一次刷新,cookie的值就会改变,但键名是不会变的,所以我们通过cookie名获取cookie,然后比对内部的值。
这里有个简单应用
@RequestMapping(value = "/login",method = RequestMethod.POST)
public void login(HttpServletRequest request,HttpServletResponse response,User u)throws ServletException,IOException{
String username = request.getParameter("user");
System.out.println(username);
String password = request.getParameter("password");
System.out.println(password);
if(username != "" && password != ""){
Cookie cookie = new Cookie("user1",username + ","+ password);
cookie.setMaxAge(60*60*10);
cookie.setPath("/");
response.addCookie(cookie);
response.sendRedirect("/test2");
}else {
response.sendRedirect("/test1");
}
}
这里就是不同的跳转页面,通过判断输入是否为空
@RequestMapping(value = "/test1",method = RequestMethod.GET)
public String fds1(){
return "test1";
}
@RequestMapping(value = "/testN",method = RequestMethod.GET)
public String fdsm(){
return "test2";
}
@RequestMapping(value = "/test2",method = RequestMethod.GET)
public void fds2(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
Cookie[] cookies = request.getCookies();
for(int i = 0;i < cookies.length;i++){
if(cookies[i].getName().equals("user1")){
System.out.println(cookies[i].getValue());
System.out.println("**********");
String value = cookies[i].getValue();
String[] elements = value.split(",");
String username = elements[0];
String password = elements[1];
System.out.println(username);
System.out.println(password);
if(username.equals("admin")){
response.sendRedirect("/testN");
}else {
response.sendRedirect("/test1");
}
}
}
}
也是一个跳转,这里没有和数据库交互,写死了一个数据admin进行比对
运行结果
先是瞎输入一个
登陆后
错误页面
控制台信息
使用写死的admin测试
正确页面的跳转
这里的逻辑并没有整理,所以写起来还算容易
但是任务中的逻辑稍有复杂,暂时还未攻破,需要明天继续花时间考虑了。
明天计划:将cookie应用到任务中的登陆中,实现逻辑通顺;使用拦截器。
问题:已解决;目前cookie残余逻辑问题,需要花时间思考
收获:request,response等的使用;输出流PrintWriter;cookie的使用。
进度:开始时间:2017.08.19
预计demo:2017.08.22
是否有延期风险:请假一天,延期1日;cookie逻辑有难度,不确定是否还会延期
禅道链接:http://task.ptteng.com/zentao/project-task-285-unclosed.html
评论