如何在过滤器Filter.java中获取jsp页面文本框的值?
4个回答
展开全部
1、在Jsp页面将文本框输入域放在html表单form之中提交
2、后台定义一个过滤器继承Filter.java
3、在过滤器中的doFilter方法中,将参数ServletRequest转化为HttpServletRequest
4、利用HttpServletRequest.getParameter();方法即可获取对应输入域的value值。
示例:
在Jsp页面:
<form>
<input type="text" name="username" value="zhangsan"/>
</form>
后台过滤器:
public class TestFilter implements Filter{
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain filter) throws IOException, ServletException {
HttpServletRequest res = (HttpServletRequest) request;
//获取页面的username输入框的值
String username = request.getParameter("username");
return;
}
@Override
public void init(FilterConfig arg0) throws ServletException {
}
}
2、后台定义一个过滤器继承Filter.java
3、在过滤器中的doFilter方法中,将参数ServletRequest转化为HttpServletRequest
4、利用HttpServletRequest.getParameter();方法即可获取对应输入域的value值。
示例:
在Jsp页面:
<form>
<input type="text" name="username" value="zhangsan"/>
</form>
后台过滤器:
public class TestFilter implements Filter{
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain filter) throws IOException, ServletException {
HttpServletRequest res = (HttpServletRequest) request;
//获取页面的username输入框的值
String username = request.getParameter("username");
return;
}
@Override
public void init(FilterConfig arg0) throws ServletException {
}
}
展开全部
楼上的说的对但不详细,这是源码,其实就是进行一下类型转换!
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
// TODO Auto-generated method stub
//请求的强制转换
HttpServletRequest request=(HttpServletRequest)arg0;
//响应的强制转换
HttpServletResponse response=(HttpServletResponse)arg1;
//转换完后就像在Servlet中的获取方式相同了
//例: String str=request.getParameters("xxx");
}
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
// TODO Auto-generated method stub
//请求的强制转换
HttpServletRequest request=(HttpServletRequest)arg0;
//响应的强制转换
HttpServletResponse response=(HttpServletResponse)arg1;
//转换完后就像在Servlet中的获取方式相同了
//例: String str=request.getParameters("xxx");
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
过滤器也是继承于servlet 可通过表单提交方式,或url传值 ,拿到文本框的值
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
System.out.println("登录检查过滤器开始工作。。。");
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response;
不过要将 ServletRequest 强制转换为 HttpServletRequest才可以拿到提交过来的值。。
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
System.out.println("登录检查过滤器开始工作。。。");
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response;
不过要将 ServletRequest 强制转换为 HttpServletRequest才可以拿到提交过来的值。。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
HttpServletRequest request = (HttpServletRequest) req
String x=req.getParameters("xxx");
String x=req.getParameters("xxx");
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询