日期:2014-05-17  浏览次数:21263 次

使用filter,servlet无法收到传值
filter



package Web.filter;

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 Web.utils.LoginInfo;


public class login implements Filter{

public void destroy() {


}

public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest)request;
HttpServletResponse res=(HttpServletResponse)response;
HttpSession session=req.getSession();
LoginInfo login=(LoginInfo) session.getAttribute("login");
String requestURI = req.getRequestURI().substring(
req.getRequestURI().indexOf("/", 1),
req.getRequestURI().length());
System.out.print("1");
if(!requestURI.equals("/login.html")){
if(login==null){
res.sendRedirect(req.getContextPath()+"/login.html");
return;
}
}
chain.doFilter(request, response);
}
public void init(FilterConfig arg0) throws ServletException {


}

}




servlet


package Web.Servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import Web.DAO.ManDAO;
import Web.utils.LoginInfo;

public class checkServlet extends HttpServlet {

@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("gb2312");
response.setCharacterEncoding("gb2312");
PrintWriter pw = response.getWriter();
String username = request.getParameter("user").toString();
String passwordname = request.getParameter("password").toString();
HttpSession session = request.getSession();
  System.out.println(username);

boolean b = ManDAO.checkuser(username,passwordname);
if(b == true){
LoginInfo login=ManDAO.findAllinfo(username);
session.setAttribute("login", login);
response.sendRedirect("ManInterface.html");
}


}

}






------解决方案--------------------
如果servlet中没有service方法
一定要doPost和doGet方法写全了,然后在一个方法中调用另一个方法
form表单如果不写method=post,默认是get方式的