jsp、cookie了解

版权声明:转载请联系作者本人!!! https://blog.csdn.net/weixin_42061805/article/details/81809480

JSP

什么是jsp?

jsp全称Java Server Page,可以理解为Servlet的另外一种形式,目前jsp主要负责的是请求发起页面(显示表单,超链接)和请求结束页面(显示数据),而Servlet主要负责的是处理数据。jsp由html、java脚本、jsp标签(指令)构成。

jsp注释

在最后浏览器显示时源代码不会显示此注释

<%-- .... --%>

jsp的三种Java脚本

<%...%>相当于一个Java方法,Java方法中能写的这里就可以写

<%=...%>主要作为输出作用。response.getWriter().print("...")中能写的这里就可以写

<%!...%>相当于Java的一个类,Java类中能写的这里也可以写

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
</head>

<body>
	<!-- 相当于一个方法体 -->
	<%
		int a = 100;
	%>
	<!-- 相当于一个输出 -->
	<%=a%>

	<%!public void fun() {
		System.out.println("fun()...");
	}%>

	<%
		fun();
	%>
</body>
</html>

jsp脚本的简单示例(循环生成表格)

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
</head>

<body>
	<table border="1" align="center" width="500px">
		<tr>
			<td>姓名</td>
			<td>年龄</td>
			<td>性别</td>
		</tr>
		<%
			for (int i = 1; i < 99; i++){
		 %>
		 
		 <tr>
		 	<td><%=i %></td>
		 	<td><%=i+1 %></td>
		 	<td><%=i+2 %></td>
		 </tr>
		 
		 <%
		 	}
		  %>
	</table>
</body>
</html>

Cookie

什么是Cookie?

(百度词条)Cookie,有时也用其复数形式 Cookies,指某些网站为了辨别用户身份、进行 session 跟踪而储存在用户本地终端上的数据(通常经过加密)

Cookie的应用

//创建Cookie
Cookie cookie = new Cookie("aaa","AAA");
response.addCookie(cookie);

//获取Cookie数组
Cookie[] cookies = resquest.getCookies();//返回的是一个数组

//设置cookie生命期限
/*
* setMaxAge > 0    生命期限的具体值,值=60意思就是60s
* setMaxAge < 0    cookie只在内存中出现,浏览器关闭时即死亡
* setMaxAge = 0    检测到有cookie存在时,立马杀死
*/
cookie.setMaxAge();

//设置Cookie路径
cookie.setPath(uri);

Cookie的域

不同域名之间可以共享Cookie,如www.baidu.com、zhidao.baidu.com等等

//设置domain
cookie.setDomain(".baidu.com");
//设置路径
cookie.setPath("/");

 

猜你喜欢

转载自blog.csdn.net/weixin_42061805/article/details/81809480