Java学习日记5:jsp

    xiaoxiao2021-03-25  71

    在网络应用的开发中,我们一般用的都是MVC模式。 MVC模式,百度上的解释是:

    MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范,用一种业务逻辑、数据、界面显示分离的方法组织代码,将业务逻辑聚集到一个部件里面,在改进和个性化定制界面及用户交互的同时,不需要重新编写业务逻辑。MVC被独特的发展起来用于映射传统的输入、处理和输出功能在一个逻辑的图形化用户界面的结构中。

    一般情况下:

    Model—Java代码Controller—Servlet View—jsp

    jsp

    jsp是一种写动态网页的技术,简单地说就是可以吧Java代码写到HTML中,大大简化了很多流程。 因为Servlet很强大,不过不善于显示网页的内容,所以出现了jsp。

    Servlet的长处是“流程控制 和处理事务”JSP的长处是 “生成和显示动态网页”

    如果用Servlet显示一个页面:

    out.println("<html>"); out.println("<head><title>demo1</title></head>"); out.println("<body>"); out.println(" Hello World <br>"); out.println("大家好"); out.println("</body>"); out.println("</html>");

    如果用jsp显示一个页面:

    <html> <head><title>demo1</title></head> <body> <% out.println(" Hello World "); %> <br> <% out.println("大家好"); %> </body> </html> 一个JSP文件被转换成servlet的源代码,然后编译通过容器自动执行部署后,首次访问JSP页面不同容器,JSP源代码驻留的地方也不同

    jsp脚本元素有三种类型:

    表达式: <%=Expressions%> Scriplet: <%Code%> 声明: <%! Declarations %>

    表达式:

    <html><head><title>Expression Sample</title></head> <body><h2> Current time: <%=new java.util.Date()%><br/> Random number: <%=Math.random()%><br/> Simple string: <%=“Hello, world”%><br/> Simple statement: 1 + 1 = <%=1 + 1%> <br/> Visit implicit object: remote host is <%=request.getRemoteHost()%> </h2></body> </html>

    转换成Java代码:

    public void _jspService(HttpServletRequest request, HttpServletResponse response) throws java.io.IOException, ServletException { … out.write(" Current time: "); out.print(new java.util.Date()); out.write("<br/>\r\n"); out.write(" Random number: "); out.print(Math.random()); out.write("<br/>\r\n"); out.write(" Simple string: "); out.print("Hello, world"); out.write("<br/>\r\n"); out.write(" Simple statement: 1 + 1 = "); out.print(1 + 1); out.write(" <br/>\r\n"); out.write(" Visit implicit object: remote host is "); out.print(request.getRemoteHost()); out.write("\r\n"); }

    Scriplet:

    <html><head><title>Expression Sample</title></head> <% 这里写Java 代码; %> //这就是Scriptlet,这中间就等于Java代码 </html>

    记得写Java代码时带分号。

    public void _jspService(HttpServletRequest request, HttpServletResponse response) throws java.io.IOException, ServletException { 这里写Java 代码; }

    声明 :

    声明可在servlet类的主体里定义变量和方法相当于在Servlet中的Service() 方法外面不可声明隐式对象声明可重写 jspInit() 和 jspDestroy()方法来初始化和结束JSP页面 <html><head><title>Expression Sample</title></head> <%! 方法或变量声明代码;%> </html>

    在Java中:

    public final class decSample_jsp extends org.apache.jasper.runtime.HttpJspBase implements org.apache.jasper.runtime.JspSourceDependent { 方法或变量声明代码; } public void _jspService(HttpServletRequest request, HttpServletResponse response) throws java.io.IOException, ServletException { ... } }

    指令:

    指令是发送给JSP编译器的信息告诉JSP编译器如何处理JSP页面不直接生成输出

    e.g.

    <%@page import=“java.util.*”%>

    page: 导入页面所依赖的属性和把这些传递给JSP编译器

    <%@page session="false" buffer="4kb" autoFlush="false" contentType="text/html;charset=gb2312" import="java.util.*, java.net.*" errorPage="error.jsp“ %>

    include: JSP页面翻译时用来包含文本或代码 相当于在用include语句的地方引用include语句指向的页面中的语句。

    <%@include file=“banner.html”%> <jsp:include page="banner.html"/> <jsp:include page="date.jsp"> <jsp:param name="user" value="George"/> </jsp:include>

    taglib: 描述了JSP编译器所使用的标签库

    <%@taglib uri=“http://java.sun.com/jsp/jstl/core” prefix=“c”%>

    JSP中有两种注释: JSP页面功能文档的注释. 以下是这种类型注释的语法:

    <%-- this is a comment ... --%>

    发送给用户响应的注释。以下是这种类型注释的语法:

    <!-- comments ... -->

    如果有错误请多指正~

    转载请注明原文地址: https://ju.6miu.com/read-35798.html

    最新回复(0)