参照:

  浅谈cookie跨域的解决方案——document.domain(http://blog.csdn.net/zhouziyu2011/article/details/61200943)

  

Servlet 目录:

  • servlet的基本访问:

  • request的相关信息:

  • cookie:

  • session:

servlet的基本访问:

1.所有的SpringMvc,struts等都是基于Servlet的访问封装。

最原始的访问:

web.xml

  <servlet>
<servlet-name>helloExample</servlet-name>
<servlet-class>servlets.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>helloExample</servlet-name>
<url-pattern>/servlets/helloExample</url-pattern>
</servlet-mapping>

创建java:

/**
* 最原始的Servlet
*
* @author DennyZhao
* @date 2017年11月5日
* @version 1.0
*/
public class HelloServlet extends HttpServlet { /**
* 自动序列号
*/
private static final long serialVersionUID = 4402969242082947388L; @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html");
resp.setCharacterEncoding("UTF-8");
resp.getWriter().append("Hello My Servlet....");
resp.flushBuffer();
} @Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}

地址访问: http://127.0.0.1:8080/ServletEx/servlets/helloExample

Request:

从request中可以获取到访问者的信息。

        System.out.println(req.getCharacterEncoding());
System.out.println(req.getContextPath());
System.out.println(req.getMethod());
System.out.println(req.getPathInfo());
System.out.println(req.getProtocol());
System.out.println(req.getRemoteAddr());
System.out.println(req.getRemoteHost());
System.out.println(req.getRemoteUser());
System.out.println(req.getRequestURI());
System.out.println(req.getRemotePort()); --------------------------------------- null
/ServletEx
GET
null
HTTP/1.1
127.0.0.1
127.0.0.1
null
/ServletEx/servlets/helloExample
62835

request的header部分信息输出:

        Enumeration<String> headerNames = req.getHeaderNames();
while(headerNames.hasMoreElements()) {
String nextElement = headerNames.nextElement();
System.out.println(String.format("------name:%s1,-----value:%s2",nextElement, req.getHeader(nextElement)));
}

结果:

------name:host1,-----value:127.0.0.1:80802
------name:connection1,-----value:keep-alive2
------name:cache-control1,-----value:max-age=02
------name:user-agent1,-----value:Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.91 Safari/537.362
------name:upgrade-insecure-requests1,-----value:12
------name:accept1,-----value:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.82
------name:accept-encoding1,-----value:gzip, deflate, br2
------name:accept-language1,-----value:zh-CN,zh;q=0.82
------name:cookie1,-----value:_ga=GA1.1.210524813.1508312579; JSESSIONID=626E4D01F67954DD8795C1C3E42D4D8C2

request.parameter 获取前台传递的表单数据:

ResourceBundle: 用于指定查找properties文件的国际化。

HttpFilter.filter:用于处理html页面的 <> &等的转义。

创建index.html页面:

    <form action="servlets/helloExample">
firstName<input type="text" name="firstName" value="" />
<br/>SecondName<input type="text" name="secondName" value=""/>
<br/><button name="提交" type="submit">提交数据</button> </form>

后台获取前台的表单数据:

        String first = req.getParameter("firstName");
String second = req.getParameter("secondName");
resp.getWriter().append(String.format("Hello My Servlet....,my Name is %s1 %s2", first, second));

Cookie:

cookie的domain和path属性:

1、domain

表示cookie所在的域,默认为请求的地址,如网址为JavaScript.exam.cn/JavaScript/read.html,那么domain默认为JavaScript.exam.cn。如域A为catagory.exam.cn,域B为JavaScript.exam.cn,那么在域A生产一个令域A和域B都能访问的cookie就要将该cookie的domain设置为.exam.com;如果要在域A生产一个令域A不能访问而域B能访问的cookie就要将该cookie的domain设置为JavaScript.test.com。

2、path

表示cookie所在的目录,默认为/,就是根目录。如在同一个服务器上有目录/JavaScript/,/JavaScript/dir1/,/JavaScript/dir2/,现设一个cookie1的path为/JavaScript/,cookie2的path为/JavaScript/dir1/,那么JavaScript下的所有页面都可以访问到cookie1,而/JavaScript/和/JavaScript/dir2/的子页面不能访问cookie2。这是因为cookie能让其path路径下的页面访问。

默认情况下,cookie对创建它的页面和域与创建它的页面在同一目录的其他页面以及创建它的页面所在目录的子目录的其他页面可见,例如,localhost/JavaScript/write.html创建的cookie对localhost/JavaScript/read.html和localhost/JavaScript/catagory/read.html都是可见的,但对localhost/read.html不可见。

可以设置cookie的path属性,只要以path指定的路径前缀开始的同一服务器的页面均可见cookie,例如,设置path=/JavaScript,则localhost/JavaScript/catagory/write.html创建的cookie对localhost/JavaScript/read.html也是可见的;设置path=/,则cookie对localhost这台服务器上的页面均可见。

        Cookie[] cookie = req.getCookies();
