这个java Web应用程序像无限循环一样运行。谁能告诉我原因?需要过滤每个请求here.allowed可以使用system.other用户应该去登录页面留言..请帮我这样做。
FilterRequest.java
package com.mobitel.bankdemo.web;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.mobitel.bankdemo.domain.User;
public class FilterRequest implements Filter{
FilterConfig filterConfig = null;
public FilterRequest() {
super();
}
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
}
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) resp;
System.out.println("Inside the filter.............." );
HttpSession session = request.getSession(true);
User u = null;
if(session.getAttribute("loggedUser")!=null){
u = (User) session.getAttribute("loggedUser");
}
if (u!= null)
{
System.out.println("user does exits.." + u.getUname() );
chain.doFilter(req, resp);
}else{
String message = "Please Login!";
req.setAttribute("loginMsg", message);
response.sendRedirect("login2.jsp");
}
}
public void destroy() {
// do cleanup stuff
}
}
web.xml过滤器映射
<filter>
<filter-name>FilterRequest</filter-name>
<filter-class>com.mobitel.bankdemo.web.FilterRequest</filter-class>
</filter>
<filter-mapping>
<filter-name>FilterRequest</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
提前谢谢
答案 0 :(得分:0)
我不是java专家,只是查看你的代码我只想说两件事
(1)考虑过滤器是否从请求中获取用户名
if(session.getAttribute("loggedUser")!=null){
u = (User) session.getAttribute("loggedUser");
}
if (u!= null)
{
System.out.println("user does exits.." + u.getUname() );
chain.doFilter(req, resp);
它会再次进入子程序或方法来过滤其他东西但是在进入这个方法之后你正在进行user=null
并且由于这个原因,如果我们在之前已整理出来,它将再次进入相同的状态
是
if (u!= null)
{
System.out.println("user does exits.." + u.getUname() );
chain.doFilter(req, resp);
所以在你的方法之外定义User u = null。可能会解决你的问题
答案 1 :(得分:0)
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) resp;
System.out.println("Inside the filter.............." );
String loginUrl= request.getContextPath() + "/login2.jsp";//url for login page
String uri = request.getRequestURI().toString();
if (uri.endsWith(loginUrl)){// if uri is login page then no need to check if login, just process the chain
chain.doFilter();
}
else{ HttpSession session = request.getSession(true);
User u = null;
if(session.getAttribute("loggedUser")!=null){
u = (User) session.getAttribute("loggedUser");
}
if (u!= null)
{
System.out.println("user does exits.." + u.getUname() );
chain.doFilter(req, resp);
}else{
String message = "Please Login!";
req.setAttribute("loginMsg", message);
response.sendRedirect("login2.jsp");
return;
}
}
}