request.getParamter()方法应返回类型参数值,但它返回空值。
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse resp = (HttpServletResponse) response;
String action = req.getParameter("type");
if(action != null && controllerMapping.containsKey(action)) {
config.getServletContext().getRequestDispatcher(controllerMapping.get(action)).forward(req, resp);
}
else {
chain.doFilter(request, response);
}
}
呼叫页面在这里:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Add an Athlete</title>
<link href="<c:url value='/css/main.css' />" type="text/css" rel="stylesheet" />
</head>
<body>
<div class="add_athlete">
<fieldset>
<legend> Athlete Information </legend>
<form class="add_athlete" action="<c:url value='/' />" method="post" enctype="multipart/form-data">
<input type="hidden" name="type" value="add_athlete" />
<label>First Name:</label>
<input type="text" name="first_name" placeholder="Enter First Name" required="required"/>
<label>Last Name:</label>
<input type="text" name="last_name" placeholder="Enter Last Name" required="required"/>
<label>Age:</label>
<input type="number" name="age" placeholder="Enter Age" required="required" />
<label>Email:</label>
<input type="email" name="email" placeholder="Enter Email Address" required="required" />
<label>Image:</label>
<input type="file" name="file" required="required" /><br />
<input type="submit" name="submit" value="Submit" />
</form>
</fieldset>
</div>
</body>
</html>
当我在servlet的doPost()方法中使用类型param时,它可以正常工作,但是当此请求转发到servlet的doFilter()方法时,我收到NullPointerException。是因为POST方法吗? 我不明白为什么它不起作用,有人可以帮我解决吗?