JSP小练习新闻管理系统

(2) 2024-04-19 09:23

Hi,大家好,我是编程小6,很荣幸遇见你,我把这些年在开发过程中遇到的问题或想法写出来,今天说一说JSP小练习新闻管理系统,希望能够帮助你!!!。

JSP小练习新闻管理系统

该练习为帮助大家梳理编程思路 和 增删改查 练习其中不符合业务逻辑部分请忽略
源码地址:https://download.csdn.net/download/qq_24330181/85705794
配套视频讲解:https://www.bilibili.com/video/BV1cY4y137hA

项目演示:

JSP小练习新闻管理系统_https://bianchenghao6.com/blog__第1张

JSP小练习新闻管理系统_https://bianchenghao6.com/blog__第2张

JSP小练习新闻管理系统_https://bianchenghao6.com/blog__第3张

JSP小练习新闻管理系统_https://bianchenghao6.com/blog__第4张

JSP小练习新闻管理系统_https://bianchenghao6.com/blog__第5张
JSP小练习新闻管理系统_https://bianchenghao6.com/blog__第6张

JSP小练习新闻管理系统_https://bianchenghao6.com/blog__第7张

JSP小练习新闻管理系统_https://bianchenghao6.com/blog__第8张

JSP小练习新闻管理系统_https://bianchenghao6.com/blog__第9张

JSP小练习新闻管理系统_https://bianchenghao6.com/blog__第8张

JSP小练习新闻管理系统_https://bianchenghao6.com/blog__第11张

JSP小练习新闻管理系统_https://bianchenghao6.com/blog__第12张

JSP小练习新闻管理系统_https://bianchenghao6.com/blog__第6张

新建项目:

JSP小练习新闻管理系统_https://bianchenghao6.com/blog__第14张

JSP小练习新闻管理系统_https://bianchenghao6.com/blog__第15张

JSP小练习新闻管理系统_https://bianchenghao6.com/blog__第16张

配置pom.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.lihaozhe</groupId>
    <artifactId>news</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>news</name>
    <packaging>war</packaging>

    <properties>
        <jdk.version>17</jdk.version>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <maven.compiler.compilerVersion>17</maven.compiler.compilerVersion>
        <maven.compiler.encoding>utf-8</maven.compiler.encoding>
        <project.build.sourceEncoding>utf-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <maven.test.failure.ignore>true</maven.test.failure.ignore>
        <maven.test.skip>true</maven.test.skip>
        <junit.version>5.8.2</junit.version>

        <fastjson.version>2.0.6</fastjson.version>
        <gson.version>2.9.0</gson.version>
        <hutool.version>5.8.0.M3</hutool.version>
        <jackson.version>2.13.3</jackson.version>
        <lombok.version>1.18.24</lombok.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>jakarta.servlet</groupId>
            <artifactId>jakarta.servlet-api</artifactId>
            <version>5.0.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <!--commons-->
        <!--commons-lang3-->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.8.1</version>
        </dependency>
        <!--commons-io-->
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.11.0</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>${hutool.version}</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok.version}</version>
            <scope>provided</scope>
        </dependency>
        <!--json-->
        <!--fastjson-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>${fastjson.version}</version>
        </dependency>
        <!--gson-->
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>${gson.version}</version>
        </dependency>
        <!--jackson-->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.datatype</groupId>
            <artifactId>jackson-datatype-jsr310</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        <!--webjar-->
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>jquery</artifactId>
            <version>3.6.0</version>
        </dependency>
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>bootstrap</artifactId>
            <version>5.1.3</version>
        </dependency>
        <!--el表达式-->
        <dependency>
            <groupId>jakarta.servlet.jsp.jstl</groupId>
            <artifactId>jakarta.servlet.jsp.jstl-api</artifactId>
            <version>2.0.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.glassfish.web</groupId>
            <artifactId>jakarta.servlet.jsp.jstl</artifactId>
            <version>2.0.0</version>
            <scope>provided</scope>
        </dependency>

    </dependencies>

    <build>
        <!--项目打包文件名-->
        <!--<finalName>news</finalName>-->
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.3.2</version>
            </plugin>
            <!-- 编译级别 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <!-- 设置编译字符编码 -->
                    <encoding>UTF-8</encoding>
                    <!-- 设置编译jdk版本 -->
                    <source>${jdk.version}</source>
                    <target>${jdk.version}</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-clean-plugin</artifactId>
                <version>3.2.0</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.2.0</version>
            </plugin>
            <!-- 打包的时候跳过测试junit begin -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.2</version>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>
            <!-- tomcat7插件 -->
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <port>6633</port>
                    <path>/lhz</path>
                    <uriEncoding>UTF-8</uriEncoding>
                    <!-- tomcat热部署 -->
                    <username>admin</username>
                    <password>admin</password>
                    <url>http://192.168.95.114:8080/manager/text</url>
                </configuration>
            </plugin>
            <!-- jetty插件 -->
            <plugin>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>11.0.9</version>
                <configuration>
                    <webApp>
                        <contextPath>/lhz</contextPath>
                        <defaultsDescriptor>${basedir}/src/main/resources/webdefault.xml</defaultsDescriptor>
                    </webApp>
                    <httpConnector>
                        <port>6633</port>
                    </httpConnector>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

编辑webdefault.xml文件:

webdefault.xml文件存放在src/main/resources目录下

