标签:内置对象 sys ini 语法 异常 内容 基础 管理 应用
Servlet是用Java编写的服务器端程序。其主要功能在于交互式地浏览和修改数据,生成动态Web内容。狭义的Servlet是指Java语言实现的一个接口,广义的Servlet是指任何实现了这个Servlet接口的类,一般情况下,人们将Servlet理解为后者。
JSP经编译后就变成了Servlet。(JSP的本质就是Servlet,JVM只能识别java的类,不能识别JSP的代码,Web容器将JSP的代码编译成JVM能够识别的java类)
2.JSP更擅长表现于页面显示,Servlet更擅长于逻辑控制。
3.Servlet中没有内置对象,JSP中的内置对象都是必须通过HttpServletRequest对象,HttpServletResponse对象以及HttpServlet对象得到。
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 Hello World! 11 </body> 12 </html>
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 7 <%! 8 String str="全局变量"; 9 %> 10 <%! 11 public void fun1(){ 12 System.out.println("全局方法"); 13 } 14 %> 15 <%! 16 class C{ 17 private int a; 18 public void f(){ 19 System.out.println("全局类"); 20 } 21 } 22 %> 23 <% 24 int a=1234; 25 String b="java"; 26 out.println(a+b+"局部变量"); 27 %> 28 <title>Insert title here</title> 29 </head> 30 <body> 31 <%=b %> 32 </body> 33 </html>
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <%-- <h1>静态包含</h1> 11 <%@ include file="common/head.html" %> 12 <p>content</p> 13 <%@ include file="common/footer.jsp" %> --%> 14 <h1>动态包含</h1> 15 <jsp:include page="common/head.html"/> 16 <p>content</p> 17 <jsp:include page="common/footer.jsp"/> 18 </body> 19 </html>
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <jsp:forward page="target.jsp"> 11 <jsp:param value="admin" name="userName"/> 12 <jsp:param value="123456" name="password"/> 13 </jsp:forward> 14 </body> 15 </html>
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <% 11 // 设置两个page范围的数据 key-> value 12 pageContext.setAttribute("name","page小王"); 13 pageContext.setAttribute("age",12); 14 %> 15 <% 16 // 取值 17 String name=(String)pageContext.getAttribute("name"); 18 int age=(Integer)pageContext.getAttribute("age"); 19 %> 20 <font>姓名:<%=name %></font> 21 <font>年龄:<%=age %></font> 22 </body> 23 </html>
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <% 11 // 设置两个request范围的数据 key-> value 12 request.setAttribute("name","request小王"); 13 request.setAttribute("age",12); 14 %> 15 <jsp:forward page="requestTarget.jsp"></jsp:forward> 16 </body> 17 </html>
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <%@ page import="java.util.*" %> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 8 <title>Insert title here</title> 9 </head> 10 <body> 11 <% 12 // 取值 13 String name=(String)request.getAttribute("name"); 14 int age=(Integer)request.getAttribute("age"); 15 // 获取头信息 16 Enumeration enu=request.getHeaderNames(); 17 while(enu.hasMoreElements()){ 18 String headerName=(String)enu.nextElement(); 19 String headerValue=request.getHeader(headerName); 20 %> 21 <h4><%=headerName %> <%=headerValue %></h4> 22 <% 23 } 24 %> 25 <font>姓名:<%=name %></font> 26 <font>年龄:<%=age %></font> 27 </body> 28 </html>
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <% 11 // 设置两个session范围的数据 key-> value 12 session.setAttribute("name","session小王"); 13 session.setAttribute("age",12); 14 %> 15 <h1>session值设置完毕!</h1> 16 </body> 17 </html>
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <% 11 // 取值 12 String name=(String)session.getAttribute("name"); 13 int age=(Integer)session.getAttribute("age"); 14 %> 15 <font>姓名:<%=name %></font> 16 <font>年龄:<%=age %></font> 17 </body> 18 </html>
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <% 11 // 设置两个application范围的数据 key-> value 12 application.setAttribute("name","application小王"); 13 application.setAttribute("age",12); 14 %> 15 <h1>application值设置完毕!</h1> 16 </body> 17 </html>
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 7 <title>Insert title here</title> 8 </head> 9 <body> 10 <% 11 // 取值 12 String name=(String)application.getAttribute("name"); 13 int age=(Integer)application.getAttribute("age"); 14 %> 15 <font>姓名:<%=name %></font> 16 <font>年龄:<%=age %></font> 17 </body> 18 </html>
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <%@ page import="java.util.*"%> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 8 <title>Insert title here</title> 9 </head> 10 <body> 11 <% 12 // 每隔一秒刷新一次页面 13 response.setHeader("refresh","1"); 14 // 获取当前时间 15 Date myDate=new Date(); 16 %> 17 当前时间:<%= myDate.toLocaleString() %> 18 </body> 19 </html>
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <%@ page import="java.util.*"%> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 8 <title>Insert title here</title> 9 </head> 10 <body> 11 <% 12 int totalbuffer=out.getBufferSize(); // 获取总共缓冲区的大小 13 int available=out.getRemaining(); // 获取未使用的缓冲区的大小 14 int user=totalbuffer-available; // 获取使用的缓冲区大小 15 out.println("总共缓冲区的大小:"+totalbuffer); 16 out.println("未使用的缓冲区的大小:"+available); 17 out.println("使用的缓冲区大小:"+user); 18 %> 19 </body> 20 </html>
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <%@ page import="java.util.*"%> 4 <%@ page errorPage="error.jsp"%> 5 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 6 <html> 7 <head> 8 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 9 <title>Insert title here</title> 10 </head> 11 <body> 12 <% 13 pageContext.setAttribute("name0", "pageInfo"); 14 request.setAttribute("name1", "requestInfo"); 15 session.setAttribute("name2", "sessionInfo"); 16 application.setAttribute("name3", "applicationInfo"); 17 18 out.println("使用pageContext<br/>"); 19 out.println("page中的属性值:"+pageContext.getAttribute("name0")+"<br/>"); 20 out.println("request中的属性值:"+pageContext.getRequest().getAttribute("name1")+"<br/>"); 21 out.println("session中的属性值:"+pageContext.getSession().getAttribute("name2")+"<br/>"); 22 out.println("application中的属性值:"+pageContext.getServletContext().getAttribute("name3")+"<br/>"); 23 %> 24 </body> 25 </html>
标签:内置对象 sys ini 语法 异常 内容 基础 管理 应用
原文地址:http://www.cnblogs.com/ShawnYuki/p/6358407.html