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

时间:2015-12-16 07:33:34

标签: java session servlets session-state

Servlet5和servlet6是从各个html页面给出的单独请求,AllItems.java是最终的servlet,它没有显示servlet5的所有sesssion属性为什么?

这是此应用程序的主页,请求从此处转到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页面的两个属性添加到sesssion

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());
    }

}
}
}

1 个答案:

答案 0 :(得分:0)

输入类型参数名称设置错误,使其正常工作

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>