JSP复习二

摘要:HTTP 头部请求实例,自动刷新实例,使用URL的GET方法实例,使用表单的 POST 方法实例,页面重定向,设置cookie,获取cookies

index2.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"

    pageEncoding="utf-8"%>
<%@ page import="java.io.*,java.util.*" %>
<%@ page import="java.net.*" %>


<!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>JSP复习</title>
</head>
<body>
<h1> HTTP 头部请求实例</h1>
<table width="100%" border="1" align="center">
<tr bgcolor="#949494">
<th>Header Name</th>
<th>Header Value(s)</th>
</tr>
<%
Enumeration headerNames = request.getHeaderNames();
while(headerNames.hasMoreElements()) {
  String paramName = (String)headerNames.nextElement();
  out.print("<tr><td>" + paramName + "</td>\n");
  String paramValue = request.getHeader(paramName);
  out.println("<td> " + paramValue + "</td></tr>\n");
}
%>
</table>
<h1> 自动刷新实例</h1>
<%
   // 设置每隔5秒自动刷新
   response.setIntHeader("Refresh", 5);
   // 获取当前时间
   Calendar calendar = new GregorianCalendar();
   String am_pm;
   int hour = calendar.get(Calendar.HOUR);
   int minute = calendar.get(Calendar.MINUTE);
   int second = calendar.get(Calendar.SECOND);
   if(calendar.get(Calendar.AM_PM) == 0)
      am_pm = "AM";
   else
      am_pm = "PM";
   String CT = hour+":"+ minute +":"+ second +" "+ am_pm;
   out.println("当前时间: " + CT + "\n");
%>
<%-- <%
   // 设置错误代码,并说明原因
   response.sendError(407, "Need authentication!!! -.-" );
%> --%>
<h1> 使用URL的GET方法实例</h1>
<ul>
<li>
<p><b>站点名:</b>
<%
// 解决中文乱码的问题
String name0=request.getParameter("name");
String name1="-.-";
if(name0!=null){
name1 = new String(name0.getBytes("ISO-8859-1"),"UTF-8");
}
%>
<%=name1%></p>
</li>
<li>
<p><b>网址:</b>
<%
String url0=request.getParameter("url");
String url1="-.-";
if(url0 != null){
url1=url0;
}
%>
<%= url1%></p>
</li>
</ul>


<h1> 使用表单的 POST 方法实例</h1>
<ul>
<li>
<p><b>站点名:</b>
<%=name1%></p>
</li>
<li>
<p><b>网址:</b>
<%= url1%></p>
</li>
</ul>


<h1> 从复选框中读取数据</h1>
<ul>
<li><p><b>Google 是否选中:</b>
  <%= request.getParameter("google")%>
</p></li>
<li><p><b>菜鸟教程是否选中:</b>
  <%= request.getParameter("runoob")%>
</p></li>
<li><p><b>淘宝是否选中:</b>
  <%= request.getParameter("taobao")%>
</p></li>
</ul>


<h1> 读取所有表单参数</h1>
<table width="100%" border="1" align="center">
<tr bgcolor="#949494">
<th>参数名</th>
<th>参数值</th>
</tr>
<%
  Enumeration paramNames = request.getParameterNames();
  while(paramNames.hasMoreElements()) {
     String paramName = (String)paramNames.nextElement();
     out.print("<tr><td>" + paramName + "</td>\n");
     String paramValue = request.getParameter(paramName);
     out.println("<td> " + paramValue + "</td></tr>\n");
  }
%>
</table>


<h1> 页面重定向</h1>
<%-- <%response.sendRedirect("http://www.runoob.com"); %> --%>
<%-- <%
   // 重定向到新地址
   String site = new String("http://www.runoob.com");
   response.setStatus(response.SC_MOVED_TEMPORARILY);
   response.setHeader("Location", site); 
