我正在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()
中的字符串以/
开头,而我添加的路径也是相对于项目的根目录。
答案 0 :(得分:0)
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
RequestDispatcher view = getServletContext().getRequestDispatcher("designGoal.jsp");<!--dont add '/' here --!>
view.forward(request, response);
}
不要添加&#34; /&#34;在调度员
答案 1 :(得分:0)
如果您的designGoal.jsp位于根目录WebContent
中String nextJSP = "/searchResults.jsp";
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(nextJSP);
dispatcher.forward(request, response);
如果它在WebContent / WEB-INF文件夹中
String nextJSP = "/WEB-INF/searchResults.jsp";
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(nextJSP);
dispatcher.forward(request, response);