Java Web 应用程序不使用注释

标签 java servlets jakarta-ee controller web.xml

需要构建一个不使用 Java3 中使用的注释的应用程序,并且开始时遇到一些麻烦。看起来 web.xml/view/controller 中的设置是合乎逻辑的并且应该有效。给出 404 错误,如果我使用注释则不会。我想这就是作业声明不要使用注释的原因!老前辈对 S.O 有什么建议吗?

P.S 任何建议都会真正帮助我启动这个应用程序,我正在网上阅读,我读过的解决方案似乎不起作用。大多数人最终建议使用注释...我不能使用!

welcome.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Welcome Page</title>
</head>
<body>

<h1>Welcome Page</h1>

<br/>

<form action="/OptionsForYou" name="options">

<select>
    <option>---Select---</option>
</select>

<button type="submit" value="submit">Submit</button>

</form>

</body>
</html>

OptionsWork.java

package com.server;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class OptionsWork
 */

public class OptionsWork extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        String options = req.getParameter("options");
        System.out.println(options);
    }

}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>Options</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
    <servlet>
        <servlet-name>OptionsWork</servlet-name>
        <servlet-class>com.server.OptionsWork</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>OptionsWork</servlet-name>
        <url-pattern>/OptionsForYou</url-pattern>
    </servlet-mapping>
</web-app>

最佳答案

问题是,除非您的 web 应用程序部署在 ROOT 上下文中,否则您必须在任何链接中指定上下文路径,包括表单的 action属性。

假设您的应用程序部署为 myApp (即在 Tomcat 中的 $TOMCAT_ROOT/webapps/myApp 文件夹中),然后是您的 welcome.jsp有网址http://localhost:8080/myApp/welcome.jspOptionsWork servlet“位于”http://localhost:8080/myApp/OptionsForYou网址。但如果您仅指定 /OptionsForYou<form action="/OptionsForYou" name="options"> ,然后您调用http://localhost:8080/OptionsForYou ,它确实不存在,并且您会收到 404 错误。

所以,添加<%= request.getContextPath() %> 之前/OptionsForYou在操作中包含上下文路径,即您的 <form>标签应该看起来像

<form action="<%= request.getContextPath() %>/OptionsForYou" name="options">

它应该可以工作。

关于Java Web 应用程序不使用注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42288235/

相关文章:

java - 无法登录 IBM WebSphere 管理控制台

java - vraptor Controller 的结果(json)序列化中未出现类型为 "String"的字段

java - Gson : Unable to invoke no-args constructor for class

java - List<Void> 的目的是什么?

java - 激活 Maven 插件 : location of java files changed, servlet 3.0 后不再工作

java - Accept-Language header 和 ResourceBundle 之间缺少功能

java - 为什么我们在匿名内部类中使用 final 关键字?

java - 如何在 Windows 中设置 ESC/Java2 环境并使用 ESC/Java2 构建/运行?

java - 反序列化 Google Web Kit 响应 (Java) 和 Java Servlet

java - 使用 runtime.getExec ("<tcl file>") 函数运行的 Gwt 进程 ID