一文学会JSP
构建Web应用
这里使用IDEA构建Web应用
新建项目
选择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>
启动应用
启动相关的应用
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>
    在无知的道路上缓步前行
 
                    
                     
                    
                 
                    
                









 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号