Java 在筛选器类中填充JSP下拉列表
我想在JSP的下拉列表中填充数据库中的值。 问题是,我没有从任何servlet向这个JSP转发请求。我直接从URL打开这个JSPJava 在筛选器类中填充JSP下拉列表,java,jsp,servlets,jpa,Java,Jsp,Servlets,Jpa,我想在JSP的下拉列表中填充数据库中的值。 问题是,我没有从任何servlet向这个JSP转发请求。我直接从URL打开这个JSP http://mywebsite/thisJsp.jsp 现在我不想在JSP中使用任何Java代码,而是使用EclipseLink JPA。我所做的是创建了一个过滤器类 public class MyFilter implements Filter{ private FilterConfig filterConfig = null; @Overr
http://mywebsite/thisJsp.jsp
现在我不想在JSP中使用任何Java代码,而是使用EclipseLink JPA。我所做的是创建了一个过滤器类
public class MyFilter implements Filter{
private FilterConfig filterConfig = null;
@Override
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) request;
HttpServletResponse httpResponse = (HttpServletResponse) response;
String servletPath = httpRequest.getServletPath();
if(servletPath != null & servletPath.endsWith("thisJsp.jsp")) {
CatalogDB cdb = new CatalogDB();
List<Catalog> catalogs = cdb.getCatalogs();
request.setAttribute("catalogs", catalogs);
}
chain.doFilter(request, response);
}
@Override
public void destroy() {
}
}
公共类MyFilter实现过滤器{
private FilterConfig FilterConfig=null;
@凌驾
public void init(FilterConfig FilterConfig)抛出ServletException{
this.filterConfig=filterConfig;
}
@凌驾
public void doFilter(ServletRequest请求、ServletResponse响应、FilterChain链)抛出IOException、ServletException{
HttpServletRequest httpRequest=(HttpServletRequest)请求;
HttpServletResponse=(HttpServletResponse)响应;
字符串servletPath=httpRequest.getServletPath();
if(servletPath!=null&servletPath.endsWith(“thisJsp.jsp”)){
CatalogDB cdb=新建CatalogDB();
List catalogs=cdb.getCatalogs();
setAttribute(“目录”,目录);
}
链式过滤器(请求、响应);
}
@凌驾
公共空间销毁(){
}
}
现在我在web.xml中配置了这个过滤器
<filter>
<filter-name>myFilter</filter-name>
<filter-class>MyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>myFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
我的过滤器
我的过滤器
我的过滤器
/*
要求
向前地
问题是流进入过滤器并从数据库中获取值。它甚至可以在请求对象中设置它。但是在JSP上,我遇到了一个错误,无法找到“目录”。我很抱歉,伙计们,JSP中有一个小错误。 我在用
<c:forEach var="catalog" items="catalogs" >
而不是
<c:forEach var="catalog" items="${catalogs}" >