<?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_3_1.xsd" metadata-complete="false" version="3.1"> 

  <!-- ===================================================================== -->
  <!-- This file contains the default descriptor for web applications. -->
  <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
  <!-- The intent of this descriptor is to include jetty specific or common -->
  <!-- configuration for all webapps. If a context has a webdefault.xml -->
  <!-- descriptor, it is applied before the context's own web.xml file -->
  <!-- -->
  <!-- A context may be assigned a default descriptor by calling -->
  <!-- WebAppContext.setDefaultsDescriptor(String). -->
  <!-- -->
  <!-- This file is present in the jetty-webapp.jar, and is used as the -->
  <!-- defaults descriptor if no other is explicitly set on a context. -->
  <!-- -->
  <!-- A copy of this file is also placed into the $JETTY_HOME/etc dir of -->
  <!-- the distribution, and is referenced by some of the other xml files, -->
  <!-- eg the jetty-deploy.xml file. -->
  <!-- ===================================================================== -->

  <description>
    Default web.xml file.  
    This file is applied to a Web application before its own WEB_INF/web.xml file
  </description>

  <!-- ==================================================================== -->
  <!-- Removes static references to beans from javax.el.BeanELResolver to -->
  <!-- ensure webapp classloader can be released on undeploy -->
  <!-- ==================================================================== -->
  <listener>
   <listener-class>org.eclipse.jetty.servlet.listener.ELContextCleaner</listener-class>
  </listener>
  
  <!-- ==================================================================== -->
  <!-- Removes static cache of Methods from java.beans.Introspector to -->
  <!-- ensure webapp classloader can be released on undeploy -->
  <!-- ==================================================================== -->  
  <listener>
   <listener-class>org.eclipse.jetty.servlet.listener.IntrospectorCleaner</listener-class>
  </listener>
  

  <!-- ==================================================================== -->
  <!-- Context params to control Session Cookies -->
  <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
  <!-- UNCOMMENT TO ACTIVATE <context-param> <param-name>org.eclipse.jetty.servlet.SessionDomain</param-name> <param-value>127.0.0.1</param-value> </context-param> <context-param> <param-name>org.eclipse.jetty.servlet.SessionPath</param-name> <param-value>/</param-value> </context-param> <context-param> <param-name>org.eclipse.jetty.servlet.MaxAge</param-name> <param-value>-1</param-value> </context-param> -->

  <!-- ==================================================================== -->
  <!-- The default servlet. -->
  <!-- This servlet, normally mapped to /, provides the handling for static -->
  <!-- content, OPTIONS and TRACE methods for the context. -->
  <!-- The following initParameters are supported: -->
  <!-- * acceptRanges If true, range requests and responses are * supported * * dirAllowed If true, directory listings are returned if no * welcome file is found. Else 403 Forbidden. * * welcomeServlets If true, attempt to dispatch to welcome files * that are servlets, but only after no matching static * resources could be found. If false, then a welcome * file must exist on disk. If "exact", then exact * servlet matches are supported without an existing file. * Default is true. * * This must be false if you want directory listings, * but have login.jsp in your welcome file list. * * redirectWelcome If true, welcome files are redirected rather than * forwarded to. * * gzip If set to true, then static content will be served as * gzip content encoded if a matching resource is * found ending with ".gz" (default false) * (deprecated: use precompressed) * * precompressed If set to a comma separated list of encoding types (that may be * listed in a requests Accept-Encoding header) to file * extension mappings to look for and serve. For example: * "br=.br,gzip=.gz,bzip2=.bz". * If set to a boolean True, then a default set of compressed formats * will be used, otherwise no precompressed formats. * * resourceBase Set to replace the context resource base * * resourceCache If set, this is a context attribute name, which the servlet * will use to look for a shared ResourceCache instance. * * relativeResourceBase * Set with a pathname relative to the base of the * servlet context root. Useful for only serving static content out * of only specific subdirectories. * * pathInfoOnly If true, only the path info will be applied to the resourceBase * * stylesheet Set with the location of an optional stylesheet that will be used * to decorate the directory listing html. * * etags If True, weak etags will be generated and handled. * * maxCacheSize The maximum total size of the cache or 0 for no cache. * maxCachedFileSize The maximum size of a file to cache * maxCachedFiles The maximum number of files to cache * * useFileMappedBuffer * If set to true, it will use mapped file buffers to serve static content * when using an NIO connector. Setting this value to false means that * a direct buffer will be used instead of a mapped file buffer. * This file sets the value to true. * * cacheControl If set, all static content will have this value set as the cache-control * header. * * encodingHeaderCacheSize * Max entries in a cache of ACCEPT-ENCODING headers. * * otherGzipFileExtensions * defaults to .svgz but a comma separated list of gzip equivalent file extensions can be supplied * -->
 <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
  <servlet>
    <servlet-name>default</servlet-name>
    <servlet-class>org.eclipse.jetty.servlet.DefaultServlet</servlet-class>
    <init-param>
      <param-name>acceptRanges</param-name>
      <param-value>true</param-value>
    </init-param>
    <init-param>
      <param-name>dirAllowed</param-name>
      <param-value>true</param-value>
    </init-param>
    <init-param>
      <param-name>welcomeServlets</param-name>
      <param-value>false</param-value>
    </init-param>
    <init-param>
      <param-name>redirectWelcome</param-name>
      <param-value>false</param-value>
    </init-param>
    <init-param>
      <param-name>maxCacheSize</param-name>
      <param-value>256000000</param-value>
    </init-param>
    <init-param>
      <param-name>maxCachedFileSize</param-name>
      <param-value>200000000</param-value>
    </init-param>
    <init-param>
      <param-name>maxCachedFiles</param-name>
      <param-value>2048</param-value>
    </init-param>
    <init-param>
      <param-name>etags</param-name>
      <param-value>false</param-value>
    </init-param>
    <init-param>
      <param-name>useFileMappedBuffer</param-name>
      <param-value>false</param-value>
    </init-param>
    <load-on-startup>0</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>


  <!-- ==================================================================== -->
  <!-- JSP Servlet -->
  <!-- This is the jasper JSP servlet. -->
  <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
  <!-- The JSP page compiler and execution servlet, which is the mechanism -->
  <!-- used by the jsp container to support JSP pages. Traditionally, -->
  <!-- this servlet is mapped to URL pattern "*.jsp". This servlet -->
  <!-- supports the following initialization parameters (default values -->
  <!-- are in square brackets): -->
  <!-- -->
  <!-- checkInterval If development is false and reloading is true, -->
  <!-- background compiles are enabled. checkInterval -->
  <!-- is the time in seconds between checks to see -->
  <!-- if a JSP page needs to be recompiled. [300] -->
  <!-- -->
  <!-- compiler Which compiler Ant should use to compile JSP -->
  <!-- pages. See the Ant documentation for more -->
  <!-- information. [javac] -->
  <!-- -->
  <!-- classdebuginfo Should the class file be compiled with -->
  <!-- debugging information? [true] -->
  <!-- -->
  <!-- classpath What class path should I use while compiling -->
  <!-- generated servlets? [Created dynamically -->
  <!-- based on the current web application] -->
  <!-- Set to ? to make the container explicitly set -->
  <!-- this parameter. -->
  <!-- -->
  <!-- development Is Jasper used in development mode (will check -->
  <!-- for JSP modification on every access)? [true] -->
  <!-- -->
  <!-- enablePooling Determines whether tag handler pooling is -->
  <!-- enabled [true] -->
  <!-- -->
  <!-- fork Tell Ant to fork compiles of JSP pages so that -->
  <!-- a separate JVM is used for JSP page compiles -->
  <!-- from the one Tomcat is running in. [true] -->
  <!-- -->
  <!-- ieClassId The class-id value to be sent to Internet -->
  <!-- Explorer when using <jsp:plugin> tags. -->
  <!-- [clsid:8AD9C840-044E-11D1-B3E9-00805F499D93] -->
  <!-- -->
  <!-- javaEncoding Java file encoding to use for generating java -->
  <!-- source files. [UTF-8] -->
  <!-- -->
  <!-- keepgenerated Should we keep the generated Java source code -->
  <!-- for each page instead of deleting it? [true] -->
  <!-- -->
  <!-- logVerbosityLevel The level of detailed messages to be produced -->
  <!-- by this servlet. Increasing levels cause the -->
  <!-- generation of more messages. Valid values are -->
  <!-- FATAL, ERROR, WARNING, INFORMATION, and DEBUG. -->
  <!-- [WARNING] -->
  <!-- -->
  <!-- mappedfile Should we generate static content with one -->
  <!-- print statement per input line, to ease -->
  <!-- debugging? [false] -->
  <!-- -->
  <!-- -->
  <!-- reloading Should Jasper check for modified JSPs? [true] -->
  <!-- -->
  <!-- suppressSmap Should the generation of SMAP info for JSR45 -->
  <!-- debugging be suppressed? [false] -->
  <!-- -->
  <!-- dumpSmap Should the SMAP info for JSR45 debugging be -->
  <!-- dumped to a file? [false] -->
  <!-- False if suppressSmap is true -->
  <!-- -->
  <!-- scratchdir What scratch directory should we use when -->
  <!-- compiling JSP pages? [default work directory -->
  <!-- for the current web application] -->
  <!-- -->
  <!-- tagpoolMaxSize The maximum tag handler pool size [5] -->
  <!-- -->
  <!-- xpoweredBy Determines whether X-Powered-By response -->
  <!-- header is added by generated servlet [false] -->
  <!-- -->
  <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
  <servlet id="jsp">
    <servlet-name>jsp</servlet-name>
    <servlet-class>org.eclipse.jetty.jsp.JettyJspServlet</servlet-class>
    <init-param>
      <param-name>logVerbosityLevel</param-name>
      <param-value>DEBUG</param-value>
    </init-param>
    <init-param>
      <param-name>fork</param-name>
      <param-value>false</param-value>
    </init-param>
    <init-param>
      <param-name>xpoweredBy</param-name>
      <param-value>false</param-value>
    </init-param>
    <init-param>
      <param-name>compilerTargetVM</param-name>
      <param-value>1.7</param-value>
    </init-param>
    <init-param>
      <param-name>compilerSourceVM</param-name>
      <param-value>1.7</param-value>
    </init-param>
    <!-- <init-param> <param-name>classpath</param-name> <param-value>?</param-value> </init-param> -->
    <load-on-startup>0</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>jsp</servlet-name>
    <url-pattern>*.jsp</url-pattern>
    <url-pattern>*.jspf</url-pattern>
    <url-pattern>*.jspx</url-pattern>
    <url-pattern>*.xsp</url-pattern>
    <url-pattern>*.JSP</url-pattern>
    <url-pattern>*.JSPF</url-pattern>
    <url-pattern>*.JSPX</url-pattern>
    <url-pattern>*.XSP</url-pattern>
  </servlet-mapping>


  <!-- ==================================================================== -->
  <!-- Default session configuration -->
  <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
  <session-config>
    <session-timeout>30</session-timeout>
  </session-config>

  <!-- ==================================================================== -->
  <!-- Default MIME mappings -->
  <!-- The default MIME mappings are provided by the mime.properties -->
  <!-- resource in the jetty-http.jar file. Additional or modified -->
  <!-- mappings may be specified here -->
  <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
  <!-- UNCOMMENT TO ACTIVATE <mime-mapping> <extension>mysuffix</extension> <mime-type>mymime/type</mime-type> </mime-mapping> -->

  <!-- ==================================================================== -->
  <!-- Default welcome files -->
  <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

  <!-- ==================================================================== -->
  <!-- Default locale encodings -->
  <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
  <locale-encoding-mapping-list>
    <locale-encoding-mapping>
      <locale>ar</locale>
      <encoding>ISO-8859-6</encoding>
    </locale-encoding-mapping>
    <locale-encoding-mapping>
      <locale>be</locale>
      <encoding>ISO-8859-5</encoding>
    </locale-encoding-mapping>
    <locale-encoding-mapping>
      <locale>bg</locale>
      <encoding>ISO-8859-5</encoding>
    </locale-encoding-mapping>
    <locale-encoding-mapping>
      <locale>ca</locale>
      <encoding>ISO-8859-1</encoding>
    </locale-encoding-mapping>
    <locale-encoding-mapping>
      <locale>cs</locale>
      <encoding>ISO-8859-2</encoding>
    </locale-encoding-mapping>
    <locale-encoding-mapping>
      <locale>da</locale>
      <encoding>ISO-8859-1</encoding>
    </locale-encoding-mapping>
    <locale-encoding-mapping>
      <locale>de</locale>
      <encoding>ISO-8859-1</encoding>
    </locale-encoding-mapping>
    <locale-encoding-mapping>
      <locale>el</locale>
      <encoding>ISO-8859-7</encoding>
    </locale-encoding-mapping>
    <locale-encoding-mapping>
      <locale>en</locale>
      <encoding>ISO-8859-1</encoding>
    </locale-encoding-mapping>
    <locale-encoding-mapping>
      <locale>es</locale>
      <encoding>ISO-8859-1</encoding>
    </locale-encoding-mapping>
    <locale-encoding-mapping>
      <locale>et</locale>
      <encoding>ISO-8859-1</encoding>
    </locale-encoding-mapping>
    <locale-encoding-mapping>
      <locale>fi</locale>
      <encoding>ISO-8859-1</encoding>
    </locale-encoding-mapping>
    <locale-encoding-mapping>
      <locale>fr</locale>
      <encoding>ISO-8859-1</encoding>
    </locale-encoding-mapping>
    <locale-encoding-mapping>
      <locale>hr</locale>
      <encoding>ISO-8859-2</encoding>
    </locale-encoding-mapping>
    <locale-encoding-mapping>
      <locale>hu</locale>
      <encoding>ISO-8859-2</encoding>
    </locale-encoding-mapping>
    <locale-encoding-mapping>
      <locale>is</locale>
      <encoding>ISO-8859-1</encoding>
    </locale-encoding-mapping>
    <locale-encoding-mapping>
      <locale>it</locale>
      <encoding>ISO-8859-1</encoding>
    </locale-encoding-mapping>
    <locale-encoding-mapping>
      <locale>iw</locale>
      <encoding>ISO-8859-8</encoding>
    </locale-encoding-mapping>
    <locale-encoding-mapping>
      <locale>ja</locale>
      <encoding>Shift_JIS</encoding>
    </locale-encoding-mapping>
    <locale-encoding-mapping>
      <locale>ko</locale>
      <encoding>EUC-KR</encoding>
    </locale-encoding-mapping>
    <locale-encoding-mapping>
      <locale>lt</locale>
      <encoding>ISO-8859-2</encoding>
    </locale-encoding-mapping>
    <locale-encoding-mapping>
      <locale>lv</locale>
      <encoding>ISO-8859-2</encoding>
    </locale-encoding-mapping>
    <locale-encoding-mapping>
      <locale>mk</locale>
      <encoding>ISO-8859-5</encoding>
    </locale-encoding-mapping>
    <locale-encoding-mapping>
      <locale>nl</locale>
      <encoding>ISO-8859-1</encoding>
    </locale-encoding-mapping>
    <locale-encoding-mapping>
      <locale>no</locale>
      <encoding>ISO-8859-1</encoding>
    </locale-encoding-mapping>
    <locale-encoding-mapping>
      <locale>pl</locale>
      <encoding>ISO-8859-2</encoding>
    </locale-encoding-mapping>
    <locale-encoding-mapping>
      <locale>pt</locale>
      <encoding>ISO-8859-1</encoding>
    </locale-encoding-mapping>
    <locale-encoding-mapping>
      <locale>ro</locale>
      <encoding>ISO-8859-2</encoding>
    </locale-encoding-mapping>
    <locale-encoding-mapping>
      <locale>ru</locale>
      <encoding>ISO-8859-5</encoding>
    </locale-encoding-mapping>
    <locale-encoding-mapping>
      <locale>sh</locale>
      <encoding>ISO-8859-5</encoding>
    </locale-encoding-mapping>
    <locale-encoding-mapping>
      <locale>sk</locale>
      <encoding>ISO-8859-2</encoding>
    </locale-encoding-mapping>
    <locale-encoding-mapping>
      <locale>sl</locale>
      <encoding>ISO-8859-2</encoding>
    </locale-encoding-mapping>
    <locale-encoding-mapping>
      <locale>sq</locale>
      <encoding>ISO-8859-2</encoding>
    </locale-encoding-mapping>
    <locale-encoding-mapping>
      <locale>sr</locale>
      <encoding>ISO-8859-5</encoding>
    </locale-encoding-mapping>
    <locale-encoding-mapping>
      <locale>sv</locale>
      <encoding>ISO-8859-1</encoding>
    </locale-encoding-mapping>
    <locale-encoding-mapping>
      <locale>tr</locale>
      <encoding>ISO-8859-9</encoding>
    </locale-encoding-mapping>
    <locale-encoding-mapping>
      <locale>uk</locale>
      <encoding>ISO-8859-5</encoding>
    </locale-encoding-mapping>
    <locale-encoding-mapping>
      <locale>zh</locale>
      <encoding>GB2312</encoding>
    </locale-encoding-mapping>
    <locale-encoding-mapping>
      <locale>zh_TW</locale>
      <encoding>Big5</encoding>
    </locale-encoding-mapping>
  </locale-encoding-mapping-list>

  <!-- ==================================================================== -->
  <!-- Disable TRACE method with security constraint -->
  <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
  <security-constraint>
    <web-resource-collection>
      <web-resource-name>Disable TRACE</web-resource-name>
      <url-pattern>/</url-pattern>
      <http-method>TRACE</http-method>
    </web-resource-collection>
    <auth-constraint/>
  </security-constraint>
  <security-constraint>
    <web-resource-collection>
      <web-resource-name>Enable everything but TRACE</web-resource-name>
      <url-pattern>/</url-pattern>
      <http-method-omission>TRACE</http-method-omission>
    </web-resource-collection>
  </security-constraint>

