JavaWeb预习(jsp)
1.jsp指令元素
page:定义页面全局属性
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8" import="java.util.Date"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<% Date date = new Date(); %>
<h1>page's import shili:</h1>
<p>now time is <%=date %></p>
</body>
</html>
include:将其他的文件插入jsp页面
//include1.jsp<%@page language="java" pageEncoding="UTF-8"%>
<html><head><title>被include包含的文件</title></head><body><h1>hello world</h1></body>
</html>
<%@page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html><head><title>include指令实例</title></head><body><center>现在的时间:<%=new Date() %></center><%@include file="include2.jsp" %></body>
</html>
taglib:引用自定义、第三方标签库
2.jsp动作元素
<jsp:include>
引入其他文件,
与<%@include file %>类似,但是前者是动态包含,可以传参数。
<jsp:include file="include2">
<jsp:forward>
停止当前页,转向另一个页面,但浏览器的地址不发生变化
<jsp:forward page="page2">
<jsp:param>
不能独立使用,需作为<jsp:include>、<jsp:forward>标签的子标签使用,
给include传入参数,动态加载页面;给forward传入参数,在被传入的页面进行参数获取
<jsp:include page="test2"><jsp:param value="" name=""/></jsp:include><jsp:forward page="test3"><jsp:param value="" name=""/></jsp:forward>
3.jsp内置对象
1.request(请求对象)
传递参数,有3种形式
1.使用jsp的forward、include。
2.表单传递
<%@ page pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="infoReceive.jsp" method="post">姓名<input name="rdName"><br>电话<input name="phName"><br><input type="submit" value="提交">
</form></body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
request.setCharacterEncoding("UTF-8");
String str1 = request.getParameter("rdName");
String str2 = request.getParameter("phName");
%>
<font>
您的姓名为:<%=str1 %>;
<br>
电话:<%=str2 %>;
</font></body>
</html>
3.追加在网址后面的参数
2.response(响应对象)
提供了页面重定向sendRedirect方法。
3.session(会话对象)
4.application(应用程序对象)
5.page(页面对象)
6.pageContext(页面上下文对象)
7.out(输出对象)
主要用于输出响应信息。主要方法:print(),与<%=exp%>作用类似
<%@ page pageEncoding="UTF-8"%>
<html><body><% out.print("hello world<br>"); %><%="hello world" %></body>
</html>