java web-03-jsp

1、什么是JSP

java servlet pages:java服务器端页面,也和servlet一样,用于动态web技术;

2、JSP基础语法

JSP表达式:

<%--  
    jsp表达式
    作用:用来将程序的输出,输出到客户端
    <%= 变量或者表达式%>
--%>
<%= new java.util.Date()%>

JSP脚本片段:

<%
int sum=0;
for (int i = 0; i <= 100; i++) {
    sum = sum + i;
}
out.println(sum);
%>

脚本片段的再实现:

<%
  int x=1;
  out.println(x);
%>
<p>这是一个jsp文档</p>
<%
  out.println(x);
%>

<%--  在代码嵌入html元素--%>
<%
  for (int i = 0; i < 5; i++) {
%>
<h1>hello world</h1>
<%
  }
%>

JSP声明:

<%!
  static {
    System.out.println("Loading Servlet!");
  }
  private int globalVar = 0;
  public void hello(){
    System.out.println("进入了方法hello");
  }
%>
<%
  hello();
%>

JSP声明:会被编译到SP生成java的类中!其他的,就会生成到_jspService方法中!

在JSP中,嵌入java代码即可!

JSP注释,不会在客户端显示,HTML就会!

3、JSP指令

<head>
    <title>header</title>
</head>
<body>
<h1>这是header</h1>
</body>
<head>
    <title>footer</title>
</head>
<body>
<h1>这是footer</h1>
</body>
<head>
    <title>jsp指令</title>
</head>
<body>
<%-- include会将两个页面合二为一  --%>
<%@ include file="common/header.jsp"%>
<h1>网页主体</h1>
<%@ include file="common/footer.jsp"%>

<hr>

<%--二--%>
<%-- jsp:include:拼接页面,本质还是三个--%>
<jsp:include page="/common/header.jsp"></jsp:include>
<h1>网页主体</h1>
<jsp:include page="/common/footer.jsp"></jsp:include>
</body>

错误页面跳转.jsp

<head>
    <title>404</title>
</head>
<body>
<h1>404</h1>
<img src="../image/404.jpg">
</body>
<head>
    <title>500</title>
</head>
<body>
<h1>500</h1>
<img src="../image/500.jpg">
</body>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%--如果代码出现错误,则跳转到500页面--%>
<%@ page errorPage="error/500.jsp" %>
<%--指定这个页面是个错误页面--%>
<%--<%@ page isErrorPage="true" %>--%>
<%--如果搜索的路径(文件)不存在,则跳转到404页面--%>
<html>
<head>
    <title>错误页面跳转</title>
</head>
<body>
<% int x = 1/0; %>
</body>
</html>

web.xml

<error-page>
    <error-code>404</error-code>
    <location>/error/404.jsp</location>
</error-page>

4、9大内置对象

存东西

<%--内置对象--%>
<%
    pageContext.setAttribute("name1","1号");//只在一个页面中有效
    request.setAttribute("name2","2号");//只在一次请求中有效
    session.setAttribute("name3","3号");//只在一次会话中有效
    application.setAttribute("name4","4号");//只在服务器中有效
%>

<%
    //从底层到高层(作用域):page--request--session--application
    String name1 = (String) pageContext.findAttribute("name1");
    String name2 = (String) pageContext.findAttribute("name2");
    String name3 = (String) pageContext.findAttribute("name3");
    String name4 = (String) pageContext.findAttribute("name4");
    String name5 = (String) pageContext.findAttribute("name5");

%>

<%--使用ER表达式输出  ${}--%>
<h1>取出的值为</h1>
<h3>${name1}</h3>
<h3>${name2}</h3>
<h3>${name3}</h3>
<h3>${name4}</h3>
<h3><%=name5%></h3>

实现页面跳转:

<head>
    <title>pageContextDemo02</title>
</head>
<body>
<%
    pageContext.forward("/index.jsp");
    //request.getRequestDispatcher("/index.jsp").forward(request,response);
%>
</body>

获取pageContextDemo01中的数据

