我正在 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/