在form action中滥用绝对路径导致session的attribute丢失(无法正常保存)

症状:

  刚才在做一个利用session的attribute保存用户的id的实验,login.jsp输入用户id,提交给LoginServlet去数据库验证,然后LoginServlet根据验证情况跳转到相应的页面。

  但是发现,LoginServlet中使用了HttpServletRequest.getSession().setAttribute("uid", user_id)却无法成功保存已登录的用户的id,比如说:在welcome.jsp会根据session中

  保存的uid attribute显示一些用户特定的欢迎信息,但是welcome.jsp中使用session.getAttribute("uid")却始终返回的是null

  在此期间没有关闭浏览器、更没有重新启动服务器,经过简单的debug,session.isNew()返回的是false,但是session.getAttribute("uid")却始终返回null

分析:

  分析过程见http://www.cnblogs.com/qrlozte/p/3515881.html

  原因是login.jsp中提交到LoginServlet的action写法有误,不应该使用绝对路径,但是为什么使用绝对路径提交form会造成session的attribute无法正常保存我暂时还不清楚

  错误的action路径:<form action="http://localhost:8080/site/xxx/LoginServlet">

解决方案:

  action改为相对路径即可(server.xml配置的path="/site", docBase="X:\...\WebCotent")

  <form action="/site/xxx/LoginServlet">

为了具体化我遇到的问题,我将简化后的代码列举如下:

/site/xxx/login.jsp

  (注意这里的action使用的是绝对路径,你会发现,提交之后,uid并没有保存到session中(你可以在不关闭浏览器的情况下刷新login.jsp,你可以看到request.getSession().getAttribute("uid")返回null),而且session也没有被销毁(你可以用session.isNew()验证),怎么解决?action改为"/site/xxx/LoginServlet"或者"LoginServlet"即可)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="http://localhost:8080/site/xxx/LoginServlet" method="post">
    attr:<input type="text" name="uid"><br>
    <input type="submit" value="提交"><input type="reset" value="重置">
</form>
<%=request.getSession().getAttribute("uid") %>
</body>
</html>

/site/xxx/yyy/welcome.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <%=request.getSession().getAttribute("uid") %>
</body>
</html>

/site/xxx/LoginServlet

public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        req.getSession().setAttribute("uid", req.getParameter("uid"));
        req.getRequestDispatcher("yyy/welcome.jsp").forward(req, resp);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        doGet(req, resp);
    }
}

web.xml

<servlet>
    <servlet-name>loginServlet</servlet-name>
    <servlet-class>
        org.foo.servletdemo.LoginServlet
    </servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>loginServlet</servlet-name>
    <url-pattern>/xxx/LoginServlet</url-pattern>
</servlet-mapping>

 

posted @ 2014-01-12 13:09  rldts  阅读(4080)  评论(0编辑  收藏  举报