我试图制作一个servlet,并试图覆盖我的doGet
方法。 @Override doGet
中存在错误
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
这是在processRequest下的错误来了。具体的错误消息是:
The method processRequest(HttpServletRequest, HttpServletResponse) is undefined for the type
CookieUsageServlet
有人知道我为什么会收到此错误吗?
package ExamplePackage;
import java.io.IOException;
import java.util.Date;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/CookieUsageServlet")
public class CookieUsageServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Cookie cookie = getCookie(request, response);
printResponse(response, cookie);
}
private Cookie getCookie(HttpServletRequest request, HttpServletResponse response) {
Cookie cookie = null;
Cookie[] cookiesArray = request.getCookies();
if(cookiesArray != null) {
for(int x = 0; x < cookiesArray.length; x++) {
if(cookiesArray[x].getName().equals("test_cookie")) cookie = cookiesArray[x];
}
}
if(cookie != null) {
System.out.println("cookie retrieved");
} else {
int timeToExpireCookie = new Integer(getServletContext().getInitParameter("time-to-expire-cookie")).intValue();
cookie = new Cookie("test_cookie", Long.toString(new Date().getTime()));
cookie.setMaxAge(timeToExpireCookie);
cookie.setPath(request.getContextPath()); // Sti til login
System.out.println("Cookie created...");
response.addCookie(cookie);
}
return cookie;
}
private void printResponse(HttpServletResponse response, Cookie cookie) throws IOException {
PrintWriter printWriter = response.getWriter();
response.setContentType("text/html");
printWriter.println("<html>");
printWriter.println("<head><title>Cookie In JAVA servlet</title></head>");
printWriter.println("<body>");
printWriter.println("<cookie name: " + cookie.getName() + "<br>");
printWriter.println("<cookie value: " + cookie.getValue() + "<br>");
printWriter.println("<time to expire cookie in seconds: " + cookie.getMaxAge() + "<br>");
printWriter.println("</body>");
printWriter.println("</html>");
}
}
最佳答案
根据例外,getServletContext().getInitParameter("time-to-expire-cookie")
的值为null
。