讲给Android程序员看的前端系列教程(40集免费视频教程+源码)
版权声明
- 本文原创作者:谷哥的小弟
- 作者博客地址:http://blog.csdn.net/lfdfhl
在本节教程中,我们将介绍JSP常用的基本语法。
JSP表达式
JSP表达式用于将数据输出至客户端。
语法如下:
<%=表达式expression %>
示例如下:
<%=System.currentTimeMillis() %>
注意事项:
JSP表达式中的变量或者表达式之后不能有分号;
JSP脚本片段
JSP脚本片段是指嵌套在<% %>中的一条或多条Java代码。
语法如下:
<%
Java Codes;
%>
示例如下:
<%
int number=9527;
out.println("number="+number);
%>
注意事项:
- JSP脚本片段中的Java代码必须严格遵守Java语法规范,否则编译报错。
- JSP页面中可以有一个或者多个JSP脚本片段。
- 单个JSP脚本片段中的Java语句可以是不完整的。但是,多个JSP脚本片段组合后必须是完整的Java语句;否则,编译报错。因为,JSP脚本片段中的Java代码会被原封不动地移动到JSP页面对应的Servlet的_jspService( )方法中。
JSP声明
默认情况下,当JSP页面被翻译成Servlet程序时,JSP中包含的脚本片段、表达式、前端代码都将被翻译至Servlet的_jspService( )方法中。此时,JSP脚本片段中定义的变量都将成为_jspService( )方法中的局部变量,JSP脚本片段中定义的方法也都将插人到_jspService( )方法中。此时,因为在方法中再定义方法造成语法错误。为了解决类似问题,在JSP技术中提供了声明。
语法如下:
<%!
Java Codes;
%>
在该声明中的Java代码将会被翻译至_jspService( )方法之外。
示例如下:
<%!
private String name="lucy";
private int age=18;
public void printInfo(){
System.out.println("name="+name+",age="+age);
}
%>
注意事项:
- JSP声明中的Java代码必须严格遵守Java语法规范,否则编译报错。
- JSP页面中可以有一个或者多个JSP声明。
- 单个JSP声明中的Java语句可以是不完整的。但是,多个JSP声明组合后必须是完整的Java语句;否则,编译报错。因为,JSP声明中的Java代码会被原封不动地移动到JSP页面对应的Servlet的_jspService( )方法。
JSP注释
JSP中常用注释如下:
-
HTML风格的注释,即
<!-- 注释内容 -->
-
Java风格的注释,即
// 和 / *…… * /
-
JSP风格的注释,即
<%-- 注释内容 --%>
注意事项:
当用户在浏览器中查看JSP源文件时可看见HTML风格的注释,但是看不见Java风格的注释和JSP风格的注释。
综合示例
在此,以示例的形式综合运用JSP基本语法。
index.jsp
<%@ 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>JSP基本语法</title>
</head>
<body>
<h2 align="left" style="color: red;">本文作者:谷哥的小弟</h2>
<h2 align="left" style="color: red;">博客地址:http://blog.csdn.net/lfdfhl</h2>
<!-- THML风格的注释 -->
<h2>JSP语法学习</h2>
<%
//Java风格的注释
int number=9527;
out.println("number="+number+"<br/>");
%>
<%-- JSP风格的注释 --%>
<%=System.currentTimeMillis() %>
<%-- JSP声明 --%>
<%!
private String name="lucy";
private int age=18;
public void printInfo(){
System.out.println("name="+name+",age="+age);
}
%>
</body>
</html>
index_jsp.java
/*
* Generated by the Jasper component of Apache Tomcat
* Version: Apache Tomcat/9.0.19
* Generated at: 2019-10-04 04:27:09 UTC
* Note: The last modified time of this file was set to
* the last modified time of the source file after
* generation to assist with modification tracking.
*/
package org.apache.jsp;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
public final class index_jsp extends org.apache.jasper.runtime.HttpJspBase
implements org.apache.jasper.runtime.JspSourceDependent,
org.apache.jasper.runtime.JspSourceImports {
private String name="lucy";
private int age=18;
public void printInfo(){
System.out.println("name="+name+",age="+age);
}
private static final javax.servlet.jsp.JspFactory _jspxFactory =
javax.servlet.jsp.JspFactory.getDefaultFactory();
private static java.util.Map<java.lang.String,java.lang.Long> _jspx_dependants;
private static final java.util.Set<java.lang.String> _jspx_imports_packages;
private static final java.util.Set<java.lang.String> _jspx_imports_classes;
static {
_jspx_imports_packages = new java.util.HashSet<>();
_jspx_imports_packages.add("javax.servlet");
_jspx_imports_packages.add("javax.servlet.http");
_jspx_imports_packages.add("javax.servlet.jsp");
_jspx_imports_classes = null;
}
private volatile javax.el.ExpressionFactory _el_expressionfactory;
private volatile org.apache.tomcat.InstanceManager _jsp_instancemanager;
public java.util.Map<java.lang.String,java.lang.Long> getDependants() {
return _jspx_dependants;
}
public java.util.Set<java.lang.String> getPackageImports() {
return _jspx_imports_packages;
}
public java.util.Set<java.lang.String> getClassImports() {
return _jspx_imports_classes;
}
public javax.el.ExpressionFactory _jsp_getExpressionFactory() {
if (_el_expressionfactory == null) {
synchronized (this) {
if (_el_expressionfactory == null) {
_el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
}
}
}
return _el_expressionfactory;
}
public org.apache.tomcat.InstanceManager _jsp_getInstanceManager() {
if (_jsp_instancemanager == null) {
synchronized (this) {
if (_jsp_instancemanager == null) {
_jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig());
}
}
}
return _jsp_instancemanager;
}
public void _jspInit() {
}
public void _jspDestroy() {
}
public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response)
throws java.io.IOException, javax.servlet.ServletException {
if (!javax.servlet.DispatcherType.ERROR.equals(request.getDispatcherType())) {
final java.lang.String _jspx_method = request.getMethod();
if ("OPTIONS".equals(_jspx_method)) {
response.setHeader("Allow","GET, HEAD, POST, OPTIONS");
return;
}
if (!"GET".equals(_jspx_method) && !"POST".equals(_jspx_method) && !"HEAD".equals(_jspx_method)) {
response.setHeader("Allow","GET, HEAD, POST, OPTIONS");
response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED, "JSP 只允许 GET、POST 或 HEAD。Jasper 还允许 OPTIONS");
return;
}
}
final javax.servlet.jsp.PageContext pageContext;
javax.servlet.http.HttpSession session = null;
final javax.servlet.ServletContext application;
final javax.servlet.ServletConfig config;
javax.servlet.jsp.JspWriter out = null;
final java.lang.Object page = this;
javax.servlet.jsp.JspWriter _jspx_out = null;
javax.servlet.jsp.PageContext _jspx_page_context = null;
try {
response.setContentType("text/html; charset=UTF-8");
pageContext = _jspxFactory.getPageContext(this, request, response,
null, true, 8192, true);
_jspx_page_context = pageContext;
application = pageContext.getServletContext();
config = pageContext.getServletConfig();
session = pageContext.getSession();
out = pageContext.getOut();
_jspx_out = out;
out.write("\r\n");
out.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\r\n");
out.write("<html>\r\n");
out.write("\t<head>\r\n");
out.write("\t\t<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\r\n");
out.write("\t\t<title>JSP基本语法</title>\r\n");
out.write("\t</head>\r\n");
out.write("\t<body>\r\n");
out.write("\t\t<h2 align=\"left\" style=\"color: red;\">本文作者:谷哥的小弟</h2>\r\n");
out.write("\t\t<h2 align=\"left\" style=\"color: red;\">博客地址:http://blog.csdn.net/lfdfhl</h2>\r\n");
out.write("\r\n");
out.write("\t\t<!-- THML风格的注释 -->\r\n");
out.write("\t\t<h2>JSP语法学习</h2>\r\n");
out.write("\r\n");
out.write("\t\t");
//Java风格的注释
int number=9527;
out.println("number="+number+"<br/>");
out.write("\r\n");
out.write("\t\t\r\n");
out.write("\t\t");
out.write("\r\n");
out.write("\t\t");
out.print(System.currentTimeMillis() );
out.write("\r\n");
out.write("\r\n");
out.write("\t\t");
out.write("\r\n");
out.write("\t\t");
out.write("\r\n");
out.write("\t</body>\r\n");
out.write("</html>\r\n");
} catch (java.lang.Throwable t) {
if (!(t instanceof javax.servlet.jsp.SkipPageException)){
out = _jspx_out;
if (out != null && out.getBufferSize() != 0)
try {
if (response.isCommitted()) {
out.flush();
} else {
out.clearBuffer();
}
} catch (java.io.IOException e) {}
if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
else throw new ServletException(t);
}
} finally {
_jspxFactory.releasePageContext(_jspx_page_context);
}
}
}
转载:https://blog.csdn.net/lfdfhl/article/details/102060589
查看评论