</web-app>

启动项目:

jetty插件启动:

JSP小练习新闻管理系统_https://bianchenghao6.com/blog__第17张

启动信息:

/home/lhz/opt/jdk-17/bin/java -Dmaven.multiModuleProjectDirectory=/home/lhz/IdeaProjects/news -Dmaven.home=/home/lhz/.m2/wrapper/dists/apache-maven-3.8.5-bin/5i5jha092a3i37g0paqnfr15e0/apache-maven-3.8.5 -Dclassworlds.conf=/home/lhz/.m2/wrapper/dists/apache-maven-3.8.5-bin/5i5jha092a3i37g0paqnfr15e0/apache-maven-3.8.5/bin/m2.conf -Dmaven.ext.class.path=/home/lhz/opt/idea-IU/plugins/maven/lib/maven-event-listener.jar -javaagent:/home/lhz/opt/idea-IU/lib/idea_rt.jar=43273:/home/lhz/opt/idea-IU/bin -Dfile.encoding=UTF-8 -classpath /home/lhz/.m2/wrapper/dists/apache-maven-3.8.5-bin/5i5jha092a3i37g0paqnfr15e0/apache-maven-3.8.5/boot/plexus-classworlds.license:/home/lhz/.m2/wrapper/dists/apache-maven-3.8.5-bin/5i5jha092a3i37g0paqnfr15e0/apache-maven-3.8.5/boot/plexus-classworlds-2.6.0.jar org.codehaus.classworlds.Launcher -Didea.version=2022.1.2 org.eclipse.jetty:jetty-maven-plugin:11.0.9:run
[INFO] Scanning for projects...
[INFO] 
[INFO] -------------------------< com.lihaozhe:news >--------------------------
[INFO] Building news 1.0-SNAPSHOT
[INFO] --------------------------------[ war ]---------------------------------
[INFO] 
[INFO] >>> jetty-maven-plugin:11.0.9:run (default-cli) > test-compile @ news >>>
[INFO] 
[INFO] --- maven-resources-plugin:3.2.0:resources (default-resources) @ news ---
[INFO] Using 'utf-8' encoding to copy filtered resources.
[INFO] Using 'utf-8' encoding to copy filtered properties files.
[INFO] Copying 1 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ news ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /home/lhz/IdeaProjects/news/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:3.2.0:testResources (default-testResources) @ news ---
[INFO] Not copying test resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ news ---
[INFO] Not compiling test sources
[INFO] 
[INFO] <<< jetty-maven-plugin:11.0.9:run (default-cli) < test-compile @ news <<<
[INFO] 
[INFO] 
[INFO] --- jetty-maven-plugin:11.0.9:run (default-cli) @ news ---
[INFO] Configuring Jetty for project: news
[INFO] Classes = /home/lhz/IdeaProjects/news/target/classes
[INFO] Context path = /lhz
[INFO] Tmp directory = /home/lhz/IdeaProjects/news/target/tmp
[INFO] web.xml file = file:///home/lhz/IdeaProjects/news/src/main/webapp/WEB-INF/web.xml
[INFO] Webapp directory = /home/lhz/IdeaProjects/news/src/main/webapp
[INFO] Web defaults = /home/lhz/IdeaProjects/news/src/main/resources/webdefault.xml
[INFO] Web overrides =  none
[INFO] jetty-11.0.9; built: 2022-03-30T17:44:47.085Z; git: 243a48a658a183130a8c8de353178d154ca04f04; jvm 17.0.3.1+2-LTS-6
[INFO] Session workerName=node0
[INFO] Started o.e.j.m.p.MavenWebAppContext@617449dd{ 
   /lhz,[file:///home/lhz/IdeaProjects/news/src/main/webapp/, jar:file:///home/lhz/.m2/repository/org/webjars/jquery/3.6.0/jquery-3.6.0.jar!/META-INF/resources, jar:file:///home/lhz/.m2/repository/org/webjars/popper.js/2.9.3/popper.js-2.9.3.jar!/META-INF/resources, jar:file:///home/lhz/.m2/repository/org/webjars/bootstrap/5.1.3/bootstrap-5.1.3.jar!/META-INF/resources],AVAILABLE}{ 
   file:///home/lhz/IdeaProjects/news/src/main/webapp/}
[INFO] Started ServerConnector@bd93bc3{ 
   HTTP/1.1, (http/1.1)}{ 
   0.0.0.0:6633}
[INFO] Started Server@2443135{ 
   STARTING}[11.0.9,sto=0] @3549ms
[INFO] Automatic redeployment disabled, see 'mvn jetty:help' for more redeployment options

JSP小练习新闻管理系统_https://bianchenghao6.com/blog__第18张

浏览器访问:

访问地址:http://localhost:6633/lhz/

JSP小练习新闻管理系统_https://bianchenghao6.com/blog__第19张

设计DTO类

新闻类

package com.lihaozhe.news.dto;
import lombok.Data;

import java.util.Date;

/** * @author 李昊哲 * @version 1.0.0 2022/6/10 上午8:41 */
@Data
public class News { 
   
    private long id;
    private String title;
    private String author;
    private Date date;
    private long readCount;
    private static long count;

    public News() { 
   
        this.id = ++count;
    }

    public News(String title, String author, Date date, long readCount) { 
   
        this.title = title;
        this.author = author;
        this.date = date;
        this.readCount = readCount;
        this.id = ++count;
    }

    public News(long id, String title, String author, Date date, long readCount) { 
   
        this.id = id;
        this.title = title;
        this.author = author;
        this.date = date;
        this.readCount = readCount;
    }
}

用户类

package com.lihaozhe.news.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

/** * @author 李昊哲 * @version 1.0.0 2022/6/10 上午8:41 */
@Setter
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class User { 
   
    /** * 账号 */
    private String account;
    /** * 密码 */
    private String password;
    /** * 姓名 */
    private String nickname;

    public User(String account, String password) { 
   
        this.account = account;
        this.password = password;
    }
}

设计VO类

用户类

package com.lihaozhe.news.vo;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

/** * @author 李昊哲 * @version 1.0.0 2022/6/10 上午8:41 */
@Setter
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class UserVo { 
   
    /** * 账号 */
    private String account;
    /** * 密码 */
    private String password;
    /** * 姓名 */
    private String nickname;

    public UserVo(String account, String password) { 
   
        this.account = account;
        this.password = password;
    }
}

设计数据库类

package com.lihaozhe.news.dao;

import com.lihaozhe.news.dto.News;
import com.lihaozhe.news.dto.User;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

/** * @author 李昊哲 * @version 1.0.0 2022/6/10 上午8:41 */
public class Newsdb { 
   
    private static Newsdb instance = new Newsdb();

    private Newsdb() { 
   
        userList.add(new User("admin", "123456", "李昊哲"));
        userList.add(new User("user", "123456", "李哲昊"));
        newsList.add(new News("新闻一", "张三", new Date(), ThreadLocalRandom.current().nextInt(1000, 10000)));
        newsList.add(new News("新闻二", "张三", new Date(), ThreadLocalRandom.current().nextInt(1000, 10000)));
        newsList.add(new News("新闻三", "张三", new Date(), ThreadLocalRandom.current().nextInt(1000, 10000)));
        newsList.add(new News("新闻四", "张三", new Date(), ThreadLocalRandom.current().nextInt(1000, 10000)));
        newsList.add(new News("新闻五", "张三", new Date(), ThreadLocalRandom.current().nextInt(1000, 10000)));;
    }

    /** * 用户表 */
    private List<User> userList = new ArrayList<>();
    /** * 新闻表 */
    private List<News> newsList = new ArrayList<>();

    public List<User> getUserList() { 
   
        return userList;
    }

    public void setUserList(List<User> userList) { 
   
        this.userList = userList;
    }

    public List<News> getNewsList() { 
   
        return newsList;
    }

    public void setNewsList(List<News> newsList) { 
   
        this.newsList = newsList;
    }

    public static Newsdb getInstance() { 
   
        return instance;
    }
}

登录页面

文件位置src/main/webapp/WEB-INF/view/login.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>李昊哲-小课</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/webjars/bootstrap/5.1.3/css/bootstrap.min.css">
<script src="${pageContext.request.contextPath}/webjars/bootstrap/5.1.3/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container" style="font-family: 微软雅黑">
    <div class="page-header mt-5">
        <h3 class="text-center">登录</h3>
    </div>
    <hr class="my-5">
    <form action="${pageContext.request.contextPath}/LoginServlet" method="post">
        <div class="form-group row mb-3">
            <p class="col-5 lead text-end">账&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;号:</p>
            <div class="col-3">
                <input type="text" class="form-control" name="account" value="" placeholder="请输入账号" autocomplete="off">
            </div>
        </div>
        <div class="form-group row mb-3">
            <p class="col-5 lead text-end">密&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;码:</p>
            <div class="col-3">
                <input type="password" class="form-control" name="password" value="" placeholder="请输入密码">
            </div>
        </div>
        <div class="row">
            <p class="col-4 offset-4 text-center bg-danger text-white lead mb-3">${requestScope.msg}</p>
        </div>
        <div class="form-group row mb-3">
            <div class="col-4 offset-4">
                <div class="d-grid">
                    <button type="submit" class="btn btn-outline-success">登录系统</button>
                </div>
            </div>
        </div>
    </form>
</div>
</body>
</html>

默认主页

修改src/main/webapp/index.jsp

<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
    <title>李昊哲-小课</title>
</head>
<body>
<jsp:forward page="/WEB-INF/view/login.jsp"></jsp:forward>
</body>
</html>

浏览器访问登录页面

访问地址:http://localhost:6633/lhz/

!JSP小练习新闻管理系统_https://bianchenghao6.com/blog__第1张

登录Servlet

package com.lihaozhe.news.controller;
/** * @author 李昊哲 * @version 1.0.0 2022/6/10 上午8:41 */

import com.lihaozhe.news.dao.Newsdb;
import com.lihaozhe.news.dto.User;
import com.lihaozhe.news.vo.UserVo;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

@WebServlet(name = "LoginServlet", value = "/LoginServlet")
public class LoginServlet extends HttpServlet { 
   

    private final static Newsdb instance = Newsdb.getInstance();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
   
        String account = request.getParameter("account");
        if (StringUtils.isEmpty(account)) { 
   
            request.setAttribute("msg", "账号不能为空");
            request.getRequestDispatcher("/").forward(request, response);
            return;
        }
        String password = request.getParameter("password");
        if (StringUtils.isEmpty(password)) { 
   
            request.setAttribute("msg", "密码不能为空");
            request.getRequestDispatcher("/").forward(request, response);
            return;
        }
        List<User> userList = instance.getUserList();
        Iterator<User> it = userList.listIterator();
        while (it.hasNext()) { 
   
            User user = it.next();
            if (user.getAccount().equals(account) && user.getPassword().equals(password)) { 
   
                UserVo userVo = new UserVo();
                userVo.setAccount(user.getAccount());
                userVo.setNickname(user.getNickname());
                request.getSession().setAttribute("operator", userVo);
                request.getRequestDispatcher("/WEB-INF/view/newslist.jsp").forward(request, response);
                return;
            }
        }
        request.setAttribute("msg", "账号或者密码错误");
        request.getRequestDispatcher("/").forward(request, response);
        return;
    }
}

新闻列表页面

文件位置src/main/webapp/WEB-INF/view/newslist.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
    <head>
        <title>李昊哲-小课</title>
        <link rel="stylesheet" href="${pageContext.request.contextPath}/webjars/bootstrap/5.1.3/css/bootstrap.min.css">
        <script src="${pageContext.request.contextPath}/webjars/bootstrap/5.1.3/js/bootstrap.min.js"></script>
    </head>
    <body>
        <div class="container">
            <div class="page-header mt-5">
                <h3 class="text-center">新闻列表</h3>
            </div>
            <hr class="my-3">
            <div class="row">
                <div class="col-6">操作者:${sessionScope.operator.nickname}</div>
                <div class="col-6 text-end"><a href="${pageContext.request.contextPath}/Logout" class="btn btn-secondary">退出系统</a></div>
            </div>
            <hr class="my-3">
            <table class="table table-bordered">
                <thead>
                    <tr class="text-center">
                        <th>新闻编号</th>
                        <th>新闻标题</th>
                        <th>作者</th>
                        <th>发布时间</th>
                        <th>阅读量</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td>1</td>
                        <td>新闻一</td>
                        <td>张三</td>
                        <td> 2022-06-20</td>
                        <td>3878</td>
                        <td>
                            <a href="/lhz/RemoveNewsById.do?id=2" class="btn btn-danger">删除</a>
                            <a href="/lhz/ToAddNewsPage.do" class="btn btn-success">新增</a>
                            <a href="/lhz/ToUpdateNewsPage.do?id=2" class="btn btn-info">修改</a>
                        </td>
                    </tr>

                    <tr>
                        <td>2</td>
                        <td>新闻二</td>
                        <td>张三</td>
                        <td> 2022-06-20</td>
                        <td>8456</td>
                        <td>
                            <a href="/lhz/RemoveNewsById.do?id=1" class="btn btn-danger">删除</a>
                            <a href="/lhz/ToAddNewsPage.do" class="btn btn-success">新增</a>
                            <a href="/lhz/ToUpdateNewsPage.do?id=1" class="btn btn-info">修改</a>
                        </td>
                    </tr><tr>
                    <td>3</td>
                    <td>新闻三</td>
                    <td>张三</td>
                    <td> 2022-06-20</td>
                    <td>1895</td>
                    <td>
                        <a href="/lhz/RemoveNewsById.do?id=3" class="btn btn-danger">删除</a>
                        <a href="/lhz/ToAddNewsPage.do" class="btn btn-success">新增</a>
                        <a href="/lhz/ToUpdateNewsPage.do?id=3" class="btn btn-info">修改</a>
                    </td>
                    </tr>

                    <tr>
                        <td>4</td>
                        <td>新闻四</td>
                        <td>张三</td>
                        <td> 2022-06-20</td>
                        <td>9331</td>
                        <td>
                            <a href="/lhz/RemoveNewsById.do?id=4" class="btn btn-danger">删除</a>
                            <a href="/lhz/ToAddNewsPage.do" class="btn btn-success">新增</a>
                            <a href="/lhz/ToUpdateNewsPage.do?id=4" class="btn btn-info">修改</a>
                        </td>
                    </tr>

                    <tr>
                        <td>5</td>
                        <td>新闻五</td>
                        <td>张三</td>
                        <td> 2022-06-20</td>
                        <td>1573</td>
                        <td>
                            <a href="/lhz/RemoveNewsById.do?id=5" class="btn btn-danger">删除</a>
                            <a href="/lhz/ToAddNewsPage.do" class="btn btn-success">新增</a>
                            <a href="/lhz/ToUpdateNewsPage.do?id=5" class="btn btn-info">修改</a>
                        </td>
                    </tr>

                </tbody>
            </table>
        </div>
    </body>
</html>

测试登录过程

访问地址:http://localhost:6633/lhz/

访问主页

JSP小练习新闻管理系统_https://bianchenghao6.com/blog__第1张

未输入账号登录

JSP小练习新闻管理系统_https://bianchenghao6.com/blog__第2张

未输入密码

JSP小练习新闻管理系统_https://bianchenghao6.com/blog__第3张

输入错误账号或密码

JSP小练习新闻管理系统_https://bianchenghao6.com/blog__第4张

输入正确账号密码

测试账号:admin

测试密码:123456

输入正确账号密码登录系统后系统跳转新闻列表页面

JSP小练习新闻管理系统_https://bianchenghao6.com/blog__第6张

退出系统

退出系统路由

package com.lihaozhe.news.router;
/** * @author 李昊哲 * @version 1.0.0 2022/6/10 上午8:41 */

import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;

import java.io.IOException;

@WebServlet(name = "Logout", value = "/Logout")
public class Logout extends HttpServlet { 
   
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
   
        // session 失效
        request.getSession().invalidate();
        // 网页重定向到主页
        response.sendRedirect(request.getContextPath() + "/");
    }

}

