javaweb----jsp開發三

JSP指令

<%--
JSP指令只有三個
  page、include、taglib (標籤)

公式:
  <%@ 指定名 屬性名=屬性值;[屬性名=屬性值;屬性名=屬性值...]  %>

page指令
  autoFlush:自動刷新 (true,false:默認)
  contentType:頁面文本類型 "text/html"
  errorPage: 如果存在錯誤頁面,就跳轉至指定的頁面 【不推薦使用,推薦在XML中配置】
  language:JSP中使用的語言,默認是java
  pageEncoding:頁面編碼
  import:因爲jsp本質上就是一個servlet,所以需要導入相關jar包才能使用。

include指令
  導入其他頁面包含到本頁,網站中一般有一些公用的位置,我們可以提取出來,比如網站的頭部,和尾部
  file屬性:【要導入頁面,一般都是不完整的網頁,只包含部分】

taglib:標籤庫
  在jsp中有大量的java代碼十分痛苦,所以開發中我們可以使用一些現成的標籤庫,就相當於使用一些替代java代碼的標籤語言
  out.print()----->  <c:out>  : 這個c就是一個別人定義好的標籤庫,像這樣的庫有非常多,我們甚至可以自己定義;
  我們之後學習的JSTL標籤就是這裏的標籤庫

--%>

include指定:包含頁面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<div>
    <h1>我是footer</h1>
</div>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<div>
    <h1>我是header</h1>
</div>
<body>
  <%--使用include指定加載網頁其他部分--%>
  <%@include file="common/header.jsp"%>
  <h1>我是index頁面</h1>
  <%@include file="common/footer.jsp"%>
</body>

errorPage:錯誤頁面

<?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">

    <!--如果網站中出現了404,就跳轉到指定的頁面-->
    <error-page>
        <error-code>404</error-code>
        <location>/error/404.jsp</location>
    </error-page>
    <error-page>
        <error-code>500</error-code>
        <location>/error/500.jsp</location>
    </error-page>
    <error-page>
        <exception-type>java.lang.NullPointerException</exception-type>
        <location>/error/exception.jsp</location>
    </error-page>

</web-app>

JSP標籤

作用:爲了避免在JSP中有大量的Java代碼,造成頁面不好維護;

<%--
JSP標籤
語法:<jsp:XXX  >

jsp:include:
    page屬性:要包含的頁面地址

    jsp:include 和 @include 區別
        @include 靜態包含 : 他會把包含進來的頁面和自己的頁面融合成爲一個servlet
        jsp:include 動態包含 :他不會把包含進來的頁面和自己的頁面融合成爲一個servlet 【推薦使用,容錯率更高】

jsp:forward
    page屬性:要轉發到的頁面,url不變,
        本質就是request的請求轉發,可以攜帶參數;可以通過request取出來這個攜帶的參數
    一般配合<jsp:param>使用

jsp:param
    一般配合<jsp:forward>使用,攜帶參數
--%>

<%--
<jsp:include page="common/header.jsp"/>
<h1>我是tag頁面</h1>
<jsp:include page="common/footer.jsp"/>


<jsp:forward page="index.jsp"/>

<jsp:forward page="index.jsp">
    <jsp:param name="username" value="kuangshen"/>
    <jsp:param name="age" value="18"/>
</jsp:forward>

index頁面的取出代碼

  名字:<%=request.getParameter("username")%>
  年齡:<%=request.getParameter("age")%>

--%>

JavaBean

實體類:pojo,entity,beans…

屬性私有,get/set ,有參,無參構造。爲了方便程序調試 toString();

實體類

package com.wu.pojo;


public class Student {

    //面向對象編程三大特性:封裝,繼承,多態。
    //封裝: 屬性私有,不讓用戶瞎操作,於是我們設置操作屬性的公開方法,我們會在公開的方法中定義安全性代碼;

    private String name;
    private int id;
    private int age;
    private boolean isGraduate;//是否畢業

    public Student() {
    }

    public Student(String name, int id, int age, boolean isGraduate) {
        this.name = name;
        this.id = id;
        this.age = age;
        this.isGraduate = isGraduate;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        if (name.equals("習近平")){
            this.name = "dog";
            return;
        }
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
//        if (id<0){
//            this.id = 學員數據庫學生數量+1;
//        }
        this.id = id;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        if (age<0 || age>150){
            this.age = 0;
            return;
        }
        this.age = age;
    }

    public boolean isGraduate() {
        return isGraduate;
    }

    public void setGraduate(boolean graduate) {
        isGraduate = graduate;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", id=" + id +
                ", age=" + age +
                ", isGraduate=" + isGraduate +
                '}';
    }
}

jsp頁面

<%@ page import="com.wu.pojo.Student" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

<%--如何通過jsp使用自定義的對象--%>

<%--
    Student student = new Student();
%>--

