小编Cus*_*ard的帖子

@WebServlet注释无法识别; init不运行

我正在努力学习注释.我目前有一个在Tomcat中启动应用程序时运行init()的webapp.

以下代码有效......

web.xml中:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">

    <servlet>
        <servlet-name>MainServlet</servlet-name>
        <servlet-class>com.company.Main</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
</web-app>
Run Code Online (Sandbox Code Playgroud)

Main.java:

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

//@WebServlet(name="MainServlet", value="/main.jsp", loadOnStartup=1)
public class Main extends GenericServlet {

    public Main() { }

    @Override
    public void init() {
        System.out.println("Hello!");
    }

    @Override
    public void destroy() {
        System.out.println("Bye!");
    }

    @Override
    public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException { }
}
Run Code Online (Sandbox Code Playgroud)

但是,当我取消注释@WebServlet注释并注释掉web.xml中的servlet条目时,init方法不会运行.

我错过了一些明显的东西吗

如果这会有帮助,这是我的pom.xml:

<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 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>...</groupId>
    <artifactId>...</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>Name</name>
    <url>http://maven.apache.org</url> …
Run Code Online (Sandbox Code Playgroud)

java annotations servlets

7
推荐指数
2
解决办法
2万
查看次数

如何在Java中将非特殊字符编码为HTML实体

以下代码:

org.apache.commons.lang.StringEscapeUtils.unescapeHtml("&#72;&#101;&#108;&#108;&#111;&#32;&#87;&#111;&#114;&#108;&#100;");
Run Code Online (Sandbox Code Playgroud)

得到:

Hello World
Run Code Online (Sandbox Code Playgroud)

但是我想知道如何从"Hello World"回到已解码的字符串.我尝试过escapeHtml方法,但这只编码特殊字符.

java encoding html-entities

2
推荐指数
1
解决办法
750
查看次数

标签 统计

java ×2

annotations ×1

encoding ×1

html-entities ×1

servlets ×1