【问题标题】:Java Web App NOT using annotationJava Web App 不使用注解
【发布时间】:2017-02-17 02:15:35
【问题描述】:

需要不使用>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>

【问题讨论】:

  • 交叉检查你的依赖..
  • 尝试在实现中指定 load-on-startup 和 Sysout 以确保 servlet 是否被加载。也只是一个想法,您应该在
    中指定请求方法,以便在 servlet 上执行正确的方法。

标签: java servlets jakarta-ee controller web.xml


【解决方案1】:

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

假设您的应用部署为myApp(即在Tomcat 中的$TOMCAT_ROOT/webapps/myApp 文件夹中),那么您的welcome.jsp 的URL 为http://localhost:8080/myApp/welcome.jsp,而OptionsWork servlet“位于”http://localhost:8080/myApp/OptionsForYou URL。但如果你在&lt;form action="/OptionsForYou" name="options"&gt; 中只指定/OptionsForYou,那么你调用http://localhost:8080/OptionsForYou,它确实不存在,你会得到404 错误。

所以,添加&lt;%= request.getContextPath() %&gt; before /OptionsForYou 以在操作中包含上下文路径,即您的&lt;form&gt; 标记应如下所示

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

它应该可以工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-19
    • 2014-03-16
    • 1970-01-01
    • 2011-09-03
    • 2013-01-03
    • 2011-09-15
    • 1970-01-01
    • 2016-09-16
    相关资源
    最近更新 更多