我有一个登录 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);
}
}