WEB访问绝对路径和cookie的设置

<span style="font-family: Arial, Helvetica, sans-serif;"><strong>代码如下:</strong></span>
<span style="font-family: Arial, Helvetica, sans-serif;"><%@page import="org.apache.commons.lang.math.RandomUtils"%></span>
<%@page import="java.util.Random"%>
<%@page import="java.io.File"%>
<%@page import="java.io.FileInputStream"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
	function onload()
	{
		alert(document.cookie);
	}
</script>
</head>
<body onload="onload()">
	<%
	String baseURL = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath();
	request.setAttribute("basePath", baseURL);
	
	//*
	//添加cookie
	/*for(int i = 20;i>0;i--)
	{//*/
		int randomId = RandomUtils.nextInt();
		out.write("randomId: "+randomId+"<br>");
		Cookie cookie = new Cookie("ID"+randomId,randomId+"");
		cookie.setMaxAge(60);
		cookie.setComment("this is test cookie id");
		cookie.setVersion(1);
		cookie.setSecure(true);
		response.addCookie(cookie);
	//}
	
	//*/
	
	%>
	baseUrl:${basePath}<%out.write("<br>request.getContextPath: "+request.getContextPath()); %>
	<hr>
	<!-- <img alt="" src="<%=request.getContextPath()%>/images/add.gif">
	<img alt="" src="<%=request.getContextPath()%>/png/helloworld.png"> -->
	<%
	/*  File file = new File(request.getContextPath()+"/png/helloworld.png");
		FileInputStream fileInputStream = new FileInputStream(file);
		ServletOutputStream os = response.getOutputStream();
		
		for(int b=-1;(b=fileInputStream.read())!=-1;)
			os.write(b);
		fileInputStream.close();
		os.close(); //*/
		
		//*
		out.write("<br>getScheme: "+request.getScheme());
		out.write("<br>getServerName: "+request.getServerName());
		out.write("<br>getServerPort: "+request.getServerPort());
		out.write("<br>getServletPath: "+request.getServletPath());
		out.write("<br>getServletContext: "+request.getServletContext());
		out.write("<br>getLocalName: "+request.getLocalName());
		out.write("<br>getLocalAddr: "+request.getLocalAddr());
		out.write("<br>getLocalPort: "+request.getLocalPort());
		out.write("<br>getContextPath: "+request.getContextPath());
		out.write("<br>getCookies: "+request.getCookies());
		out.write("<hr>Build BaseURL:");
		out.write("<br>baseUrl: "+request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath());
		
		out.write("<hr>Test Cookies:");
		Cookie[] cookies = request.getCookies();
		for(Cookie t_cookie : cookies)
		{
			//重新设置cookie的有效时间  从而达到删除的效果
			/*t_cookie.setMaxAge(1);
			response.addCookie(t_cookie);	//*/
			out.write("<br>getVersion: "+ t_cookie.getVersion());
			out.write("<br>getDomain: "+ t_cookie.getDomain());
			out.write("<br>getMaxAge: "+ t_cookie.getMaxAge());
			out.write("<br>getName: "+ t_cookie.getName());
			out.write("<br>getPath: "+ t_cookie.getPath());
			out.write("<br>getValue: "+ t_cookie.getValue());
			out.write("<br>getSecure: "+ t_cookie.getSecure());
			out.write("<br>getPath: "+ t_cookie.getClass());
			out.write("<br>*********************************************");
		}
		//*/
	%>
</body>
</html>


运行数据如下:





你可能感兴趣的:(WEB访问绝对路径和cookie的设置)