这是我的servlet LOgout程序我尝试重设Cookie的位置
package trainapp;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
@SuppressWarnings("serial")
public class LogoutServlet extends HttpServlet {
@Override
public void doGet(HttpServletRequest req,HttpServletResponse res) throws IOException, ServletException
{
PrintWriter pw=res.getWriter();
res.setContentType("text/html");
pw.println("<body style=color:#A93226;background-color:#FDF2E9>");
Cookie ck[]=req.getCookies();
ck[0].setValue("");
ck[0].setMaxAge(0);
pw.println("Logged Out Sucessfully..<br>");
RequestDispatcher rd=req.getRequestDispatcher("home.html");
rd.include(req, res);
}
}
web.xml文件这是xml文件的一部分
<servlet>
<servlet-name>LogoutServlet</servlet-name>
<servlet-class>trainapp.LogoutServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LogoutServlet</servlet-name>
<url-pattern>/logout</url-pattern>
</servlet-mapping>
这是我的html文件html代码部分,其中我使用超链接用于注销servlet
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<a href="view2">View Trains</a>
<a href="/logout">Logout</a>
</body>
</html>
只需替换以下内容
<a href="/logout">Logout</a>
with
<a href="logout">Logout</a>
更新:
如果未成功,请从一个最小的可行示例开始,例如使用下面给出的代码更改您的servlet,并检查其是否有效。
public class LogoutServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<h1>Hello World!</h1>");
}
}
如果可行,则开始调查您的代码以找到根本原因。