Dream2018
2018-03-18 21:15
首选时index.jsp页面代码:
<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>
<%
long i=System.currentTimeMillis() ;
Date d=new Date(i);
SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日hh时mm分ss秒");
String t=sdf.format(d);
%>
<body>
<h1>通过post方式传递一个参数给detail.jsp页面</h1><br>
<form action="detail.jsp" method="post">
<table>
<tr>
<td><input type="text" name="time" value=<%=t %>></td>
<td><input type="submit" value="提交当前时间"></td>
</tr>
</table>
</form>
</body>
</html>
其次是detail.jsp页面代码:
<%@ page language="java" import="java.util.*" contentType="text/html; charset=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>处理传递过来的参数</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>
<h1>接受传递过来的参数</h1>
<%
request.setCharacterEncoding("utf-8");
String time= request.getParameter("time");//接受传递过来的参数
%>
传递过来的参数是:<%=time %><br>
<%
//将传递过来的参数保存到Cookie中
//直接创建Cookie对象,就会导致每一访问的时候都会创建一个最新的Cookie对象,
//所以我们可以先判断有没有Cookie对象,并将要保存的值先保存到一个字符串变量中,
//然后再取出
Cookie[] cookies=request.getCookies();
if(cookies !=null && cookies.length>0){
for(Cookie c:cookies){
time += "<br>"+c.getValue()+"#";//将cookie对象的值保存到time变量中,并以“#”分割
}
}
Cookie cookie=new Cookie("timecookie",time);
response.addCookie(cookie);
%>
<a href="cookie.jsp">在同一次会话中获取保存的cookie</a>
</body>
</html>
然后是cookie.jsp页面代码:
<%@ page language="java" import="java.util.*" contentType="text/html; charset=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 'cookie.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>
<%
String creatCookieTime=null;
Cookie[] cookies=request.getCookies();
for(Cookie c:cookies){
if(c.getName().equals("timecookie")){
creatCookieTime=c.getValue();
out.println("获得Cookie对象"+creatCookieTime);
}
}
String[] arr=creatCookieTime.split("#");
if(arr !=null && arr.length>0){
for(String s:arr){
%>
<p>Cookie的创建时间是<%=s%><br></p>
<%
}
}
%>
</body>
</html>
运行结果是:
这里是想获取ID吗?
发现你的一个错误
JAVA遇见HTML——JSP篇
248278 学习 · 3071 问题
相似问题