回到登录页面

JSP小练习新闻管理系统_https://bianchenghao6.com/blog__第1张

新闻列表

新闻列表Servlet

package com.lihaozhe.news.controller;
/** * @author 李昊哲 * @version 1.0.0 2022/6/10 上午8:41 */

import com.lihaozhe.news.dao.Newsdb;
import com.lihaozhe.news.dto.News;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;

import java.io.IOException;
import java.util.List;

@WebServlet(name = "AllNews", value = "/AllNews.do")
public class AllNews extends HttpServlet { 
   
    private static final long serialVersionUID = -517354344752297721L;
    private final static Newsdb instance = Newsdb.getInstance();

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
   
        List<News> newsList = instance.getNewsList();
        request.setAttribute("newsList", newsList);
        request.getRequestDispatcher("/WEB-INF/view/newslist.jsp").forward(request, response);
    }
}

新闻列表页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html>
<head>
    <title>李昊哲-小课</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/webjars/bootstrap/5.1.3/css/bootstrap.min.css">
        <script src="${pageContext.request.contextPath}/webjars/bootstrap/5.1.3/js/bootstrap.min.js">
</head>
<body>
<div class="container">
    <div class="page-header mt-5">
        <h3 class="text-center">新闻列表</h3>
    </div>
    <hr class="my-3">
    <div class="row">
		<div class="col-6">操作者:${sessionScope.operator.nickname}</div>
        <div class="col-6 text-end">
            <a href="${pageContext.request.contextPath}/Logout" class="btn btn-secondary">退出系统</a>
        </div>
    </div>
    <hr class="my-3">
    <table class="table table-bordered">
        <thead>
        <tr class="text-center">
            <th>新闻编号</th>
            <th>新闻标题</th>
            <th>作者</th>
            <th>发布时间</th>
            <th>阅读量</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
        <%--
          items   遍历项
          var     列表元素
          varStatus 状态
          index   列表索引
          count   列表计数器
          step    步长值为1
          begin   起始索引
          end     结束索引
        --%>
        <c:forEach items="${requestScope.newsList}" var="news">
            <tr>
                <td>${news.id}</td>
                <td>${news.title}</td>
                <td>${news.author}</td>
                <td> <%--${news.date}--%><fmt:formatDate value="${news.date}" pattern="yyyy-MM-dd"/></td>
                <td>${news.readCount}</td>
                <td>
                    <a href="${pageContext.request.contextPath}/RemoveNewsById.do?id=${news.id}" class="btn btn-danger">删除</a>
                    <a href="${pageContext.request.contextPath}/ToAddNewsPage.do" class="btn btn-success">新增</a>
                    <a href="${pageContext.request.contextPath}/ToUpdateNewsPage.do?id=${news.id}" class="btn btn-info">修改</a>
                </td>
            </tr>
        </c:forEach>
        </tbody>
    </table>
