• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

fiter help needed

 
Naresh Chaurasia
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have following code .....

-----filter----------

package com.sams.learnweblogic7.servlets;


public class DemonstrateFilters implements Filter {

private FilterConfig filterConfig;

public DemonstrateFilters() {
log("DemonstrateFilters constructer invoked ...");
}

public void init(FilterConfig filterConfig)throws ServletException {
this.filterConfig = filterConfig;
}

public void destroy() {
this.filterConfig = null;
}


public void doFilter(ServletRequest req, ServletResponse res, FilterChain fc)
throws java.io.IOException, javax.servlet.ServletException {
log("The doFilter method has been invoked.");
fc.doFilter(req,res);
}

public FilterConfig getFilterConfig() {
return filterConfig;
}


public void setFilterConfig(FilterConfig cfg) {
filterConfig = cfg;
}


public void log(String s) {
System.out.println("[loginFilter]: " + s);
}

}

----------servlet----------------

package com.sams.learnweblogic7.servlets;



public class BookShoppingServlet extends HttpServlet{
public void init(ServletConfig sc)throws ServletException{
super.init(sc);
}

public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException{


PrintWriter out = res.getWriter();
String name = req.getParameter("name");
System.out.println("hello");

}

}

--------web.xml------------

<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">


<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">


<description>
JSP 2.0 Examples.
</description>
<display-name>JSP 2.0 Examples</display-name>




<filter>
<filter-name>DemonstrateFilters</filter-name>
<filter-class>com.sams.learnweblogic7.servlets.DemonstrateFilters</filter-class>
</filter>

<filter-mapping>
<filter-name>DemonstrateFilters</filter-name>
<url-pattern>/BookShoppingServlet</url-pattern>
</filter-mapping>

<servlet>
<servlet-name>BookShoppingServlet</servlet-name>
<servlet-classes>com.sams.learningweblogic7.servlets.BookShoppingServlet</servlet-classes>
</servlet>

</web-app>



---------------------------------------------------------------

i am using the following url : http://localhost:8080/filters/BookShoppingServlet to call the servlet, but getting the following error : The requested resource (Servlet BookShoppingServlet is not available) is not available.


Any help will be appreciated.

thnx.
 
Richard Green
Ranch Hand
Posts: 536
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
did you deploy the filter to your webapp?

in other words, does the filter (BookShoppingServlet.class) exists in the following path:

<Tomcat5>/webapps/<yourwebapp>/WEB-INF/classes/com/sams/learningweblogic7/servlets
 
Naresh Chaurasia
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes,
BookShoppingServlet.class exists in the following path:

<Tomcat5>/webapps/<yourwebapp>/WEB-INF/classes/com/sams/learningweblogic7/servlets
 
Kiran Joshi
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess the way to achieve filtering is in ur filter mapping in the Deployment Descriptor,add <servlet-name> element & give ur servlets name there.
Then use the url of ur servlets as if u would use it in the absence of filter.the webserver will automatically introduce the filter there.

Experts plz correct me if I'm wrong.
 
Kiran Joshi
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
More on this

Associate ur filter with the servlet like this

<filter-mapping>
<filter-name> urfiltername </filter-name>
<servlet-name> urservletname </servlet-name>
</filter-mapping>

then use the url for the servlet directly
like ..../servlet/urservlet

experts plz correct me if I'm wrong.
 
Sub swamy
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) In the initial posting there is no <servlet-mapping> tag. How would the container be able to map the request to the servlet.

2) If we user <servlet-name> instead of <url-pattern> in the <filter-mapping> tag, would that suffice ? Dont we need to explicitly have a <servlet-mapping> tag irrespective of whether we use a filter or not ?

I tried specifying <servlet-name> in <filter-mapping> tag without a <servlet-mapping> tag in web.xml - it didnt work - got the same exception - "Request resource not found."
 
Kiran Joshi
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Subramanian

the things I hv given for filter are only the addition to the normal web.xml settings tht r required for the normal servlet without filter.

u'll still require the servlet related elements present in the web.xml like
<servlet-mapping> explicitely irrespective of whether we use a filter or not as u said.

configure ur serlvet for just as in the normal case as if theres no filter,access it like tht way only.
but add the filter related things tht I hv mentioned above.

I think this should work.
 
Narendra Dhande
Ranch Hand
Posts: 951
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I think

<servlet-mapping>
<servlet-name>BookShoppingServlet</servlet-name>
<url-pattern>/BookShoppingServlet</url-pattern>
</servlet-mapping>

shuld added to web.xml to access the servlet first. I am not using weblogic, bu it must true for it. Filter mapping to servlet or URL is next step.

thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic