我正在尝试在jetty 9.4.8中设置url重写。
我正在使用以下配置文件:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">
<Configure class="org.eclipse.jetty.server.handler.ContextHandler">
<Set name="handler">
<New id="Rewrite" class="org.eclipse.jetty.rewrite.handler.RewriteHandler">
<Set name="handler">
<New class="org.eclipse.jetty.webapp.WebAppContext">
<Set name="contextPath">/QuestionWeb</Set>
<Set name="war"><SystemProperty name="jetty.base"/>\QuestionWeb.war</Set>
<Set name="extractWAR">true</Set>
<Set name="copyWebDir">true</Set>
</New>
</Set>
<Set name="rewriteRequestURI">true</Set>
<Set name="rewritePathInfo">false</Set>
<Set name="originalPathAttribute">requestedPath</Set>
<Call name="addRule">
<Arg>
<New class="org.eclipse.jetty.rewrite.handler.RewritePatternRule">
<Set name="pattern">/test</Set>
<Set name="replacement">/question</Set>
</New>
</Arg>
</Call>
</New>
</Set>
</Configure>
启动java -jar服务器start.jar后,重定向仅首先访问该地址:
http://localhost:8080/QuestionWeb/queston
然后访问地址:
http://localhost:8080/QuestionWeb/test
如果我尝试在该地址“http://localhost:8080/QuestionWeb/test”之前访问地址“http://localhost:8080/QuestionWeb/queston”,则会收到错误状态404.
我的Servelt代码:
@WebServlet("/question")
public class QuestonServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss:SSS");
public QuestonServlet() { }
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().append(df.format(Calendar.getInstance().getTime())).append(request.getContextPath());
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}