</div>
</body>
</html>

测试新闻列表Servlet

访问地址:http://localhost:6633/lhz/AllNews.do

JSP小练习新闻管理系统_https://bianchenghao6.com/blog__第6张

登录Servlet

修改原的登录Servlet登场成功后的跳转地址

request.getRequestDispatcher("/AllNews.do").forward(request, response);

修改后完整代码

package com.lihaozhe.news.controller;
/** * @author 李昊哲 * @version 1.0.0 2022/6/10 上午8:41 */

import com.lihaozhe.news.dao.Newsdb;
import com.lihaozhe.news.dto.User;
import com.lihaozhe.news.vo.UserVo;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

@WebServlet(name = "LoginServlet", value = "/LoginServlet")
public class LoginServlet extends HttpServlet { 
   

    private final static Newsdb instance = Newsdb.getInstance();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
   
        String account = request.getParameter("account");
        if (StringUtils.isEmpty(account)) { 
   
            request.setAttribute("msg", "账号不能为空");
            request.getRequestDispatcher("/").forward(request, response);
            return;
        }
        String password = request.getParameter("password");
        if (StringUtils.isEmpty(password)) { 
   
            request.setAttribute("msg", "密码不能为空");
            request.getRequestDispatcher("/").forward(request, response);
            return;
        }
        List<User> userList = instance.getUserList();
        Iterator<User> it = userList.listIterator();
        while (it.hasNext()) { 
   
            User user = it.next();
            if (user.getAccount().equals(account) && user.getPassword().equals(password)) { 
   
                UserVo userVo = new UserVo();
                userVo.setAccount(user.getAccount());
                userVo.setNickname(user.getNickname());
                request.getSession().setAttribute("operator", userVo);
                // request.getRequestDispatcher("/WEB-INF/view/newslist.jsp").forward(request, response);
                request.getRequestDispatcher("/AllNews.do").forward(request, response);
                return;
            }
        }
        request.setAttribute("msg", "账号或者密码错误");
        request.getRequestDispatcher("/").forward(request, response);
        return;
    }
}

