大家好我正在开发一个servlet,它使用以前存储的值来丢失参数。如果用户第一次访问该页面并且未输入任何值,则显示参数值的错误;我的问题是当值为null时它不显示错误。其次,我的cookie存储以前的值一次。例如,如果我输入缺失值,它将显示先前输入的值,但如果我连续执行,则第二次显示它将显示null。这是我的代码:
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Cookie cookie1,cookie2,cookie3;
String getfirst=request.getParameter("first");
String getlast=request.getParameter("last");
String getemail=request.getParameter("email");
cookie1 = new Cookie("firstname", getfirst);
cookie2 = new Cookie("lastname", getlast);
cookie3 = new Cookie("email", getemail);
cookie1.setMaxAge(3600);
cookie2.setMaxAge(3600);
cookie3.setMaxAge(3600);
response.addCookie(cookie1);
response.addCookie(cookie2);
response.addCookie(cookie3);
PrintWriter out=response.getWriter();
Cookie[] cookies = request.getCookies();
if (cookies!=null) {
Cookie ck;
for (int i = 0; i < cookies.length; i++) {
ck = cookies[i];
out.println(ck.getName() + " "
+ ck.getValue());
}}
else{
out.println("Unknown value");
}