Jsp
Jsp 概述:
JSP全名为Java Server Pages,中文名叫java服务器页面,其根本是一个简化的Servlet设计,它[1] 是由Sun Microsystems公司倡导、许多公司参与一起建立的一种动态网页技术标准。
B/S VS C/S:
B/S 系统是 浏览器/服务器
BS运行原理:发送一个请求到服务器,然后服务器处理
C/S 系统是 客户端/服务器
B/S C/S 比较 瘦客户端,未来发展趋势;
page 指令介绍:
Language : 用来定义要使用的脚本语言;
contentType:定义 JSP 字符的编码和页面响应的 MIME 类型;
pageEncoding:Jsp 页面的字符编码
scriptlet 标签:
通过 scriptlet 标签我们可以在 Jsp 里嵌入 Java 代码;
第一种:<%! %> 我们可以在里面定义全局变量、方法、类;
第二种:<% %> 我们可以在里面定义局部变量、编写语句;
第三种:<%= %> 我们可以在里面输出一个变量或一个具体内容;
Jsp 注释:
<!-- --> Html 注释 客户端可见
<%-- --%> Jsp 注释 客户端不可见
// java 单行注释
/* */ java 多行注释
Jsp 包含指令:
<%@ include file=”要包含的文件”%> 静态包含 先包含,后编译处理;
<jsp:include page=”要包含的文件”> 动态包含 先编译处理,后包含;
以后开发用动态包含;
Jsp 跳转指令:
<jsp:forward page="">
<jsp:param value=”” name=””>
</jsp:forward>
1 <jsp:forward page="target.jsp"> 2 <jsp:param value="java1234" name="userName"/> 3 <jsp:param value="123456" name="password"/> 4 </jsp:forward>
1 服务器内部跳转后的页面<br/> 2 userName:<%=request.getParameter("userName") %><br/> 3 password:<%=request.getParameter("password") %><br/>
服务器内部跳转,可带参数;
Jsp 九大内置对象及四大作用域概述:
在 Jsp 开发中,Jsp 提供了 9 个内置对象,这些内置对象将由容器为用户进行实例化,用户直接使用即可。这个
9 个内置对象分别是:pageContext,request,response,session,application,config,out,page,exception;常用的是前面 5
个,需要熟练掌握;
在 Jsp 开发中,可以保存数据,Jsp 提供了四种数据保存范围;分别是 page,request,session,application;
Jsp 四大作用域:
Page 范围:只在一个页面中保存数据; javax.servlet.jsp.PageContext(抽象类)
Request 范围:只在一个请求中保存数据; javax.servlet.http.HttpServletRequest(接口)
Session 范围:在一次会话范围中保存数据,仅供单个用户使用;javax.servlet.http.HttpSession(接口)
Application 范围:在整个服务器上保存数据,所有用户共享;javax.servlet.ServletContext(接口)
1 <% 2 // 设置两个request范围的数据 key-> value 3 request.setAttribute("name","request王二小"); 4 request.setAttribute("age",12); 5 %> 6 <jsp:forward page="requestTarget.jsp"></jsp:forward>
1 <% 2 // 取值 3 String name=(String)request.getAttribute("name"); 4 int age=(Integer)request.getAttribute("age"); 5 %> 6 <font>姓名:<%=name %></font> 7 <font>年龄:<%=age %></font>
1 <% 2 // 获取头信息 3 Enumeration enu=request.getHeaderNames(); 4 while(enu.hasMoreElements()){ 5 String headerName=(String)enu.nextElement(); 6 String headerValue=request.getHeader(headerName); 7 %> 8 <h4><%=headerName %> <%=headerValue %></h4> 9 <% 10 } 11 %>
response 对象:
Response 内置对象和 request 内置对象是相对应的,response 内置对象用于响应客户请求,向客户端输出信息;
javax.servlet.HttpServletResponse 接口
1,自动刷新应用
1 //因为不是内置对象所以要导包 2 <% 3 page import="java.util.*" 4 %> 5 <% 6 //每隔一秒刷新页面 7 respone.setHeader("refresh","1"); 8 //获取当前时间 9 Date myDate=new Date(); 10 %> 11 当前时间:<%= myDate.toLocaleString()%>
2,页面重定向应用 客户端跳转
1 response.sendRedirect("跳转的页面");
3,操作 cookie 应用 post get 方法比较 post 放数据包里 get 放 Url 后面 get 数据量小,不安全;
4,cookie 和 session 的比较 cookie 信息是存客户端的,session 信息是存服务器的;
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 <-- 重置 --> 10 <script type="text/javascript"> 11 function resetValue(){ 12 document.getElementById("userName").value=""; 13 document.getElementById("pwd").value=""; 14 } 15 </script> 16 <-- 读取Cookie --> 17 <% 18 String userName=null; 19 String pwd=null; 20 //用Cookie数组来接收传过来的Cookie的信息 21 Cookie[] cookies=request.getCookies(); 22 //循环Cookie 23 for(int i=0;cookies!=null &&i<cookies.length;i++){ 24 if(cookies[i].getName().equals("userNameAndPwd")){ 25 userName=cookies[i].getValue().split("-")[0]; 26 pwd=cookies[i].getValue().split("-")[1]; 27 } 28 } 29 30 if(userName==null){ 31 userName=""; 32 } 33 34 if(pwd==null){ 35 pwd=""; 36 } 37 %> 38 </head> 39 <body> 40 41 <form action="userLogin.jsp" method="post"> 42 <table> 43 <tr> 44 <td>用户名:</td> 45 <td><input type="text" id="userName" name="userName" value="<%=userName%>"/></td> 46 </tr> 47 <tr> 48 <td>密码:</td> 49 <td><input type="password" id="pwd" name="pwd" value="<%=pwd %>" /></td> 50 </tr> 51 <tr> 52 <td>记住密码:</td> 53 <td><input type="checkbox" id="remember" name="remember" value="remember-me"/></td> 54 </tr> 55 <tr> 56 <td><input type="submit" value="登录"/></td> 57 <td><input type="button" value="重置" onclick="resetValue()"/></td> 58 </tr> 59 </table> 60 </form> 61 </body> 62 </html>
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <%@ page import="javax.servlet.http.*"%> 4 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 String userName=request.getParameter("userName"); // 获取用户名 14 String pwd=request.getParameter("pwd"); // 获取密码 15 String remember=request.getParameter("remember"); // 获取记住密码 16 17 if("remember-me".equals(remember)){ 18 Cookie userNameAndPwd=new Cookie("userNameAndPwd",userName+"-"+pwd); 19 userNameAndPwd.setMaxAge(1*60*60*24*7); // cookie记录一个星期 20 response.addCookie(userNameAndPwd); // 保存cookie 21 System.out.println("设置Cookie成功"); 22 } 23 response.sendRedirect("response03.jsp"); 24 %> 25 26 </body> 27 </html>
out 对象:
Out 内置对象主要用来向客户端输出各种类型的数据,同时还可以管理应用服务器上的输出缓冲区。所以 out 内
置对象的方法是向客户端输出数据和管理缓冲区; 底层 javax.servlet.jsp.JspWriter 抽象类
1 <% 2 int totalbuffer=out.getBufferSize(); // 获取总共缓冲区的大小 3 int available=out.getRemaining(); // 获取未使用的缓冲区的大小 4 int user=totalbuffer-available; // 获取使用的缓冲区大小 5 out.println("总共缓冲区的大小:"+totalbuffer); 6 out.println("未使用的缓冲区的大小:"+available); 7 out.println("使用的缓冲区大小:"+user); 8 %>
config 对象:
Config 内置对象是 JSP 页面通过 JSP 容器进行初始化时被传递的对象。javax.servlet.ServletConfig 。在 Servlet
初始化的时候,JPS 引擎通过 config 向它传递信息。这种信息可以是属性名和属性值匹配的参数,也可以是通过
ServletContext 对象传递的服务器的有关信息;
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 String jdbcName=config.getInitParameter("jdbcName"); 13 String dbUrl=config.getInitParameter("dbUrl"); 14 %> 15 <h1>驱动名称:<%=jdbcName %></h1> 16 <h1>连接地址:<%=dbUrl %></h1> 17 </body> 18 </html>
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> 3 <display-name>HeadFirstJspServletChap02</display-name> 4 <welcome-file-list> 5 <welcome-file>index.html</welcome-file> 6 <welcome-file>index.htm</welcome-file> 7 <welcome-file>index.jsp</welcome-file> 8 <welcome-file>default.html</welcome-file> 9 <welcome-file>default.htm</welcome-file> 10 <welcome-file>default.jsp</welcome-file> 11 </welcome-file-list> 12 13 <servlet> 14 <-- 与service-mapping中的service-name值相同才能查到sysInit.jsp --> 15 <servlet-name>init</servlet-name> 16 <jsp-file>/sysInit.jsp</jsp-file> 17 <-- 初始化参数 --> 18 <init-param> 19 <param-name>jdbcName</param-name> 20 <param-value>com.mysql.jdbc.Driver</param-value> 21 </init-param> 22 <init-param> 23 <param-name>dbUrl</param-name> 24 <param-value>jdbc:mysql://localhost:3306/db_xx</param-value> 25 </init-param> 26 </servlet> 27 28 <servlet-mapping> 29 <servlet-name>init</servlet-name> 30 <url-pattern>/init</url-pattern> 31 </servlet-mapping> 32 </web-app>
exception 对象:
Exception 内置对象用来处理 JSP 文件在执行时发生的所有异常,它是 java.lang.Throwable 类的一个对象。
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 int a=1; 14 int b=0; 15 out.println(a/b); 16 %> 17 </body> 18 </html>
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <%@ page import="java.util.*"%> 4 <%@ page isErrorPage="true"%> 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 if(exception!=null){ 14 out.println("程序错误信息:"); 15 out.println(exception.getMessage()); 16 } 17 %> 18 </body> 19 </html>
pageContext 对象:
pageContext 内置对象是一个比较特殊的对象。它相当于页面中所有对象功能的集合,即使用它可以访问到本页面
中所有对象。pageContext 内置对象由 Jsp 容器创建并初始化,pageContext 对象提供了对 JSP 页面所有对象及控件
的访问。
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>
Javabean 组件引入:
JavaBean 是使用 Java 语言开发的一个可重用的组件,在 JSP 开发中可以使用 JavaBean 减少重复代码,使整个
JSP 代码的开发更简洁。
1 package cyb.entity; 2 3 public class Student { 4 5 private String name; 6 private int age; 7 8 public String getName() { 9 return name; 10 } 11 public void setName(String name) { 12 this.name = name; 13 } 14 public int getAge() { 15 return age; 16 } 17 public void setAge(int age) { 18 this.age = age; 19 } 20 21 22 }
1 <% 2 Student student=new Student(); 3 student.setName("王二小"); 4 student.setAge(12); 5 %> 6 <h1>姓名:<%=student.getName() %></h1> 7 <h1>年龄:<%=student.getAge() %></h1>
jsp:useBean 创建 javabean:
<jsp:useBean id="实例化对象名称" scope="保存范围" class="类完整名称"/>
Scope,一共有 page,request,session 和 application4 个属性范围,默认是 page;
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:useBean id="student" scope="page" class="com.java1234.model.Student"/> 11 <% 12 student.setName("王二小2"); 13 student.setAge(12); 14 %> 15 <h1>姓名:<%=student.getName() %></h1> 16 <h1>年龄:<%=student.getAge() %></h1> 17 </body> 18 </html>
jsp:setProperty 设置 javabean 属性值:
<jsp:setProperty property="属性名称" name="实例化对象的名称" value="属性值" param="参数名称"/>
Property=”*” 自动匹配所有
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 <form action="javabean03-3.jsp" method="post"> 11 <table> 12 <tr> 13 <td>姓名:</td> 14 <td><input type="text" name="name"/></td> 15 </tr> 16 <tr> 17 <td>年龄:</td> 18 <td><input type="text" name="age"/></td> 19 </tr> 20 <tr> 21 <td colspan="2"><input type="submit" value="提交"/></td> 22 </tr> 23 </table> 24 </form> 25 </body> 26 </html>
1 <%@ page language="java" contentType="text/html; charset=utf-8" 2 pageEncoding="utf-8"%> 3 <%@ page import="com.java1234.model.Student" %> 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 request.setCharacterEncoding("utf-8"); 13 String name=request.getParameter("name"); 14 String age=request.getParameter("age"); 15 Student student=new Student(); 16 student.setName(name); 17 student.setAge(Integer.parseInt(age)); 18 %> 19 <h1>姓名:<%=student.getName() %></h1> 20 <h1>年龄:<%=student.getAge() %></h1> 21 </body> 22 </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.setCharacterEncoding("utf-8"); 12 %> 13 <jsp:useBean id="student" scope="page" class="com.java1234.model.Student"/> 14 <jsp:setProperty property="*" name="student"/> 15 <h1>姓名:<%=student.getName() %></h1> 16 <h1>年龄:<%=student.getAge() %></h1> 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 <form action="javabean03-3.jsp" method="post"> 11 <table> 12 <tr> 13 <td>姓名:</td> 14 <td><input type="text" name="userNameame"/></td> 15 </tr> 16 <tr> 17 <td>年龄:</td> 18 <td><input type="text" name="age"/></td> 19 </tr> 20 <tr> 21 <td colspan="2"><input type="submit" value="提交"/></td> 22 </tr> 23 </table> 24 </form> 25 </body> 26 </html>
javaBean1.jsp
jsp:getProperty 获取 javabean 属性值:
<jsp:getProperty property="属性名称" name="实例化对象的名称"/>
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:useBean id="student" scope="page" class="com.java1234.model.Student"/> 11 <% 12 student.setName("王二小2"); 13 student.setAge(12); 14 %> 15 <h1>姓名:<jsp:getProperty property="name" name="student"/></h1> 16 <h1>年龄:<jsp:getProperty property="age" name="student"/></h1> 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 <jsp:useBean id="student" scope="request" class="com.java1234.model.Student"/> 11 <jsp:setProperty property="name" name="student" value="王八蛋"/> 12 <jsp:setProperty property="age" name="student" value="12"/> 13 <jsp:forward page="target01.jsp"/> 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 <jsp:useBean id="student" scope="request" class="com.java1234.model.Student"/> 11 <h1>姓名:<jsp:getProperty property="name" name="student"/></h1> 12 <h1>年龄:<jsp:getProperty property="age" name="student"/></h1> 13 </body> 14 </html>
javabean 的保存范围:
Javabean 的保存范围有 page,request,session.application,默认是 page;
改一下scope的范围试
javabean 删除:
Page 范围:pageContext.removeAttribute(“javaBean Name”);
request 范围:request.removeAttribute(“javaBean Name”);
session 范围:session.removeAttribute(“javaBean Name”);
application 范围:application.removeAttribute(“javaBean Name”);
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 <--改一下session就行了--> 11 <% 12 session.removeAttribute("student"); 13 %> 14 <h1>javabean已删除!</h1> 15 </body> 16 </html>

浙公网安备 33010602011771号