参照:

  浅谈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. ORA-10997:another startup/shutdown operation of this instance in progress解决方法

    SQL> startup ORA-10997: another startup/shutdown operation of this instance inprogress ORA-09967: ...

  2. Microsoft Dynamics CRM 2011 新建实体 需要注意的细节

    新建一个实体,需要红色框内的是否勾选的意义,可以进一步加深对CRM的理解.如图: 下面对部分的进行了自我的理解,不对的地方,还请大家指出来.互相学习. 1.CRM2011中,在活动方面加强的新特性包括 ...

  3. Log4j2的基本使用

    Log4j2是Log4j1.x的的升级版,其中也有很大的不同,最大的区别就是由以前的properties配置文件改为xml/json/yaml配置文件. 其中配置文件的位置官方说明如下: Log4j ...

  4. poj 3255 Roadblocks 次短路(两次dijksta)

    Roadblocks Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total S ...

  5. 1109 Group Photo (25 分)

    1109 Group Photo (25 分) Formation is very important when taking a group photo. Given the rules of fo ...

  6. 学习git最好的方式

    1:登陆git官网网站 https://git-scm.com 2:点击esay to learn连接 3:点击Book连接 4:选择简体中文,下载PDF文档,也可以在线学习.

  7. IntelliJ IDEA小问题解决方法------(持续更新)

    1:IDEA运行时报错提示“找不到或无法加载主类”:在确保IDEA开发环境无误后->file->invalidate Cache/restart,之后再重新build.问题解决. 2.如何 ...

  8. 基于sklearn的 BaseEstimator开发接口:模型融合Stacking

    转载:https://github.com/LearningFromBest/CMB-credit-card-department-prediction-of-purchasing-behavior- ...

  9. 自己写的jQuery拖动滑块

    (function ($) { $.fn.bnSlide = function (options) { var defaults = { colorData: 0, //原始滑道的有效值 maxWid ...

  10. tensorflow入门资料

    google出的说明文档 tensorflow_manual_cn.pdf google出的视频 https://www.zhihu.com/question/41667903/answer/1306 ...