我试图更好地了解servlet,但偶然发现了这个问题,我似乎也找不到答案。
因此,我制作了一个基本的Servlet,每次访问该URL时都会给您不同的问候。
@WebServlet(name = "GreetingServlet", value = "/Greeting")
public class GreetingServlet extends HttpServlet {
private Random rand;
private List<String> greetingList;
@Override
public void init() throws ServletException {
super.init();
rand = new Random();
greetingList = new ArrayList<>();
greetingList.add("Put that cookie down!");
greetingList.add("Hi!");
greetingList.add("Hey!");
greetingList.add("Good day to you!");
greetingList.add("Howdy-doody!");
}
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
resp.setContentType("text/html");
resp.setCharacterEncoding("UTF-8");
try (PrintWriter out = resp.getWriter()) {
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<body>");
out.println("<h1><b>" + greetingList.get(rand.nextInt(greetingList.size())) + "</b></h1>");
out.println("</body>");
out.println("</html>");
}
}
}
我测试了一下,效果很好。我想编写的第二个servlet应该将一些数据放入数据库中。因此,我对侦听器进行了编码,但是在对@WebListener注释进行编码后,我所有的servlet均停止工作,并且得到了404。
这是我的Listener类的代码。
@WebListener
public class PhoneBookListener implements ServletContextListener {
private static final Logger LOGGER = LogManager.getLogger(PhoneBookListener.class);
public void contextInitialized(ServletContextEvent e) {
EntityManagerFactory entityManagerFactory =
Persistence.createEntityManagerFactory("servletdb_pu");
LOGGER.info("EntityManagerFactory created in WebListener");
e.getServletContext().setAttribute("entityManagerFactory", entityManagerFactory);
}
public void contextDestroyed(ServletContextEvent e) {
EntityManagerFactory emf =
(EntityManagerFactory)e.getServletContext().getAttribute("entityManagerFactory");
if (emf != null) {
emf.close();
}
}
}
web.xml
<?xml version="1.0"?>
<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">
<filter>
<filter-name>CorsFilter</filter-name>
<filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
<init-param>
<param-name>cors.allowed.origins</param-name>
<param-value>*</param-value>
</init-param>
<init-param>
<param-name>cors.allowed.methods</param-name>
<param-value>GET,POST,HEAD,OPTIONS,PUT,DELETE</param-value>
</init-param>
<init-param>
<param-name>cors.allowed.headers</param-name>
<param-value>Content-Type,X-Requested-With,Accept,Accept-Encoding,Accept-Language,Origin,Access-Control-Request-Method,Access-Control-Request-Headers,Connection,Host,authorization</param-value>
</init-param>
<init-param>
<param-name>cors.exposed.headers</param-name>
<param-value>Access-Control-Allow-Origin,Access-Control-Allow-Credentials</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CorsFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
项目结构:
您的web.xml
缺少侦听器。
只需在web.xml中添加<listener>
:
<listener>
<listener-class>package.PhoneBookListener</listener-class>
</listener>
这应该起作用。