if(cookie != null && cookie.length > 0) {
for(int i=0; i < cookie.length; i++) {
System.out.println(cookie[i].getName());
System.out.println(cookie[i].getValue());
}
}
Cookie cook = new Cookie("fisrtName", "zhangsan");
//cook.setDomain(req.getContextPath() + "/");
resp.addCookie(cook);

Session:

HttpSession session = req.getSession();
session.getId();

Tomcat Servlet学习的更多相关文章

  1. Servlet 学习笔记

    Servlet 运行在服务器上的 java 类: Servlet 容器为 javaWeb 应用提供运行时环境,负责管理 servlet 和 jsp 生命周期,以及管理他们的共享数据. 现在我们知道了 ...

  2. Tomcat&Servlet

    Tomcat&Servlet 一.web开发相关的概念 1. 软件架构 1.1 C/S架构 C:Client客户端, S:Server服务器 比如:QQ.微信.大型网游 优点: 显示效果炫 安 ...

  3. Web开发之Tomcat&Servlet

    <!doctype html>01 - JavaEE - Tomcat&Servlet figure:first-child { margin-top: -20px; } #wri ...

  4. tomcat&servlet初记

    tomcat&servlet初记 1. web相关概念 2. web服务器软件的学习:tomcat 3. servlet入门学习 web相关概念 1,软件架构 1,cs架构:客户端/服务器端 ...

  5. JavaWeb基础(day15)( http + tomcat + servlet + 响应)

    HTTP+Tomcat+Servlet+响应 HTTP HTTP  超文本传输协议(Hyper Text  Transfer  Protocol  ),一种网络协议. 协议的组成和过程 HTTP协议由 ...

  6. Servlet学习笔记(四)

    目录 Servlet学习笔记(四) 一.会话技术Cookie.session 1. 什么是会话技术? 2. 会话技术有什么用? 3. Cookie 3.1 什么是Cookie? 3.2 使用Cooki ...

  7. Servlet学习笔记(三)

    目录 Servlet学习笔记(三) 一.HTTP协议 1.请求:客户端发送欸服务器端的数据 2.响应:服务器端发送给客户端的数据 3.响应状态码 二.Response对象 1.Response设置响应 ...

  8. Servlet学习笔记(二)

    目录 Servlet学习笔记(二) Request对象 1.request和response对象: 2.request对象继承体系结构: 3.什么是HttpServletRequest ? 4.Htt ...

  9. JavaWeb学习总结(三)——Tomcat服务器学习和使用(二) 包含https 非对称秘钥 NB

    JavaWeb学习总结(三)--Tomcat服务器学习和使用(二) 一.打包JavaWeb应用 在Java中,使用"jar"命令来对将JavaWeb应用打包成一个War包,jar命 ...

随机推荐

  1. leetcode -day28 Unique Binary Search Trees I II

    1.  Unique Binary Search Trees II Given n, generate all structurally unique BST's (binary search t ...

  2. MySQL命令行--导入导出数据库

    MySQL命令行导出数据库:   1,进入MySQL目录下的bin文件夹:cd MySQL中到bin文件夹的目录 如我输入的命令行:cd C:\Program Files\MySQL\MySQL Se ...

  3. java 中一些需要注意的知识点

    java数组的length属性是容量,而不是数组真实元素的个数: 多线程中的interrupt()方法并不会终止处于"运行状态"的线程,它只是将线程的中断标记设为true. juc ...

  4. asp.net 模拟CURL调用微信公共平台API 上传下载多媒体文

    近公司项目上在开发微信服务号的接口,需要给用户回复图片或语音或视频,这个时候就需要用到 上传下载多媒体文件接口,微信在这方面推荐采用的是开源函数库curl实现的,CURL项目包括很多版本,我主要测试的 ...

  5. SpringCloud统一配置笔记

    Server端: pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns= ...

  6. RAID磁盘阵列的原理与搭建

    学习导图 RAID-0结构关系图 RAID-1结构关系图 RAID-5:条带+分布校验(三块磁盘以上) RAID-10:镜像+条带(四块磁盘以上) RAID-0 添加两块硬盘,分别为磁盘1.磁盘2.最 ...

  7. fiddler script建议教程

    http://www.fiddlerbook.com/Fiddler/dev/ScriptSamples.asp

  8. [UE4]C++方法多个返回值给蓝图

    如果参数类型带上“&” void URegisterUserWidget::Login(FString& NickName, FString& Password, FStrin ...

  9. 微信jssdk批量添加卡券接口

    1)首先是官方接口文档: 1.批量添加卡券接口:https://mp.weixin.qq.com/wiki?action=doc&id=mp1421141115&t=0.0861973 ...

  10. Git 查询某次历史提交的修改内容

    在工作时,有时候想查看某次的提交修改了哪些的内容. 我们首先可以git log显示历史的提交列表: 之后我们用git show <commit-hashId> 便可以显示某次提交的修改内容 ...