<body>
<%
    //从pageContext取出,我们通过寻找你的方式来输出
    String name1 = (String) pageContext.findAttribute("name1");
    String name2 = (String) pageContext.findAttribute("name2");
    String name3 = (String) pageContext.findAttribute("name3");
    String name4 = (String) pageContext.findAttribute("name4");
    String name5 = (String) pageContext.findAttribute("name5");
%>

<%--使用ER表达式输出  ${}--%>
<h1>取出的值为</h1>
<h3>${name1}</h3>
<h3>${name2}</h3>
<h3>${name3}</h3>
<h3>${name4}</h3>
<h3><%=name5%></h3>
</body>

5、JSP标签、JSTL标签、EL表达式

导包

JSP标签:

<head>
    <title>jspTag01</title>
</head>
<body>
<jsp:forward page="/jspTag02.jsp">
    <jsp:param name="name" value="张三"/>
    <jsp:param name="age" value="20"/>
</jsp:forward>
</body>
<head>
    <title>jspTag02</title>
</head>
<body>
名字:<%=request.getParameter("name")%>
年龄:<%=request.getParameter("age")%>
</body>

JSTL表达式:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>coreif</title>
</head>
<body>
<form action="coreif.jsp" method="get">
    <%--
    EL表达式获取表中的数据
    ${param.参数名}
    --%>
    <input type="text" name="username" value="${param.username}">
    <input type="submit" value="登录">
</form>

<c:if test="${param.username=='admin'}" var="isAdmin">
    <c:out value="管理员欢迎你!"/>
</c:if>

<c:out value="${isAdmin}"/>
</body>
</html>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>corewhen</title>
</head>
<body>
<%--定义一个变量score--%>
<c:set var="score" value="99"></c:set>

<c:choose>
    <c:when test="${score>=90}">
        90
    </c:when>
    <c:when test="${score>=80}">
        80
    </c:when>
    <c:when test="${score>=70}">
        70
    </c:when>
    <c:when test="${score>=60}">
        60
    </c:when>
    <c:when test="${score<60}">
        不及格
    </c:when>
</c:choose>
</body>
</html>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page import="java.util.ArrayList" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>coreforeach</title>
</head>
<body>
<%
    ArrayList<String> people = new ArrayList<>();
    people.add(0,"一");
    people.add(1,"二");
    people.add(2,"三");
    people.add(3,"四");
    people.add(4,"五");
    request.setAttribute("list",people);
%>

<%--
var , 每一次遍历出来的变量
item , 要遍历的对象
begin , 哪里开始
end , 到哪里
step , 步长
--%>
<c:forEach var="people" items="${list}">
    <c:out value="${people}"></c:out>
</c:forEach>

<hr>

<c:forEach var="people" items="${list}" begin="1" end="3" step="2">
    <c:out value="${people}"></c:out><br>
</c:forEach>
</body>
</html>

EL表达式: ${ }

  • 获取数据
  • 执行运算
  • 获取web开发的常用对象

6、JavaBean

public class People {

    private int id;
    private String name;
    private int age;
    private String address;

}
<head>
    <title>javaBean</title>
</head>
<body>
<%
//    People people = new People();
//    等价于下面jsp:setProperty
//    people.setId(1);
%>

<jsp:useBean id="people" class="com.feng.pojo.People" scope="page"/>
<%--setProperty:设定属性--%>
<jsp:setProperty name="people" property="id" value="1"/>
<jsp:setProperty name="people" property="name" value="tom"/>
<jsp:setProperty name="people" property="age" value="20"/>
<jsp:setProperty name="people" property="address" value="Shanghai"/>

<%--等价于jsp:getProperty--%>
<%--<%=people.getId()%>--%>

id:<jsp:getProperty name="people" property="id"/>
姓名:<jsp:getProperty name="people" property="name"/>
年龄:<jsp:getProperty name="people" property="age"/>
地址:<jsp:getProperty name="people" property="address"/>
</body>
posted @ 2021-10-11 20:34  比特风  阅读(25)  评论(0)    收藏  举报