一. 储备知识
1.1 jsp脚本片段
翻译:按照编写顺序拷贝到service方法中
编写:<% %>
作用:在service中编写java业务逻辑代码,可通过多个jsp脚本片段结合控制页面内容的显示
由于jsp脚本片段翻译后在service方法中使用,所以可以直接使用service方法中的参数request、response
<%@ 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脚本片段翻译后在service方法中使用,
所以可以直接使用service方法中的参数request、response
*/
//获取请求参数age
String ageStr = request.getParameter("age");
int age = 0;
if(ageStr != null){
age = Integer.parseInt(ageStr);
}
if(age > 18){
%>
<h4>你已经18岁了</h4>
<%
}
%>
</body>
</html>
1.2 jsp表达式
翻译:作为out.print的参数写到响应体中
语法:<%= %>
作用:用来向页面中输出变量或对象的值
<%@ 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>
<%
String name = request.getParameter("username");
if(name != null){
%>
<h2>name的值是:</h2><%=name %>
<%
}
%>
</body>
</html>
1.3 jsp指令
介绍常用的include指令:静态包含指令,可以将其他页面中的内容拷贝到当前页面中
特点:
将另一个页面拷贝到当前页面后,服务器再将当前页面翻译为Java类,并编译为class文件,最后执行(只有一个文件被翻译被翻译)
应用:
将多个页面共有的部分提取为一个单独的页面,在多个页面中再通过include指令引入,便于共有部分的修改