Servlet cookie无法正常工作

时间:2013-09-19 19:21:06

标签: java servlets cookies

我有一个登录servlet,我接收电子邮件和密码并将它们引导到logincheck servlet。我已经设计了我的logincheck servlet,这样如果提交表单,每个字段都填满,cookie就会被设置为相应的输入值,但如果电子邮件和密码字段留空,则cookie值设置为空白,我们会被定向回到登录servlet。

当我提交保留未填写的表单而不是重定向到登录页面时,我将被重定向到错误页面,其中包含描述“请求的源不可用”。请帮助。

package newpackage;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class logincheck extends HttpServlet {

  protected void processRequest(HttpServletRequest request, HttpServletResponse  response)
        throws ServletException, IOException {


    //recieving form parameters
    String email=request.getParameter("email");
    String pass=request.getParameter("pass");

    //setting up cookies as a white field  if any credential has been left unfilled
    Boolean isMissingValue=false;
    if(email==null || email.trim().equals("")){
        email="";                                   //return white field as a cookie if the user doesnt enter anything and submits
        isMissingValue=true;
    }
    if(pass==null || pass.trim().equals("")){
        pass="";                                    //return white field as a cookie if the user doesnt enter anything and submits    
        isMissingValue=true;
    }

    //setting up cookies based on what the user entered last
    Cookie c1=new LongLivedCookie("email",email);
    response.addCookie(c1);
    Cookie c2=new LongLivedCookie("pass",pass);
    response.addCookie(c2);
    if(isMissingValue){
        RequestDispatcher obj=request.getRequestDispatcher("login.java");
        obj.forward(request,response);
    }
}
}

这是我的LongLivedCookie课程

package newpackage;

import javax.servlet.http.Cookie;

public class LongLivedCookie extends Cookie {

public static final int seconds_per_year=60*60*25*365;

public LongLivedCookie(String name, String value) {
    super(name,value);
    setMaxAge(seconds_per_year);
}
}

2 个答案:

答案 0 :(得分:1)

您没有正确实例化RequestDispather

RequestDispatcher obj=request.getRequestDispatcher("login.java");

RequestDispatcher的java文档说明了它

  

定义一个对象,该对象接收来自客户端的请求并将它们发送到服务器上的任何资源(例如 servlet,HTML文件或JSP文件)。

因此,传递login.java作为目标资源是不正确的。它希望您提供现有servlet或JSP资源的正确路径。例如,如果您的Login页面是JSP转发

RequestDispatcher view = request.getRequestDispatcher("login.jsp");
view.forward(request, response);

对于servlet,RequestDispatcher需要正确的URL模式。因此,如果您的Login页面是servlet,请检查<url-pattern>中指定的web.xml并转发为

RequestDispatcher view = request.getRequestDispatcher("/Login");

另请注意,可以从RequestDispatcherServletRequest检索a ServletContext,区别在于前者可以采用 相对路径

参考
http://docs.oracle.com/javaee/5/api/javax/servlet/RequestDispatcher.html

答案 1 :(得分:0)

根据您的代码和说明,我假设您期望将cookie值作为请求参数。但是cookie不能以这种方式工作。要获取cookie值,您必须使用HttpRequest.getCookies()方法并读取值。

Cookies[] cookies = request.getCookies();
for (Cookie cookie : cookies) {
    if (cookie.getName().equals("email")) {
         ;//logic here
    } else if (cookie.getName().equals("pass")) {
         ;//logic here.
    }
}