jsp的一些基本语法
jsp页面内容
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head> <body>
<%! public void a(){} %> <!-- 通过声明, 可在servlet中定义方法,注意前面的"!"符合 -->
<%! static{System.out.println("static");} %>
<%
Date date=new Date();
String time=date.toLocaleString();
out.write(time);
%>
<br>
<%=time%> <!-- 脚本表达式,用于向浏览器输出数据 --> <%for(int i=0;i<5;i++){ %>
hello for
<%} %> <%-- jsp注释内容 --%>
</body>
</html>
生成servlet的内容:
package org.apache.jsp; import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
import java.util.*; public final class index_jsp extends org.apache.jasper.runtime.HttpJspBase
implements org.apache.jasper.runtime.JspSourceDependent { public void a(){}
static{System.out.println("static");}
private static final javax.servlet.jsp.JspFactory _jspxFactory =
javax.servlet.jsp.JspFactory.getDefaultFactory(); private static java.util.List<java.lang.String> _jspx_dependants; private javax.el.ExpressionFactory _el_expressionfactory;
private org.apache.tomcat.InstanceManager _jsp_instancemanager; public java.util.List<java.lang.String> getDependants() {
return _jspx_dependants;
} public void _jspInit() {
_el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
_jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig());
} 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 { 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, , true);
_jspx_page_context = pageContext;
application = pageContext.getServletContext();
config = pageContext.getServletConfig();
session = pageContext.getSession();
out = pageContext.getOut();
_jspx_out = out; out.write('\r');
out.write('\n'); String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; out.write("\r\n");
out.write("\r\n");
out.write("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\r\n");
out.write("<html>\r\n");
out.write(" <head>\r\n");
out.write(" <base href=\"");
out.print(basePath);
out.write("\">\r\n");
out.write(" \r\n");
out.write(" <title>My JSP 'index.jsp' starting page</title>\r\n");
out.write(" \r\n");
out.write("\t<meta http-equiv=\"pragma\" content=\"no-cache\">\r\n");
out.write("\t<meta http-equiv=\"cache-control\" content=\"no-cache\">\r\n");
out.write("\t<meta http-equiv=\"expires\" content=\"0\"> \r\n");
out.write("\t<meta http-equiv=\"keywords\" content=\"keyword1,keyword2,keyword3\">\r\n");
out.write("\t<meta http-equiv=\"description\" content=\"This is my page\">\r\n");
out.write("\t<!--\r\n");
out.write("\t<link rel=\"stylesheet\" type=\"text/css\" href=\"styles.css\">\r\n");
out.write("\t-->\r\n");
out.write("\r\n");
out.write(" </head>\r\n");
out.write(" \r\n");
out.write(" <body>\r\n");
out.write(" ");
out.write(" <!-- 閫氳繃澹版槑锛?鍙湪servlet涓畾涔夋柟娉曪紝娉ㄦ剰鍓嶉潰鐨刓"!\"绗﹀悎 -->\r\n");
out.write(" ");
out.write("\r\n");
out.write(" "); Date date=new Date();
String time=date.toLocaleString();
out.write(time); out.write("\r\n");
out.write(" <br>\r\n");
out.write(" ");
out.print(time);
out.write(" <!-- 鑴氭湰琛ㄨ揪寮忥紝鐢ㄤ簬鍚戞祻瑙堝櫒杈撳嚭鏁版嵁 -->\r\n");
out.write(" \r\n");
out.write(" ");
for(int i=0;i<5;i++){
out.write("\r\n");
out.write(" hello for\r\n");
out.write(" ");
}
out.write("\r\n");
out.write(" \r\n");
out.write(" ");
out.write("\r\n");
out.write(" </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() != )
try { out.clearBuffer(); } catch (java.io.IOException e) {}
if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
}
} finally {
_jspxFactory.releasePageContext(_jspx_page_context);
}
}
}
包含页面
<%@ include file="/public/head.jsp" %> 静态包含
<% request.getRequestDispatcher("/public/head.jsp").include(request, response); %> 动态包含
如果是错误页面,可标记为错误页面,将可获取到错误信息。
<%@ page language="java" import="java.util.*" isErrorPage="true" pageEncoding="UTF-8"%>
<%
String message = exception.getMessage();
out.write(message);
%>
以下代码将先输出 haha 再输出 message ,因为数据最终都是通过response.getWriter()输出的,out.write()相当于另外一个缓冲区,out.write()执行
完后将再进入response缓冲区,最后再将response的数据发给tomcat执行输出。
<%
String message = exception.getMessage();
out.write(message);
response.getWriter().write("haha");
%>
pageContext封装了所有域对象,经常用在自定义标签
<%
request.setAttribute("data", "aaa");
pageContext.setAttribute("data", "bbb",PageContext.REQUEST_SCOPE);
String data = (String) pageContext.getAttribute("data",
PageContext.REQUEST_SCOPE);
out.write(data); String data2=(String)pageContext.findAttribute("data");
out.write(data2);
%>
${data} <%-- el表达式 --%> <% pageContext.forward("/index2.jsp"); %>
<% pageContext.include("/public/head.jsp"); %>
常用jsp标签
<jsp:forward page="/index2.jsp"></jsp:forward>
<jsp:include page="/public/head.jsp"></jsp:include> <!-- 动态标签 -->
<jsp:forward page="/servlet/testServlet?data2=123">
<jsp:param value="value" name="data" />
</jsp:forward>
取参数
String data=(String)request.getParameter("data");
System.out.println("data:"+data);
String data2=(String)request.getParameter("data2");
System.out.println("data2:"+data2);
将jsp页面映射到servlet
<servlet>
<servlet-name>xxx</servlet-name>
<jsp-file>/index3.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>xxx</servlet-name>
<url-pattern>/.html</url-pattern>
</servlet-mapping>
jsp的一些基本语法的更多相关文章
- eclipse加速之禁用JS、jsp等文件的语法验证,eclipsejs
eclipse加速之禁用JS.jsp等文件的语法验证 去除eclipse的JS验证:将windows->preference->Java Script->Validator-> ...
- jsp 安装环境与基本语法
1.什么是web应用程序? web应用程序是一种可以通过web访问的应用程序.web应用程序的最大好处是用户很容易访问应用程序.用户只需要有浏览器即可,不需要安装其它软件. 2.搭建 java web ...
- JSP(介绍,语法,指令)
什么是JSP JSP全名为Java Server Pages,java服务器页面.JSP是一种基于文本的程序,其特点就是HTML和Java代码共同存在! JSP的工作原理 其实JSP在第一次被访问的时 ...
- jsp页面执行java语法,获取的值在页面调用
首先在页面头引用用到类的包路径 写需要执行的java语法 页面转换引用 <!-- 引用包路径 --> <%@ page language="java" impor ...
- jsp页面的基本语法
JSP全称Java Server Pages,顾名思义就是运行中java服务器中页面,也就是在我们JavaWeb中的动态页面,其本质就是一个Servlet. 学习jsp的基本语法主要就是学习服务器是如 ...
- JSP的介绍及语法详解
一.简介 > HTML - HTML擅长显示一个静态的网页,但是不能调用Java程序. > Servlet - Servlet擅长调用Java程序和后台进行交互,但是它不擅长显示一个完整的 ...
- JAVA入门[11]-jsp几点基础语法
一.include <%@include file="copyright.jsp"%> <jsp:include page="copyright.jsp ...
- eclipse中jsp文档无语法着色,安装Eclipse Java Web Developer Tools插件
一.安装Eclipse Java Web Developer Tools插件 1.eclipse菜单:help/install New Software,打开Available Software窗体: ...
- jsp常用的jstl语法
<c:forEach items="<object>" begin="<int>" end="<int>&q ...
随机推荐
- Oracle数据库——Scheduler Job
日常的运维工作中,我们经常使用Linux Server的anacron服务来使得服务器执行一下计划之内的任务,可以按照特定的时间间隔,重复的执行相关的命令或者相关的脚本,来完成预期的目标,能够节省相关 ...
- MapReduce: 一个巨大的倒退
前言 databasecolumn 的数据库大牛们(其中包括PostgreSQL的最初伯克利领导:Michael Stonebraker)最近写了一篇评论当前如日中天的MapReduce 技术的文章, ...
- c#强制执行内存回收
[DllImport("psapi.dll")] private static extern int EmptyWorkingSet(int hProcess); GC.Colle ...
- Web前端开发笔试&面试_05
>>CW 1.JavaScript的2种变量范围有什么不同? 2.JavaScript 的对象有哪些? 3.
- css之opacity
设置div元素的不透明级别 语法: value :从0.0(完全透明)到1.0(完全不透明) inherit:应该从父元素继承opacity属性 z-index 属性设置元素的堆叠顺序,仅能在定位元素 ...
- java中的循环
while循环: while(循环条件){ 循环操作 } 停止while循环有两种方式:1.不再满足while后的循环条件时,循环终止: ...
- apache commons vfs 文件夹监控
package test.vfs; import java.io.File; import org.apache.commons.logging.Log; import org.apache.comm ...
- 制作OS X 10.10.3启动安装U盘
http://www.cnblogs.com/Bob-wei/p/4471407.html 1.获得“Install OS X Yosemite.app” 2.准备一个8GB的U盘,用磁盘工具“抹掉” ...
- DBA常用SQL之表空间与数据文件
)), ) ts_size from dba_tablespaces t, dba_data_files d where t.tablespace_name = d.tablespace_name g ...
- RMAN备份与恢复之表空间
通过以下事例来说明表空间的恢复,删除表空间内的数据文件,删除后在针对位于该表空间的表进行插入记录以及实施检查点进程 SQL> select a.tablespace_name from dba_ ...