测试登录后跳转新闻列表

访问地址:http://localhost:6633/lhz/

测试账号:admin

测试密码:123456

访问登录页面

JSP小练习新闻管理系统_https://bianchenghao6.com/blog__第1张

新闻列表

JSP小练习新闻管理系统_https://bianchenghao6.com/blog__第6张

新增新闻

1、新增按钮为a标签跳转后端路由 后端路由再跳转新增页面

2、在新增新闻页面输入新闻标题点击发布按钮向新闻标题发送给后端Servlet写入数据库

3、后端写入数据库后重新查询数据库列表信息并返回新闻列表

新增新闻按钮

<a href="${pageContext.request.contextPath}/ToAddNewsPage.do" class="btn btn-success">新增</a>

新增新闻页面后端路由

package com.lihaozhe.news.router;
/** * @author 李昊哲 * @version 1.0.0 2022/6/10 上午8:41 */

import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;

import java.io.IOException;

@WebServlet(name = "ToAddNewsPage", value = "/ToAddNewsPage.do")
public class ToAddNewsPage extends HttpServlet { 
   
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
   
        request.getRequestDispatcher("/WEB-INF/view/addnews.jsp").forward(request,response);
    }
}

新增新闻页面

文件位置src/main/webapp/WEB-INF/view/addnews.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>李昊哲-小课</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/webjars/bootstrap/5.1.3/css/bootstrap.min.css">
	<script src="${pageContext.request.contextPath}/webjars/bootstrap/5.1.3/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container mb-t">
    <div class="row">
		<div class="col-6">操作者:${sessionScope.operator.nickname}</div>
        <div class="col-6 text-end">
            <a href="${pageContext.request.contextPath}/Logout" class="btn btn-secondary">退出系统</a>
        </div>
    </div>
    <div class="row">
        <div class="col-4"></div>
        <div class="col-4">
            <form action="${pageContext.request.contextPath}/AddNews.do" method="post">
                <div class="form-group row mb-3">
                    标题: <input class="form-control" type="text" name="title" value="" autocomplete="off">
                </div>
                <div class="form-group row mb-3">
                    <div class="d-grid">
                        <button type="submit" class="btn btn-primary">发布</button>
                    </div>
                </div>
            </form>
        </div>
        <div class="col-4"></div>
    </div>
</div>
</body>
</html>

新增新闻Servlet

package com.lihaozhe.news.controller;
/** * @author 李昊哲 * @version 1.0.0 2022/6/10 上午8:41 */

import com.lihaozhe.news.dao.Newsdb;
import com.lihaozhe.news.dto.News;
import com.lihaozhe.news.vo.UserVo;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;

import java.io.IOException;
import java.util.Date;
import java.util.List;

@WebServlet(name = "AddNews", value = "/AddNews.do")
public class AddNews extends HttpServlet { 
   

    private static final long serialVersionUID = -1332560525247994566L;
    private final static Newsdb instance = Newsdb.getInstance();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
   
        List<News> newsList = instance.getNewsList();
        News news = new News();
        String title = request.getParameter("title");
        news.setTitle(title);
        UserVo operator = (UserVo) request.getSession().getAttribute("operator");
        news.setAuthor(operator.getNickname());
        news.setDate(new Date());
        newsList.add(news);
        instance.setNewsList(newsList);
        request.getRequestDispatcher("/AllNews.do").forward(request, response);
        return;
    }
}

测试新增新闻

访问新增新闻页面

在新闻列表点击新增按钮

JSP小练习新闻管理系统_https://bianchenghao6.com/blog__第7张

输入新闻标题

JSP小练习新闻管理系统_https://bianchenghao6.com/blog__第8张

发布新闻后访后自动跳转新闻列表

JSP小练习新闻管理系统_https://bianchenghao6.com/blog__第9张

修改新闻

1、点击修改新闻按钮携带该新闻id跳转后端路由

2、后端路由从数据库中查询到该新闻信息

3、后端路由携带新闻信息跳转修改新闻页面

4、页面修改新闻后 携带该新闻id 将数据发送给后端Servlet