%> --%>
<%
   // 编码,解决中文乱码   
   String str = URLEncoder.encode(request.getParameter("name"),"utf-8");  
   // 设置 name 和 url cookie 
   Cookie name = new Cookie("name",
           str);
   Cookie url = new Cookie("url",
              request.getParameter("url"));


   // 设置cookie过期时间为24小时。
   name.setMaxAge(60*60*24); 
   url.setMaxAge(60*60*24); 


   // 在响应头部添加cookie
   response.addCookie( name );
   response.addCookie( url );
%>
<%
   Cookie cookie = null;
   Cookie[] cookies = null;
   // 获取cookies的数据,是一个数组
   cookies = request.getCookies();
   if( cookies != null ){
      out.println("<h2> 查找 Cookie 名与值</h2>");
      for (int i = 0; i < cookies.length; i++){
         cookie = cookies[i];
        
         out.print("参数名 : " + cookie.getName());
         out.print("<br>");
         out.print("参数值: " + URLDecoder.decode(cookie.getValue(), "utf-8") +" <br>");
         out.print("------------------------------------<br>");
      }
  }else{
      out.println("<h2>没有发现 Cookie</h2>");
  }
%>
<h1> Session 跟踪</h1>
 <%!
   String title = "再次访问菜鸟教程实例";
   Integer visitCount = new Integer(0);
   String visitCountKey = new String("visitCount");
   String userIDKey = new String("userID");
   String userID = new String("ABCD"); 
%>
<%
//获取session创建时间
Date createTime = new Date(session.getCreationTime());
// 获取最后访问页面的时间
Date lastAccessTime = new Date(session.getLastAccessedTime());
//检测网页是否由新的访问用户
if (session.isNew()){
   title = "访问菜鸟教程实例";
   session.setAttribute(userIDKey, userID);
   session.setAttribute(visitCountKey,  visitCount);
} else {
    visitCount = (Integer)session.getAttribute(visitCountKey);
    if(visitCount==null){
    visitCount=0;
    }
    visitCount = visitCount+1;
    userID = (String)session.getAttribute(userIDKey);
    if(userID==null){
    userID="ABCD";
    }
    session.setAttribute(visitCountKey,  visitCount);
}
%>
<table border="1" align="center"> 
<tr bgcolor="#949494">
  <th>Session 信息</th>
  <th>值</th>
</tr> 
<tr>
  <td>id</td>
  <td><% out.print( session.getId()); %></td>
</tr> 
<tr>
  <td>创建时间</td>
  <td><% out.print(createTime); %></td>
</tr> 
<tr>
  <td>最后访问时间</td>
  <td><% out.print(lastAccessTime); %></td>
</tr> 
<tr>
  <td>用户 ID</td>
  <td><% out.print(userID); %></td>
</tr> 
<tr>
  <td>访问次数</td>
  <td><% out.print(visitCount); %></td>
</tr> 
</table>
</body>

</html>

text.html

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
</head>
<body>
<h1>使用表单的GET方法实例</h1>
<form action="index2.jsp" method="GET">
站点名: <input type="text" name="name"><br />
网&nbsp;&nbsp;&nbsp;&nbsp;址: <input type="text" name="url" /><br />
<input type="submit" value="提交" /><br />
</form>


<h1>使用表单的POST方法实例</h1>
<form action="index2.jsp" method="POST">
站点名: <input type="text" name="name"><br />
网&nbsp;&nbsp;&nbsp;&nbsp;址: <input type="text" name="url" /><br />
<input type="submit" value="提交" /><br />
</form>


<h1>传递 Checkbox 数据到JSP程序</h1>
<form action="index2.jsp" method="POST" target="_blank">
<input type="checkbox" name="google" checked="checked" />Google
<input type="checkbox" name="runoob"  />菜鸟教程
<input type="checkbox" name="taobao" checked="checked" />淘宝
<input type="submit" value="选择网站" />
</form>
</body>
</html>

参考资料:菜鸟教程

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值