java.lang.IllegalArgumentException : Path designGoal. jsp 不以 "/"字符开头

标签 java jsp tomcat servlets

我正在 Eclipse 中使用 servlet 和 JSP 编写一个非常简单的 Web 应用程序。服务器是Tomcat v7.0。 Java 版本为 1.8.0_60。下面是我的 servlet 代码,写在名为 DesignGoal.java

的文件中
package com.example;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(
    urlPatterns = "/DesignGoal",
    initParams = {
            @WebInitParam(name="text",value=" extreme knitting " )
    }
)
public class DesignGoal extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        RequestDispatcher view = getServletContext().getRequestDispatcher("/designGoal.jsp");
        view.forward(request, response);
    }

}

这是我的文件 designGoal.jsp:

<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
 pageEncoding="ISO-8859-1"%>

<!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=ISO-8859-1">
    <title>Insert title here</title>
  </head>

  <body>
    Hello!
  </body>
</html>

但是当我运行文件 DesignGoal.java 时,出现以下错误:

java.lang.IllegalArgumentException: Path designGoal.jsp does not start with a "/" character

即使 getRequestDispatcher() 中的字符串以 / 开头,并且我添加的路径是相对于项目的根。

最佳答案

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
RequestDispatcher view = getServletContext().getRequestDispatcher("designGoal.jsp");<!--dont add '/' here   --!>
view.forward(request, response);

不要在调度器中添加“/”

关于java.lang.IllegalArgumentException : Path designGoal. jsp 不以 "/"字符开头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33491174/

相关文章:

java - 网络连接时如何检测网络变化和从 A 到 B 的变化 Activity

java - 关于 XML 文件的写入问题

java - jsp 未以正确格式传递 UTF-8 数据

java - 在 Java 的 Arraylist 中创建 Arraylist

java - 将excel文件复制到基于java中当前日期创建的文件夹中

java - 如何在点击警报后获得相同的JSP页面

java - 如何在 Struts 2 中将 QRCode 图像从操作类发送并显示到 JSP

apache - named的Apache服务报如下错误: Unable to open logs

java - 使用简单的 Java 类连接(登录)到 Alfresco

mysql - 在 openshift 缩放应用程序上使用资源注释