request1 的会话值不可用,同时在最终 servlet 中生成响应

session values of request1 are not available, while generating response in final servlet

Servlet5 和 servlet6 从单独的 html 页面获得单独的请求,AllItems.java 是最终的 servlet,为什么不显示 servlet5 的所有会话属性??。

这是此应用程序的主页,请求从这里转到 servlet5.java

HttpSessionTechniquesMain.html
]<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Cookie mechanism</title>
</head>
<body>
 <form action="Servlet5" method="post">
 Enter rollno<input type="text" name ="rn"> <br>
 Enter name<input type="text" name="nm"><br>
 <input type="submit" value="send rn and nm to servlet5"> 
 </form>
</body>
</html>

Servlet5.java 将 html 页面的两个属性添加到会话

package com.sessionhandling.httpsessionmechanism;  
public class Servlet5 extends HttpServlet {
private static final long serialVersionUID = 1L;`
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String num = request.getParameter("no");
String name = request.getParameter("name");
HttpSession session = request.getSession();
response.setContentType("text/html");
PrintWriter writer = response.getWriter();
session.setAttribute("num", num);
session.setAttribute("name", name);

int maxInactiveInterval = session.getMaxInactiveInterval();
writer.println("session expire time" + maxInactiveInterval + "  session id  " + session.getId());
session.setMaxInactiveInterval(10);
writer.println("<a href='SendMoreValues.html'>Click here to add more values to cart</a>");
writer.close();
}

}

正在将新请求从 SendMoreValues.html 发送到 Servlet6.java

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
 <form action="Servlet6" method="post">
 Enter email<input type="text" name ="eml"> <br>
 Enter pswd<input type="text" name="pw"><br>
 <input type="submit" value="send eml and pwd to servlet6"> 
 </form>

</body>
</html>

Servlet6.java
public class Servlet6 extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String email = request.getParameter("eml");
String pswd = request.getParameter("pw");
HttpSession session = request.getSession(true);
session.setAttribute("email", email);
session.setAttribute("password", pswd);
session.setAttribute("password", "password value is overrided by other request");
response.setContentType("text/html");
PrintWriter writer = response.getWriter();
int maxInactiveInterval = session.getMaxInactiveInterval();
writer.println("session timout time"+maxInactiveInterval+"  sesssion id"+ session.getId()); 
writer.println("<a href='AllItems'>Click here to display all cart values from Allitems servlet</a>");
writer.close();
}
}

最后一个 servlet 读取添加到 servlet5 和 servlet6 的所有属性值,但是 servlet5 属性没有显示或检索...为什么??

public class AllItems extends HttpServlet {
private static final long serialVersionUID = 1L;
private PrintWriter writer;
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {

    HttpSession session = request.getSession(false);
    response.setContentType("text/html");
    writer = response.getWriter();
    Enumeration<String> names = session.getAttributeNames();
    while (names.hasMoreElements()) {
    String string = names.nextElement();
    writer.println(string + " :  " + session.getAttribute(string).toString());
    }

    writer.close();
} catch (Exception e) {
    writer.print(" old session object not found... ");
    HttpSession session = request.getSession(true);
    response.setContentType("text/html");
    writer = response.getWriter();
    Enumeration<String> names = session.getAttributeNames();
    while (names.hasMoreElements()) {
    String string = names.nextElement();
    writer.println(string + " :  " + session.getAttribute(string).toString());
    }

}
}
}

输入类型参数名称哪里设置错了,改正了就可以了

HttpSessionTechniquesMain.html
]<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Cookie mechanism</title>
</head>
<body>
 <form action="Servlet5" method="post">
 Enter rollno<input type="text" name ="no"> <br>
 Enter name<input type="text" name="name"><br>
 <input type="submit" value="send rn and nm to servlet5"> 
 </form>
</body>
</html>