错误情况
当我们在项目中,实现页面挑战中,总是会遇到明明路径是匹配的,但是在实际操作中会报错
如
解决方法
如果你的电脑是自己的,绝壁会出现这样的错误,所以我们最好手动用contextPath读取项目路径
比如在jsp的form表单设置成
<%--
Created by IntelliJ IDEA.
User: chen Pen
Date: 2022/5/17
Time: 14:01
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/c/c" method="post">
<input type="text" name="username">
<input type="submit">
</form>
</body>
</html>
在servlet中
package com.javaweb;
import util.Constant;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
public class SServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String contextPath =request.getContextPath();
if (username.equals("admin")){
request.getSession().setAttribute(Constant.USER_SESSION,request.getSession().getId());
System.out.println("22222222");
response.sendRedirect(contextPath+"/sys/sesssion.jsp");
}
response.sendRedirect(contextPath+"/error.jsp");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
}
一般都能完美解决问题。