5、后端Servlet将修改后的信息存入数据库

6、查询数据库中的新闻列表后跳跳新闻列表页面

修改新闻按钮

<a href="${pageContext.request.contextPath}/ToUpdateNewsPage.do?id=${news.id}" class="btn btn-info">修改</a>

修改新闻后端路由

package com.lihaozhe.news.controller;
/** * @author 李昊哲 * @version 1.0.0 2022/6/10 上午8:41 */

import com.lihaozhe.news.dao.Newsdb;
import com.lihaozhe.news.dto.News;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

@WebServlet(name = "ToUpdateNewsPage", value = "/ToUpdateNewsPage.do")
public class ToUpdateNewsPage extends HttpServlet { 
   
    private static final long serialVersionUID = 6231273724806573111L;
    private final static Newsdb instance = Newsdb.getInstance();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
   
        String id = request.getParameter("id");
        List<News> newsList = instance.getNewsList();
        Iterator<News> it = newsList.listIterator();
        News news = null;
        while (it.hasNext()) { 
   
            News next = it.next();
            if (String.valueOf(next.getId()).compareTo(id) == 0) { 
   
                news = next;
                break;
            }
        }
        request.setAttribute("news", news);
        request.getRequestDispatcher("/WEB-INF/view/updatenews.jsp").forward(request, response);
    }
}

修改新闻页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>李昊哲-小课</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/webjars/bootstrap/5.1.3/css/bootstrap.min.css">
	<script src="${pageContext.request.contextPath}/webjars/bootstrap/5.1.3/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container mb-t">
    <div class="row">
		<div class="col-6">操作者:${sessionScope.operator.nickname}</div>
        <div class="col-6 text-end">
            <a href="${pageContext.request.contextPath}/Logout" class="btn btn-secondary">退出系统</a>
        </div>
    </div>
    <div class="row">
        <div class="col-4"></div>
        <div class="col-4">
            <form action="${pageContext.request.contextPath}/UpdateNews.do" method="post">
                <div class="form-group row mb-3">
                    <input class="form-control" type="text" name="id" value="${requestScope.news.id}" autocomplete="off" readonly hidden>
                </div>
                <div class="form-group row mb-3">
                    标题: <input class="form-control" type="text" name="title" value="${requestScope.news.title}" autocomplete="off">
                </div>
                <div class="form-group row mb-3">
                    <div class="d-grid">
                        <button type="submit" class="btn btn-primary">发布</button>
                    </div>
                </div>
            </form>
        </div>
        <div class="col-4"></div>
    </div>
</div>
</body>
</html>

修改新闻Servlet

传统编码方式
package com.lihaozhe.news.controller;
/** * @author 李昊哲 * @version 1.0.0 2022/6/10 上午8:41 */

import com.lihaozhe.news.dao.Newsdb;
import com.lihaozhe.news.dto.News;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

@WebServlet(name = "UpdateNews", value = "/UpdateNews.do")
public class UpdateNews extends HttpServlet { 
   

    private static final long serialVersionUID = 7817625721378462501L;
    private final static Newsdb instance = Newsdb.getInstance();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
   
        String id = request.getParameter("id");
        String title = request.getParameter("title");
        List<News> newsList = instance.getNewsList();
        for (int i = 0; i < newsList.size(); i++) { 
   
            News news = newsList.get(i);
            if (String.valueOf(news.getId()).compareTo(id) == 0) { 
   
                news.setTitle(title);
                break;
            }
        }
        instance.setNewsList(newsList);
        request.getRequestDispatcher("/AllNews.do").forward(request, response);
        return;
    }
}

Stream API方式
// 传统编码
for (int i = 0; i < newsList.size(); i++) { 
   
    News news = newsList.get(i);
    if (String.valueOf(news.getId()).compareTo(id) == 0) { 
   
        news.setTitle(title);
        break;
    }
}
// Stream API
newsList = newsList.stream().map(news -> { 
   
    if (String.valueOf(news.getId()).compareTo(id) == 0) { 
   
        news.setTitle(title);
    }
    return news;
}).collect(Collectors.toList());

修改后完整代码

package com.lihaozhe.news.controller;
/** * @author 李昊哲 * @version 1.0.0 2022/6/10 上午8:41 */

import com.lihaozhe.news.dao.Newsdb;
import com.lihaozhe.news.dto.News;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

@WebServlet(name = "UpdateNews", value = "/UpdateNews.do")
public class UpdateNews extends HttpServlet { 
   

    private static final long serialVersionUID = 7817625721378462501L;
    private final static Newsdb instance = Newsdb.getInstance();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
   
        String id = request.getParameter("id");
        String title = request.getParameter("title");
        List<News> newsList = instance.getNewsList();
// for (int i = 0; i < newsList.size(); i++) { 
   
// News news = newsList.get(i);
// if (String.valueOf(news.getId()).compareTo(id) == 0) { 
   
// news.setTitle(title);
// break;
// }
// }
        newsList = newsList.stream().map(news -> { 
   
            if (String.valueOf(news.getId()).compareTo(id) == 0) { 
   
                news.setTitle(title);
            }
            return news;
        }).collect(Collectors.toList());
        instance.setNewsList(newsList);
        request.getRequestDispatcher("/AllNews.do").forward(request, response);
        return;
    }
}

测试修改新闻

访问修改新闻页面

在新闻列表点击修改按钮

JSP小练习新闻管理系统_https://bianchenghao6.com/blog__第8张

修改新闻

JSP小练习新闻管理系统_https://bianchenghao6.com/blog__第11张

发布修改后的新闻自动跳转新闻列表

JSP小练习新闻管理系统_https://bianchenghao6.com/blog__第9张

删除新闻

1、点击删除按钮 携带该新闻id 跳转后端删除Servlet

2、后端Servlet在数据库中删除该条新闻

3、删除新闻后跳转新闻列表

删除新闻按钮

<a href="${pageContext.request.contextPath}/RemoveNewsById.do?id=${news.id}" class="btn btn-danger">删除</a>

删除新闻Servlet

传统编码方式
package com.lihaozhe.news.controller;
/** * @author 李昊哲 * @version 1.0.0 2022/6/10 上午8:41 */

import com.lihaozhe.news.dao.Newsdb;
import com.lihaozhe.news.dto.News;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

@WebServlet(name = "RemoveNewsById", value = "/RemoveNewsById.do")
public class RemoveNewsById extends HttpServlet { 
   
    private static final long serialVersionUID = 1438029433243714106L;
    private final static Newsdb instance = Newsdb.getInstance();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
   
        String id = request.getParameter("id");
        if (StringUtils.isEmpty(id)) { 
   
            request.getRequestDispatcher("/AllNews.do").forward(request, response);
            return;
        }
        List<News> newsList = instance.getNewsList();
        for (int i = 0; i < newsList.size(); i++) { 
   
            News news = newsList.get(i);
            if (String.valueOf(news.getId()).compareTo(id) == 0) { 
   
                newsList.remove(i);
                break;
            }
        }
        instance.setNewsList(newsList);
        request.getRequestDispatcher("/AllNews.do").forward(request, response);
        return;
    }
}

Stream API方式
// 传统编码
for (int i = 0; i < newsList.size(); i++) { 
   
    News news = newsList.get(i);
    if (String.valueOf(news.getId()).compareTo(id) == 0) { 
   
        newsList.remove(i);
        break;
    }
}
// Stream API
newsList = newsList.stream().filter(news -> String.valueOf(news.getId()).compareTo(id) != 0).collect(Collectors.toList());

修改后完整代码

package com.lihaozhe.news.controller;
/** * @author 李昊哲 * @version 1.0.0 2022/6/10 上午8:41 */

import com.lihaozhe.news.dao.Newsdb;
import com.lihaozhe.news.dto.News;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

@WebServlet(name = "RemoveNewsById", value = "/RemoveNewsById.do")
public class RemoveNewsById extends HttpServlet { 
   
    private static final long serialVersionUID = 1438029433243714106L;
    private final static Newsdb instance = Newsdb.getInstance();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
   
