• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

fiter help needed

 
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 536
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes,
BookShoppingServlet.class exists in the following path:

<Tomcat5>/webapps/<yourwebapp>/WEB-INF/classes/com/sams/learningweblogic7/servlets
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 951
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
An elephant? An actual elephant. Into the apartment. How is the floor still here. Hold this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic