使用servlet技术重写HttpSession Url无法正常工作

时间:2015-12-17 08:58:18

标签: java jsp session servlets url-rewriting

我想使用http会话url重写技术维护servlet7和servlet8的会话值,但是会话在tomcat服务器中不能使用以下代码。

请求Flow1: - httpsessionurlrewriting.jsp ---> servlet7.java

请求流程2: - sendmorevalues1.jsp -----> servlet8.java ----> AllItems.java

我想在会话中维护servlet7和servlet8值,并使用session url rewriting tehnique读取Allitems.java中的会话值

这是我的主页App /SessionHandling/WebContent/HttpSessionUrlRewritingMain.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Http url rewriting technique</title>
</head>
<body>
<h1>Homepage1 sending request to servlet7 and setting attributes to session object</h1>
	<form action="Servlet7" method="get">
	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 servlet7">	
	</form>
</body>
</html>

Servlet7.java

public class Servlet7 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();
writer.print("<h1>Servlet 7 home page</h1>");
session.setAttribute("rollnum", num);
session.setAttribute("name", name);

int maxInactiveInterval = session.getMaxInactiveInterval();
writer.println("session expire time" + maxInactiveInterval + "  session id  " + session.getId()+"<br>");
session.setMaxInactiveInterval(-25);
writer.println("session expire time" + maxInactiveInterval + "  session id  " + session.getId()+"<br>");
response.encodeRedirectURL("Servlet8");
writer.println("<br><a href='Servlet8'>Click here to add more values to cart</a>");
writer.close();

第二个请求主页: - / SessionHandling / WebContent / SendMoreValues1.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Http url rewriting technique</title>
</head>
<body>
<h1>Homepage2 sending request to servlet8 and setting attributes to session object</h1>
	<form action="Servlet8" method="get">
	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 servlet8">	
	</form>
</body>
</html>

Servlet8.java

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

} catch (Exception e) {
    writer.print("session object not found by container, session id not be received from request");
}
}
}

Allitems.java

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("<h4>" + string + " :  " + session.getAttribute(string).toString() + "</h4>");
    }
    writer.print("all values are retrieved");
//    session.invalidate();
    writer.print("session object inavlidated/destroyed");
    writer.close();
} catch (Exception e) {
    writer.print(" old session object not found... ");
    }

}

}

我正在使用eclipse Ide和apache tomcat server8,

0 个答案:

没有答案