        String id = request.getParameter("id");
        if (StringUtils.isEmpty(id)) { 
   
            request.getRequestDispatcher("/AllNews.do").forward(request, response);
            return;
        }
        List<News> newsList = instance.getNewsList();
// for (int i = 0; i < newsList.size(); i++) { 
   
// News news = newsList.get(i);
// if (String.valueOf(news.getId()).compareTo(id) == 0) { 
   
// newsList.remove(i);
// break;
// }
// }
        newsList = newsList.stream().filter(news -> String.valueOf(news.getId()).compareTo(id) != 0).collect(Collectors.toList());
        instance.setNewsList(newsList);
        request.getRequestDispatcher("/AllNews.do").forward(request, response);
        return;
    }
}

测试修改新闻

访问新闻列表页面

在新闻列表点击删除按钮

JSP小练习新闻管理系统_https://bianchenghao6.com/blog__第12张

删除后重新访问新闻列表

JSP小练习新闻管理系统_https://bianchenghao6.com/blog__第6张

未登录处理

编写过滤器

package com.lihaozhe.news.filter;
/** * @author 李昊哲 * @version 1.0.0 2022/6/10 上午8:41 */

import jakarta.servlet.*;
import jakarta.servlet.annotation.*;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;

@WebFilter(filterName = "Authentication", urlPatterns = { 
   "*.do", "*.action"})
public class Authentication implements Filter { 
   
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws ServletException, IOException { 
   
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        HttpSession session = request.getSession();
        if (session.getAttribute("operator") == null) { 
   
            request.setAttribute("msg", "请输入账号密码");
            request.getRequestDispatcher("/").forward(request, response);
            return;
        }
        chain.doFilter(request, response);
    }
}

测试未登录

访问地址:http://localhost:6633/lhz/AllNews.do

JSP小练习新闻管理系统_https://bianchenghao6.com/blog__第5张

代码优化

抽取冗余代码

抽取bootstrap引入

将bootstrap引入抽取到src/main/webapp/WEB-INF/commons/bootstrap.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<link rel="stylesheet" href="${pageContext.request.contextPath}/webjars/bootstrap/5.1.3/css/bootstrap.min.css">
<script src="${pageContext.request.contextPath}/webjars/bootstrap/5.1.3/js/bootstrap.min.js"></script>

将源代码中bootstrap引入替换

<jsp:include page="/WEB-INF/commons/bootstrap.jsp"></jsp:include>

抽取登录人显示信息和退出系统按钮

将 登录人显示信息和退出系统按钮抽 取到src/main/webapp/WEB-INF/commons/operator.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<div class="row">
    <div class="col-6">操作者:${sessionScope.operator.nickname}</div>
    <div class="col-6 text-end"><a href="${pageContext.request.contextPath}/Logout" class="btn btn-secondary">退出系统</a></div>
</div>

将 源代码中登录人显示信息和退出系统按钮 替换

<jsp:include page="/WEB-INF/commons/operator.jsp"></jsp:include>

登录页面

文件位置src/main/webapp/WEB-INF/view/login.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>李昊哲-小课</title>
    <jsp:include page="/WEB-INF/commons/bootstrap.jsp"></jsp:include>
</head>
<body>
<div class="container" style="font-family: 微软雅黑">
    <div class="page-header mt-5">
        <h3 class="text-center">登录</h3>
    </div>
    <hr class="my-5">
    <form action="${pageContext.request.contextPath}/LoginServlet" method="post">
        <div class="form-group row mb-3">
            <p class="col-5 lead text-end">账&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;号:</p>
            <div class="col-3">
                <input type="text" class="form-control" name="account" value="" placeholder="请输入账号" autocomplete="off">
            </div>
        </div>
        <div class="form-group row mb-3">
            <p class="col-5 lead text-end">密&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;码:</p>
            <div class="col-3">
                <input type="password" class="form-control" name="password" value="" placeholder="请输入密码">
            </div>
        </div>
        <div class="row">
            <p class="col-4 offset-4 text-center bg-danger text-white lead mb-3">${requestScope.msg}</p>
        </div>
        <div class="form-group row mb-3">
            <div class="col-4 offset-4">
                <div class="d-grid">
                    <button type="submit" class="btn btn-outline-success">登录系统</button>
                </div>
            </div>
        </div>
    </form>
</div>
</body>
</html>

新闻列表页面

文件位置src/main/webapp/WEB-INF/view/newslist.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html>
<head>
    <title>李昊哲-小课</title>
    <jsp:include page="/WEB-INF/commons/bootstrap.jsp"></jsp:include>
</head>
<body>
<div class="container">
    <div class="page-header mt-5">
        <h3 class="text-center">新闻列表</h3>
    </div>
    <hr class="my-3">
    <jsp:include page="/WEB-INF/commons/operator.jsp"></jsp:include>
    <hr class="my-3">
    <table class="table table-bordered">
        <thead>
        <tr class="text-center">
            <th>新闻编号</th>
            <th>新闻标题</th>
            <th>作者</th>
            <th>发布时间</th>
            <th>阅读量</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
        <%--
          items   遍历项
          var     列表元素
          varStatus 状态
          index   列表索引
          count   列表计数器
          step    步长值为1
          begin   起始索引
          end     结束索引
        --%>
        <c:forEach items="${requestScope.newsList}" var="news">
            <tr>
                <td>${news.id}</td>
                <td>${news.title}</td>
                <td>${news.author}</td>
                <td> <%--${news.date}--%><fmt:formatDate value="${news.date}" pattern="yyyy-MM-dd"/></td>
                <td>${news.readCount}</td>
                <td>
                    <a href="${pageContext.request.contextPath}/RemoveNewsById.do?id=${news.id}" class="btn btn-danger">删除</a>
                    <a href="${pageContext.request.contextPath}/ToAddNewsPage.do" class="btn btn-success">新增</a>
                    <a href="${pageContext.request.contextPath}/ToUpdateNewsPage.do?id=${news.id}" class="btn btn-info">修改</a>
                </td>
            </tr>
        </c:forEach>
        </tbody>
    </table>
</div>
</body>
</html>

新增新闻页面

文件位置src/main/webapp/WEB-INF/view/addnews.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>李昊哲-小课</title>
    <jsp:include page="/WEB-INF/commons/bootstrap.jsp"></jsp:include>
</head>
<body>
<div class="container mb-t">
    <jsp:include page="/WEB-INF/commons/operator.jsp"></jsp:include>
    <div class="row">
        <div class="col-4"></div>
        <div class="col-4">
            <form action="${pageContext.request.contextPath}/AddNews.do" method="post">
                <div class="form-group row mb-3">
                    标题: <input class="form-control" type="text" name="title" value="" autocomplete="off">
                </div>
                <div class="form-group row mb-3">
                    <div class="d-grid">
                        <button type="submit" class="btn btn-primary">发布</button>
                    </div>
                </div>
            </form>
        </div>
        <div class="col-4"></div>
    </div>
</div>
</body>
</html>

修改新闻页面

文件位置src/main/webapp/WEB-INF/view/updatenews.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>李昊哲-小课</title>
    <jsp:include page="/WEB-INF/commons/bootstrap.jsp"></jsp:include>
</head>
<body>
<div class="container mb-t">
    <jsp:include page="/WEB-INF/commons/operator.jsp"></jsp:include>
    <div class="row">
        <div class="col-4"></div>
        <div class="col-4">
            <form action="${pageContext.request.contextPath}/UpdateNews.do" method="post">
                <div class="form-group row mb-3">
                    <input class="form-control" type="text" name="id" value="${requestScope.news.id}" autocomplete="off" readonly hidden>
                </div>
                <div class="form-group row mb-3">
                    标题: <input class="form-control" type="text" name="title" value="${requestScope.news.title}" autocomplete="off">
                </div>
                <div class="form-group row mb-3">
                    <div class="d-grid">
                        <button type="submit" class="btn btn-primary">发布</button>
                    </div>
                </div>
            </form>
        </div>
        <div class="col-4"></div>
    </div>
</div>
</body>
</html>

今天的分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。

上一篇

已是最后文章

下一篇

已是最新文章

发表回复