一文学会JSP

8次阅读

共计 3459 个字符,预计需要花费 9 分钟才能阅读完成。

构建 Web 应用

这里使用 IDEA 构建 Web 应用

新建项目


添加新的 Tomcat

勾选上正确的 Tomcat

选择 Filsh

创建好目录如下

其自动生成的 Web.XML 文件如下

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
</web-app>

同时还生成了一个 jsp 文件,生成的 jsp 文件如下

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2020/7/5
  Time: 22:39
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
  $END$
  </body>
</html>

配置应用首页

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

启动应用

启动相关的应用


这样就完成了最基本的 tomcat 的部署

jsp 的基本语法

jsp 的注释

jsp 的基本注释如下

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2020/7/5
  Time: 22:39
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
  <%-- 注释内容 --%>
  $END$
  </body>
</html>

jsp 声明

对 jsp 的声明如下

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2020/7/5
  Time: 22:39
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
  <%!
    // 声明一个整形变量
    public int count;
  // 声明一个方法
    public String info(){return "hello";}
  %>
  $END$

  <%
    // 把 count 值输出后加 1
    out.println(count++);
  %>

  <%
    // 输出 info()方法后的返回值
    out.println(info());
  %>
  </body>
</html>

访问的页面结果如下

jsp 输出表达式

jsp 提供了一种简单的输出表达式

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2020/7/5
  Time: 22:39
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
  <%!
    // 声明一个整形变量
    public int count;
  // 声明一个方法
    public String info(){return "hello";}
  %>
  $END$
`<%=count++%>
  <%=info()%>
  </body>
</html>

jsp 三个编译指令

这里对 jsp 有三个编译的指令

page 指令

page 指令位于 jsp 页面的顶端,一个 jsp 页面可以有多个 page 指令,page 指令的语法为

<%@ page import="java.sql.*" %>

include 指令

include 指令可以将一个外部文件嵌入到当前 jsp 文件中,同时解析这个页面中的 jsp 语句。include 命令既可以包含 jsp 页面也可以包含静态文本。编译指令语法如下:

<%@ include file="要导入的 jsp 页面或文本文件" %>

taglib 指令

taglib 指令用于引入一些特定的标签库, 语法格式:

<%@ taglib prefix="tagPrefix" uri="tagLibraryURI" %>

如使用 struts 标签库:

<%@ taglib prefix="s" taglib="/struts-tags" %>

动作指令

forward

进行页面跳转的指令
如果转发的时候需要传递参数可以使用 <jsp:param></jsp:param> 指令进行设置。
比如,访问 index.jsp 页面时自动转发至 login.jsp,需要把 username 和 password 传递过去:
index.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<jsp:forward page="login.jsp">
    <jsp:param value="yaopan" name="username" />
    <jsp:param value="123456" name="password" />
</jsp:forward>
<%--mac 上按住 comment 键(windows 下按住 ctrl 键),再点击 login.jsp  forword 以下的代码不会被执行 --%>

在 login.jsp 中可以使用 getParameter 方法获取传入的参数值:

<%
  String name=request.getParameter("username");
  String pwd=request.getParameter("password");
  out.println(name);
  out.println("<br>");
  out.println(pwd);
%>

执行 forword 指令时用户请求的地址没有发生变化,页面内容被 forward 目标替代。

include 指令

include 指令用于包含某个页面,但不会导入被 include 页面的编译指令。可以通过 param 指令传递参数:
新建一个 index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<jsp:include page="head.html"></jsp:include>
<jsp:include page="body.jsp">
   <jsp:param value="#1d99f6" name="bgcolor"/>
</jsp:include>
</html>

body.jsp

<body bgcolor="<%=request.getParameter("bgcolor")%>">
</body>

正文完
 0