标签:编译 his 打印 调用 port acl new 包含 导入
首先在没有jsp文件的情况下,是以下方式写页面:
package com.oracle.index; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class indexServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8"); response.getWriter().write("<!DOCTYPE html>"); response.getWriter().write("<html>"); response.getWriter().write("<head>"); response.getWriter().write("<meta http-equiv=‘Content-Type‘ content=‘text/html; charset=UTF-8‘>"); response.getWriter().write("<title>Insert title here</title>"); response.getWriter().write("</head>"); response.getWriter().write("<body>"); response.getWriter().write("1234567890"); response.getWriter().write("</body>"); response.getWriter().write("</html>"); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
效果如下:
1、jsp
(实质上就是一个servlet)
(1)jsp脚本:
1)<% java代码 %>--相当于写在方法里;所以不能定义方法;
2)<%= java变量或表达式%>---相当于写在out.print()里面
3)<%!java代码%>---相当于成员变量;所以能定义方法;
(2)jsp注释:
1)//java单行注释 jsp源码、翻译后的servlet、
2)/*java多行注释*/ jsp源码、翻译后的servlet、
3)<!--html注释--> jsp源码、翻译后的servlet、html源代码
4)<%--jsp注释--%> jsp源码、
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" session="false" errorPage="error.jsp"%> <!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> <!-- html注释 --> <%--jsp注释 --%> <%int i = 0; System.out.println(i); //java单行注释 %> <%= i %> <%! String str = "ni hao java"; /*java多行注释*/ %> <%=str %> </body> </html>
具体表现如下:
网页显示为:
控制台显示为:
具体显示的java文件可以发现具体的注释位置和变量
/* * Generated by the Jasper component of Apache Tomcat * Version: Apache Tomcat/7.0.52 * Generated at: 2018-11-16 03:11:37 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 { String str = "ni hao java"; /*java多行注释*/ 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 javax.el.ExpressionFactory _el_expressionfactory; private org.apache.tomcat.InstanceManager _jsp_instancemanager; public java.util.Map<java.lang.String,java.lang.Long> 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; 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, "error.jsp", false, 8192, true); _jspx_page_context = pageContext; application = pageContext.getServletContext(); config = pageContext.getServletConfig(); 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("<head>\r\n"); out.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\r\n"); out.write("<title>Insert title here</title>\r\n"); out.write("</head>\r\n"); out.write("\r\n"); out.write("<body>\r\n"); out.write("\t<!-- html注释 -->\r\n"); out.write("\t"); out.write("\r\n"); out.write(" "); int i = 0; System.out.println(i); //java单行注释 out.write("\r\n"); out.write(" "); out.print( i ); out.write("\r\n"); out.write(" "); out.write("\r\n"); out.write(" "); out.print(str ); out.write("\r\n"); out.write("</body>\r\n"); out.write("</html>"); } catch (java.lang.Throwable t) { if (!(t instanceof javax.servlet.jsp.SkipPageException)){ out = _jspx_out; if (out != null && out.getBufferSize() != 0) try { 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); } } }
以及页面源代码:
(3)jsp运行原理
第一次访问index.jsp --> index_jsp.java [Tomcat下的work目录下] --> 编译运行;
当访问完index.jsp后如图:
(4)jsp三大指令
1)page指令:
language:jsp脚本可以嵌套的代码,但是只能是java;
pageEncoding:当前jsp脚本的编码
contentType:"text/html; charset=UTF-8"相当于响应的中文乱码问题解决;
session:"true","false";
是否使用session对象
import:导包;
一个一个的导入:
<%@ page import="java.sql.*" %>
<%@ page import="java.util.regex.*"%>
一块导入:
<%@ page import="java.sql.*,java.util.regex.*" %>中间用逗号隔开,后面不加逗号!
errorPage:当前页面出错跳转到其他页面;
isErrorPage:当前页面是一个处理错误的页面;
2)include指令:
静态:<%@ include file="被包含的文件地址"%>;
<%@ 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>
<h1>这是头部!</h1>
</body>
</html>
<%@ 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> <%@ include file="header.jsp"%> <h3>这是内容!</h3> <%@ include file="footer.jsp"%> </body> </html>
<%@ 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>
<h1>这是尾部!</h1>
</body>
</html>
效果如下:
动态:<jsp:include page="被包含的页面"/>;
<%@ 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>
<h1>这是include1</h1>
</body>
</html>
<%@ 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>
<jsp:include page="/include.jsp"></jsp:include>
<h2>这是include的h2标签</h2>
</body>
</html>
效果如下:
静态包含:被包含文件不编译,直接以Html进入文件;动态包含:被包含文件编译,指向被包含文件地址,进行编译;
3)taglib指令;
(5)九大内置对象:out、request、response、config、session、application、page、pagecontext、exception、
(6)out对象:out缓冲区的内容添加到response缓冲区、默认为buffer="8kb",设置"0kb"时直接进入response缓冲区;
最后注意打印顺序:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" buffer="8kb"%>
<!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>
<%
out.write("bbbbbbbbbbbbbbb");
response.getWriter().write("ccccccccccccccccc");
%>
<%="gggggggggggggggg" %>
</body>
</html>
具体表现如下:
原因如下:
1、首先介绍write()和print()方法的区别:
(1)、write():仅支持输出字符类型数据,字符、字符数组、字符串等
(2)、print():可以将各种类型(包括Object)的数据通过默认编码转换成bytes字节形式,这些字节都通过write(int c)方法被输出
2、介绍response.getWriter()和out的区别:
(1)、out和response.getWriter的类不一样,一个是JspWriter,另一个是java.io.PrintWriter。
(2)、执行原理不同:JspWriter相当于一个带缓存功能的printWriter,它不是直接将数据输出到页面,而是将数据刷新到response的缓冲区后再输出,
response.getWriter直接输出数据(response.print()),所以(out.print)只能在其后输出。
(3)、out为jsp的内置对象,刷新jsp页面,自动初始化获得out对象,所以使用out对象是需要刷新页面的,
而response.getWriter()响应信息通过out对象输出到网页上,当响应结束时它自动被关闭,与jsp页面无关,无需刷新页面
形象的比喻:当我们调用response.getWriter()这个对象同时获得了网页的画笔,这时你就可以通过这个画笔在网页上画任何你想要显示的东西。
(4)、out的print()方法和println()方法在缓冲区溢出并且没有自动刷新时候会产生ioexception,
而response.getWrite()方法的print和println中都是抑制ioexception异常的,不会有ioexception
(7)pageContext对象:pageContext可以向指定的其他域中存取数据;
findAttribute(String name)依次从pageContext域,request域,session域,application域中获取属性,
在某个域中获取后将不在向后寻找
pageContext.getRequest();可以获得其他8大隐式对象
<%@ 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> <% pageContext.setAttribute("name", "zhangsan"); %> <!-- 向request域中设置值 --> <% pageContext.setAttribute("name", "lisi",pageContext.REQUEST_SCOPE); %> <% pageContext.setAttribute("name", "wangwu",pageContext.APPLICATION_SCOPE); %> <% pageContext.setAttribute("name", "xiaohongmao",pageContext.SESSION_SCOPE); %> <%= pageContext.getAttribute("name") %> <%= pageContext.getAttribute("name",pageContext.REQUEST_SCOPE) %> <%= pageContext.getAttribute("name",pageContext.SESSION_SCOPE) %> </body> </html>
(8)jsp标签(动作):
1)页面包含(动态包含):<jsp:include page="被包含的页面"/>
2)请求转发:<jsp:forward page="要转发的资源" />
标签:编译 his 打印 调用 port acl new 包含 导入
原文地址:https://www.cnblogs.com/scw123/p/9968519.html