Java for Web学习笔记(十一):JSP(1)何为JSP

使用JSP

通java代码来编写HTML很是繁琐,通过使用JSTL(JSP标准标签库,JavaServer Pages™ Standard Tag Library),通过JSP来更便携的实现。对JSP的支持Maven中包括javax.servlet.jsp-api、javax.servlet.jsp.jstl-api。Jstl在2011年12月发布1.2.1后就没有再更新,如需要更全面的功能,引入org.glassfish.web提供的jar包。Maven的pom.xml代码片段如下:

<dependency>
    <groupId>javax.servlet.jsp</groupId>  
    <artifactId>javax.servlet.jsp-api</artifactId>  
    <version>2.3.2-b02</version>  
    <scope>provided</scope>  
</dependency>
    
<dependency>
    <groupId>javax.servlet.jsp.jstl</groupId>  
    <artifactId>javax.servlet.jsp.jstl-api</artifactId>  
    <version>1.2.1</version>  
    <scope>compile</scope>  
</dependency>

<dependency>
    <groupId>org.glassfish.web</groupId>  
    <artifactId>javax.servlet.jsp.jstl</artifactId>  
    <version>1.2.2</version>
    <scope>compile</scope>
    <exclusions>
        <exclusion>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
        </exclusion>
        <exclusion>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
        </exclusion>
        <exclusion>
            <groupId>javax.servlet.jsp.jstl</groupId>
            <artifactId>jstl-api</artifactId>
        </exclusion>
    </exclusions>             
</dependency>        

什么是JSP

下面是一个JSP的例子:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
	<title>Chapter 04 Application</title>
</head>
<body>
	<h2>Hello World!</h2>
</body>
</html>

JSP提供了脚本和java混合方式。例子是index.jsp。第一行的相关语法后面学习。其中contentType="text/html;charset=UTF-8"设置了HTTP应答中的header:Content-Type,即response.setContentType("text/html;charset=UTF-8");

JSP是“syntactic sugar”(英国计算机科学家彼得·约翰·兰达(Peter J. Landin)发明的一个术语。指的是,在计算机语言中添加某种语法,这种语法能使程序员更方便的使用语言开发程序,同时增强程序代码的可读性,避免出错的机会;但是这种语法对语言的功能并没有影响。)JSP编译器将jsp转换为Java代码,每一个jsp作为一个servlet。如果在tomcat中部署,位于tomcat目录下work\Catalina\localhost\<your app>,如果使用Eclipse调测,则在缺省项目目录\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\work\Catalina\localhost\chapter04\org\apache\jsp,里面有index_jsp.java和index_jsp.class。

我们打开index_jsp.java,看到:

public final class index_jsp extends org.apache.jasper.runtime.HttpJspBase
    implements org.apache.jasper.runtime.JspSourceDependent,org.apache.jasper.runtime.JspSourceImports {
    … …
    public void _jspService(final javax.servlet.http.HttpServletRequest request,final javax.servlet.http.HttpServletResponse response)
        throws java.io.IOException, javax.servlet.ServletException {
        ……
    }
    … …
}

不同的web container翻译java code会有所不同,tomcat使用的是org.apache.jasper类,但是都会符合jsp的标准,虽然代码看起来不同,但是运行的结果是一样的。

Tomcat会在第一次访问jsp时进行翻译和编译,因此第一次运行可能会引发性能问题。有不少web container提供在部署时预编译的选项,这会增加部署时间,根据需求选择。


相关链接: 我的Professional Java for Web Applications相关文章

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值