参照:
浅谈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();