1.page指令:描述页面信息
pageENcoding:软件编码
contentType:浏览器编码
2.include指令:将多个网页合成一个网页,静态包含网页
问题:1.在网页源代码中,会形成错误的多遍代码,将主页面代码和副页面各删除一半。
2.多个网页中的代码不能有任何冲突
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%@ include file ="NewFile.jsp" %>
<h3>这位是一个主网页1</h3>
<h3>这是另一个网页的变量:<%=a %></h3>
</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>
<h3>这是一个导航栏</h3>
<h3><% int a=12;%></h3>
查看网页源代码
完整代码
3.include动作
jsp动作:include,param,forward
基本语法:
1.<jsp:include page=""/>
2.<jsp:include page="">
包含网页传递数据
include指令:静态包含,代码复用,一起生成网页,
注意:被包含网页不需要写部分网页代码,一起编译执行,运行1次,运行效率高
include动作:动态包含,结果复用,共同生成网页,
注意:被包含网页不需要写结构网页代码,一起编译执行,运行多次,运行效率低
<%@ 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>
<jsp:include page="NewFile1.jsp"/>
<h3>这是一个网页1</h3>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<h3>次级网页</h3>
4.param动作
param动作:向其他网页传递数据
用法:<jsp:param value="值" name="值对应的名字">
注意事项:动作不能单独使用,要和其他动作一起使用
<%@ 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>
<jsp:include page="NewFile1.jsp">
<jsp:param value="18" name="age"/>
</jsp:include>
<h3>这是一个网页1</h3>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<h3>次级网页</h3>
<%
String s = request.getParameter("age");
out.print(s);
%>
传递中文乱码:数据传输和数据接收编码
解决方法:在发送和接收的网页中添加<%request.setCharacterEncoding("UTF-8");%>
<%@ 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");%>
<jsp:include page="NewFile1.jsp">
<jsp:param value="12" name="id1"/>
<jsp:param value="12.34" name="id2"/>
<jsp:param value="张红" name="id3"/>
</jsp:include>
<h3>这是一个网页1</h3>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<h3>次级网页</h3>
<%
request.setCharacterEncoding("UTF-8");
String s = request.getParameter("id1");
String s1 = request.getParameter("id2");
String s2 = request.getParameter("id3");
Double b = Double.parseDouble(s1)+10;//把s字符串转换为double类型
Integer i=Integer.parseInt(s)+10;//首字母大写是类名
String s5=b + "";
out.print(s);
out.print(s1);
out.print(s2);
%>
forward:跳转到新页面
跳转到新页面
语法:1.<jsp:forward page=“relativeURL”>不用传参数
2.<jsp:forward page="要转向的页面" >
使用param动作传递数据
<jsp:param name= "paramterName" value= " parametrValue" />传参数
</jsp:forward>
注意:用forward跳转后的页面显示的网址是跳转之前的网页
执行forward代码后不在执行
<%@ 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>
<h3>这是主页面</h3>
<%-- <jsp:forward page="NewFile1.jsp"/> --%>
<%request.setCharacterEncoding("UTF-8");%>
<jsp:forward page="NewFile1.jsp" >
<jsp:param name= "张三" value= "name" />
</jsp:forward>
<% request.setAttribute("id", "18"); %>
</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"); %>
<h3>这是跳转网页</h3>
<%
String s = request.getParameter("name");
%>
<%
out.print(request.getAttribute("id"));
%>
<h3>登录成功,欢迎</h3>
</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>
<h3>这是主页面</h3>
<%-- <jsp:forward page="NewFile1.jsp"/> --%>
<%request.setCharacterEncoding("UTF-8");%>
<% request.setAttribute("id", "18"); %>
<jsp:forward page="NewFile1.jsp" >
<jsp:param name= "张三" value= "name" />
</jsp:forward>
</body>
</html>