<%--
jsp:useBean標籤
    創建一個類的實例化對象;
    java代碼:Student student = new Student();

    屬性:
        id:實例化對象的名稱,對象名
        class:要實例化的類
        scope:對象的作用域,默認是page,千萬不要放在application中;


--%>

<jsp:useBean id="student" class="com.wu.pojo.Student" scope="page"/>

<%--jsp標籤--%>
<%--setProperty默認調用的是實體類中的set方法--%>
<jsp:setProperty name="student" property="id" value="2"/>
<jsp:setProperty name="student" property="age" value="18"/>
<jsp:setProperty name="student" property="name" value="wuwu"/>
<jsp:setProperty name="student" property="graduate" value="false"/>


<%--普通方法
<%
    student.setAge(999);
    student.setName("wu");
    student.setId(001);
%>
--%>

<%=student.getAge()%>
<%=student.getName()%>
<%=student.getId()%>
<%=student.isGraduate()%>

<hr>

${student.age}
${student.name}
${student.id}
${student.graduate}

<hr>

<%--getProperty默認調用的是實體類中的get方法--%>
<jsp:getProperty name="student" property="age"/>
<jsp:getProperty name="student" property="name"/>
<jsp:getProperty name="student" property="id"/>


<%--
Spring
    IOC: 控制反轉
        DI:依賴注入
    AOP: 面向切面編程
--%>

</body>
</html>

JSTL標籤庫

和jsp標籤一樣,爲了解決jsp中嵌入java代碼看着不舒服;

注意點:

  1. 項目要導入對應的jstl標籤庫的jar包。

  2. Tomcat中也需要放置對應的jar包

  3. 代碼中,有些特殊符號需要使用轉義字符;

    <%@ page import=“com.wu.pojo.Student” %>
    <%@ page contentType=“text/html;charset=UTF-8” language=“java” %>

         <%--
         使用jstl或者其他標籤庫需要先導入,需要下載對應的jar包-jstl-1.2;
         prefix:需要使用的標籤庫名
         tagdir:標籤庫的本地目錄
         uri : 對應庫的網絡地址
         --%>
     <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
     
     <html>
     <head>
         <title>Title</title>
     </head>
     <body>
     
     
     <%--
     <c:out  輸出
     <c:out value="西部開源"/>
     --%>
     <c:out value="西部開源"/>
     
     <%--
     <c:if
         test: 需要判斷的條件 ,等價於 if(exp)中的exp
     
     if(){
     }
     --%>
     
     <%
         request.getSession().setAttribute("name","qinjiang");
         Student student = new Student();
         student.setName("qinjiang");
     %>
     
     <%=student.getName().equals("qinjiang")%>
     ${sessionScope.name=='qinjiang'}
     
     <c:if test="<%=student.getName().equals(\"qinjiang\")%>">
        <c:out value="admin"/>
     </c:if>
     
     
     </body>
     </html>
    

EL表達式

同理,簡化Jsp的代碼難度;

${表達式}
${page、session、request、Application的變量}

過濾器

在這裏插入圖片描述
我們主要用它來實現過濾亂碼問題!

過濾器一般就用來過濾一些違規詞語,解決亂碼問題;主要還是爲了解耦,職責統一

實現步驟

  1. 寫一個類實現Filter接口,必定會重寫三個方法

    • 初始化
    • doFilter(req,resp,doFilterChain)
      • doFilterChain(req,resp)
    • 註銷
  2. 配置web.xml

    <filter>
       <filter-name>myFilter</filter-name>
       <filter-class>com.wu.filter.MyFilter</filter-class>
     </filter>
     
     <!--這裏url代表需要過濾的請求路徑
     /*  所有的請求都會被過濾
     /regist.do  只有這個請求會被過濾
     過濾的註銷和初始化時隨着Tomcat容器一起啓動或者關閉
     -->
     
     <filter-mapping>
       <filter-name>myFilter</filter-name>
       <url-pattern>/*</url-pattern>
     </filter-mapping>
    

測試代碼

package com.wu.filter;

import javax.servlet.*;
import java.io.IOException;

//filter過濾實現
//1.實現Filter接口   : javax.servlet.Filter;
//2.配置web.xml
public class MyFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("MyFilter初始化了");
    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain filterChain) throws IOException, ServletException {
        //主要過濾方法編寫在doFilter中
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");

        if (req.getParameter("username")!=null){
            System.out.println(req.getParameter("username"));
            if (req.getParameter("username").equals("不能隨意出現的名字")){
                resp.getWriter().println("?????????????????????????????");
            }
        }

        System.out.println("執行方法filterChain.doFilter(req,resp);前===========");
        filterChain.doFilter(req,resp); //這行代碼代表過濾器放行
        System.out.println("執行方法filterChain.doFilter(req,resp);後===========");
    }

    @Override
    public void destroy() {
        System.out.println("MyFilter註銷了");
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章