1. ¿Qué es JSP?
Páginas del servidor Java [jsp] —página del servidor
Java [ páginas que contienen programas Java que se ejecutan en el servidor {HTML}]
Página— [HTML] —incluidos los códigos de programa Java— que se ejecutan en el servidor.
P.ej:
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.util.Date"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<%
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss E");
String datetime=sdf.format(new Date());
%>
<h1>当前系统时间:<% out.write(datetime); %></h1>
</body>
</html>
JSP es en realidad HTML que contiene código Java. Utilice etiquetas html para representar efectos estáticos, y el código java incluido maneja datos dinámicos.
2. ¿Cuáles son los elementos de la página JSP?
2.1
Formato de instrucción : <% @ nombre de instrucción atributo 1 = ”valor de atributo 1" ...%>
Función: alguna información adicional que se declara en la página jsp para ser ejecutada por el navegador.
Instrucciones comunes: instrucción de página / incluir instrucción / instrucción taglib
1. instrucción de página — atributos comunes
1.language = "java"
—especificar idioma 2.contentType = "text / html; charset = utf-8": establece el tipo de contenido de la página jsp actual
3.pageEncoding = "utf -8 ": establece la codificación de caracteres actual de la página jsp
4.import =" java.util.Date ": importa el paquete de dependencia de la biblioteca de clases java [pueden aparecer múltiples]
5.isELIgnored =" false ", ya sea para habilitar la expresión EL language "$ {expression}", false está disponible, true no es
posible 6.isErrorPage = "false": si es una página de error, si lo es, puede usar objetos JSP incorporados de excepción.
Por ejemplo:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8" isELIgnored="false" isErrorPage="true"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<h1>page指令--常见属性</h1>
<h2>1.language="java"---指定语言</h2>
<h2>2.contentType="text/html; charset=utf-8":设置当前jsp页面的内容类型</h2>
<h2>3.pageEncoding="utf-8":设置当前jsp页面的字符编码</h2>
<h2>4.import="java.util.Date":导入java类库的依赖包【可以出现多个】</h2>
<h3><%@ page import="java.text.SimpleDateFormat"<br>
import="java.util.Date"%></h3>
<hr>
<h3><%@ page import="java.text.SimpleDateFormat"%></h3>
<h3><%@ page import="java.util.Date"%></h3>
<h2>5.isELIgnored="false"---是否启用EL表达式语言 “${表达式}”,false可用,true不可以</h2>
<h3>123+234=${123+234}</h3>
<h2>6.isErrorPage="false"---是否是一个错误页面,如果是那么就可以使用exception的JSP内置对象</h2>
<%
exception.printStackTrace();
%>
</body>
</html>
2. Incluya la instrucción [incluya otros recursos para ingresar a esta página jsp] -atributos
comunes como:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8" isELIgnored="false" isErrorPage="true"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<h1>include指令--常见属性</h1>
<h2>include指令--包含其他的资源进入本jsp页面</h2>
<h2>file="test.txt":指定被包含的其他的资源的路径</h2>
包含文本信息:<%@include file="test.txt" %><br>
包含html信息:<%@include file="mytest.html" %>
包含jsp信息:<%@include file="test1.jsp" %>
</body>
</html>
Instrucción 3.taglib: cuando se usan etiquetas JSTL [biblioteca de etiquetas estándar jsp] en la página jsp, se usa para importar la biblioteca de etiquetas
[esta instrucción no se procesará temporalmente, la presentaremos en detalle cuando aprendamos JSTL más adelante]
2.2
Formato de fragmento de código Java : <% programa java%>
Función: programa java anidado en html [proporcionar datos dinámicos]
Por ejemplo:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"
import="java.text.SimpleDateFormat"
import="java.util.Date"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<%
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss E");
String datetime=sdf.format(new Date());
%>
<h1>当前系统时间:<% out.write(datetime); %></h1>
</body>
</html>
2.3jsp
formato de expresión : <% = expresión / variable / método%>
Función: Calcula el resultado de una determinada expresión, o el resultado de una variable / método.
Formato de declaración 2.4jsp
: <%! Class / variable / method%>
Función: define algunas variables / métodos / clases de Java que necesitas en la página jsp
Por ejemplo:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8" isELIgnored="false" isErrorPage="true"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<h1>jsp表达式</h1>
<h2>计算某种表达式的结果,或者变量/方法的结果。</h2>
<h2>算术表达式 123+234=<%= 123+234 %></h2>
<h2>关系表达式 123>234=<%= 123>234 %></h2>
<h2>逻辑表达式 (123>234) || (123 < 234)===<%= (123 > 234) || (123 < 234) %></h2>
<h1>jsp声明</h1>
<h2>在jsp页面中定义一些自己需要的java变量/方法/类</h2>
<%!
String name="zhangsan";
public String getInfo(){
return "hello,wangxing";
}
%>
<h2>声明中的变量name==<%= name %></h2>
<h2>声明中的变量getInfo()==<%= getInfo() %></h2>
</body>
</html>
Formato de acción 2.5jsp : <jsp: nombre de acción atributo de acción ...> </ jsp: nombre de acción>
Función: encapsular algunos programas java de uso común para facilitar la llamada
de acciones jsp incluidas
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8" isELIgnored="false" isErrorPage="true"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<h1>jsp动作</h1>
<h2>将一些常用的java程序封装,方便调用</h2>
<h2>include的jsp动作--包含其他的资源进入本jsp页面</h2>
包含文本信息:<jsp:include page="test.txt"></jsp:include><br>
包含html信息:<jsp:include page="mytest.html"></jsp:include>
包含jsp信息:<jsp:include page="test1.jsp"></jsp:include>
</body>
</html>
acción de avance jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8" isELIgnored="false" isErrorPage="true"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<h1>jsp动作</h1>
<h2>forward的jsp动作--跳转到其他的资源</h2>
<jsp:forward page="mytest.html"></jsp:forward>
</body>
</html>
¿Cuál es la diferencia entre la directiva include [<% @ include file = ””%>] y la acción include [<jsp: include page = ""> </ jsp: include>] ?
3. El proceso de ejecución de la página
JSP 1. JSP es esencialmente un programa Servlet.
Jsp se convertirá en un programa Servlet cuando se ejecute.
Guardar ubicación
F: \ 20200728 \ javawebworkspace.metadata.plugins \ org.eclipse.wst.server.core \ tmp0 \ work \ Catalina \ localhost \ TestJSP1 \ org \ apache \ jsp
public final class test5_jsp extends org.apache.jasper.runtime.HttpJspBase
implements org.apache.jasper.runtime.JspSourceDependent,
org.apache.jasper.runtime.JspSourceImports {
org.apache.jasper.runtime.HttpJspBase继承过javax.servlet.http.HttpServlet
Cuando solicitamos una página jsp, después de que el servidor recibe la solicitud de la página jsp, primero convertirá el archivo jsp accedido en un archivo servlet, continuará compilando el archivo servlet convertido para obtener el archivo de código de bytes correspondiente al archivo servlet, y luego ejecute Este archivo de código de bytes compilado encapsula el resultado en ejecución en el objeto de respuesta y lo devuelve al navegador del cliente.
Desventajas: La primera visita a la página jsp será más lenta.
4. ¿La diferencia entre Servlet y JSP?
5. ¿Cuáles son los objetos incorporados en JSP, cuáles son sus tipos, cuáles son sus funciones y cuáles son los métodos comúnmente utilizados?
El objeto incorporado en JSP es el objeto pre-creado cuando el servidor está en ejecución, no necesitamos crear [nuevo] por nosotros mismos. JSP es un programa de Servlet.
Hay 9 objetos integrados en JSP .