Java学习日记5:jsp

本文介绍了MVC模式的基本概念及其在网络应用开发中的应用,同时详细解析了JSP技术的特点,包括其与Servlet的区别、如何使用JSP生成动态网页以及JSP文件的组成部分如表达式、Scriplet和声明等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在网络应用的开发中,我们一般用的都是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 ... --> 

如果有错误请多指正~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值