脚本程序
<> 或者,您也可以编写与其等价的XML语句,就像下面这样: <jsp:scriptlet> 代码片段 </jsp:scriptlet>任何文本、HTML标签、JSP元素必须写在脚本程序的外面。 ###中文编码问题 如果我们要在页面正常显示中文,我们需要在 JSP 文件头部添加以下代码:<>
JSP声明
JSP表达式
<%@ directive attribute="value" %>
JSP内置对象
1.内置对象简介(web容器创建的 不需要new)
- 四种作用域范围
- out(是JSPWriter的一个实例,是向客户端输出内容常用的对象)
void println();void clear() 清除缓冲区的内容
- request/response(请求与响应模式)
5 session - application
-
其他内置对象
JSP客户端请求
当浏览器请求一个网页时,它会向网络服务器发送一系列不能被直接读取的信息,因为这些信息是作为HTTP信息头的一部分来传送的
HTTP请求例子
在这个例子中,我们会使用HttpServletRequest类的getHeaderNames()方法来读取HTTP信息头。这个方法以枚举的形式返回当前HTTP请求的头信息。
获取Enumeration对象后,用标准的方式来遍历Enumeration对象,用hasMoreElements()方法来确定什么时候停止,用nextElement()方法来获得每个参数的名字
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@ page import="java.io.*,java.util.*" %> 4 <!DOCTYPE html> 5 <html> 6 <head> 7 <meta charset="utf-8"> 8 <title>菜鸟教程(runoob.com)</title> 9 </head> 10 <body> 11 <h2>HTTP 头部请求实例</h2> 12 <table width="100%" border="1" align="center"> 13 <tr bgcolor="#949494"> 14 <th>Header Name</th><th>Header Value(s)</th> 15 </tr> 16 <% 17 Enumeration headerNames = request.getHeaderNames(); 18 while(headerNames.hasMoreElements()) { 19 String paramName = (String)headerNames.nextElement(); 20 out.print("<tr><td>" + paramName + "</td> "); 21 String paramValue = request.getHeader(paramName); 22 out.println("<td> " + paramValue + "</td></tr> "); 23 } 24 %> 25 </table> 26 </body> 27 </html>
JSP服务器响应
Response响应对象主要将JSP容器处理后的结果传回到客户端。可以通过response变量设置HTTP的状态和向客户端发送数据,如Cookie、HTTP文件头信息等。
HTTP响应头程序示例
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.io.*,java.util.*" %> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>菜鸟教程(runoob.com)</title> </head> <body> <h2>自动刷新实例</h2> <% // 设置每隔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 + " "); %> </body> </html>
Http状态吗实例
1 <html> 2 <head> 3 <title>Setting HTTP Status Code</title> 4 </head> 5 <body> 6 <% 7 // 设置错误代码,并说明原因 8 response.sendError(407, "Need authentication!!!" ); 9 %> 10 </body> 11 </html>
Get获取URL
1 <%-- 2 Created by IntelliJ IDEA. 3 User: tangwei9 4 Date: 2017/7/14 5 Time: 10:22 6 To change this template use File | Settings | File Templates. 7 --%> 8 <%@ page contentType="text/html;charset=UTF-8" language="java" %> 9 <%@ page import ="java.io.*,java.util.*"%> 10 <html> 11 <head> 12 <title>GET方式请求实例</title> 13 </head> 14 <body> 15 <h1>Get方式获取URL</h1> 16 <u1> 17 <li><p><b>站点名</b> 18 <%= request.getParameter("name")%> 19 </p>></li> 20 21 <li><p><b>网址名</b> 22 <%= request.getParameter("url")%> 23 24 </p>></li> 25 26 </u1> 27 </body> 28 </html> 29
JSP Cookie
设置Cookie
<% 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的时间为1天 name.setMaxAge(60*60*24); name.setMaxAge(60*60*24); //在响应头添加Cookie response.addCookie(name); response.addCookie(url); %>
读取Cookie
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@ page import="java.net.*" %> 4 5 <% 6 Cookie cookie = null; 7 Cookie[] cookies = null; 8 // 获取cookies的数据,是一个数组 9 cookies = request.getCookies(); 10 if( cookies != null ){ 11 out.println(""); 12 13 for (int i = 0; i < cookies.length; i++){ 14 cookie = cookies[i]; 15 out.print("参数名 : " + cookie.getName()); 16 out.print("<br>"); 17 out.print("参数值: " + URLDecoder.decode(cookie.getValue(), "utf-8") +" <br>"); 18 out.print("------------------------------------<br>"); 19 } 20 }else{ 21 out.println("没有发现 Cookie"); } 22 23 %>
删除Cookie
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@ page import="java.net.*" %> 4 5 <% 6 Cookie cookie = null; 7 Cookie[] cookies = null; 8 // 获取当前域名下的cookies,是一个数组 9 cookies = request.getCookies(); 10 if( cookies != null ){ 11 out.println("查找 Cookie 名与值");for (int i = 0; i < cookies.length; i++){ cookie = cookies[i]; 12 13 if((cookie.getName( )).compareTo("name") == 0 ){ 14 cookie.setMaxAge(0); 15 response.addCookie(cookie); 16 out.print("删除 Cookie: " + 17 cookie.getName( ) + "); 18 " 19 } 20 out.print("参数名 : " + cookie.getName()); 21 out.print(""); 22 23 out.print("参数值: " + URLDecoder.decode(cookie.getValue(), "utf-8") +" "); 24 25 out.print("------------------------------------"); 26 27 } 28 }else{ 29 out.println("没有发现 Cookie"); 30 31 } 32 %> 33 34
JSP Session
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@ page import="java.io.,java.util." %> 4 <% 5 // 获取session创建时间 6 Date createTime = new Date(session.getCreationTime()); 7 // 获取最后访问页面的时间 8 Date lastAccessTime = new Date(session.getLastAccessedTime()); 9 10 String title = "再次访问菜鸟教程实例"; 11 Integer visitCount = new Integer(0); 12 String visitCountKey = new String("visitCount"); 13 String userIDKey = new String("userID"); 14 String userID = new String("ABCD"); 15 16 // 检测网页是否由新的访问用户 17 if (session.isNew()){ 18 title = "访问菜鸟教程实例"; 19 session.setAttribute(userIDKey, userID); 20 session.setAttribute(visitCountKey, visitCount); 21 } else { 22 visitCount = (Integer)session.getAttribute(visitCountKey); 23 visitCount += 1; 24 userID = (String)session.getAttribute(userIDKey); 25 session.setAttribute(visitCountKey, visitCount); 26 } 27 %>
文件上传代码
http://www.runoob.com/jsp/jsp-file-uploading.html
重定向百度代码
1 <% 2 String site = new String("http://www.baidu.com"); 3 response.setStatus(response.SC_MOVED_TEMPORARILY); 4 response.setHeader("Location",site); 5 6 %>
访问量统计
<% Integer count = (Integer) application.getAttribute("count"); if(count == null || count == 0){ out.print("<br>第一次访问</br>"); count = 1; }else { out.print("欢迎访问本站"); count++; } application.setAttribute("count",count); %> <p>页面的访问量为:<%= count%></p>> 发送邮件
http://www.runoob.com/jsp/jsp-sending-email.html
JSP操作数据库
http://www.runoob.com/jsp/jsp-database-access.html