问个问题
有几十个页面要用户登入才能查看。怎么防止游客登入啊
[解决办法]
对
是Session
[解决办法]
过滤
[解决办法]
就是给你的应用程序配置Filter
web.xml
<filter>
<filter-name> Action Filter </filter-name>
<filter-class> com.yourcompany.ActionFilter </filter-class>
</filter>
<filter-mapping>
<filter-name> Action Filter </filter-name>
<url-pattern> *.do </url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name> Action Filter </filter-name>
<url-pattern> *.jsp </url-pattern>
</filter-mapping>
public class ActionFilter implements Filter
{
public void doFilter(ServletRequest arg0,ServletResponse arg1,FilterChain arg2) throws IOException,ServletException
{
HttpServletRequest request = (HttpServletRequest) arg0;
HttpServletResponse response = (HttpServletResponse) arg1;
if (request.getSession()== null)
{
if (request.getRequestURI().indexOf( "pathofyourpage ") == -1 )
response.sendRedirect(request.getContextPath() + "/sessionInvalid.htm ");
}
arg2.doFilter(arg0,arg1);
return;
}
public void destroy()
{
// TODO Auto-generated method stub
}
public void init(FilterConfig arg0) throws ServletException
{
}
}
[解决办法]
session
[解决办法]
用session或者用Struts 的Filter
[解决办法]
把这些页面的内容都放在IF中用SESSION来判断,如果SESSION为空说明没有登陆,显示错误信息,如果SESSION有值的话就正常显示改页面
如if(session==null){
显示错误信息
}
else{
显示页面信息
}
[解决办法]
用synchronized,给session加锁