File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes fiter help needed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "fiter help needed" Watch "fiter help needed" New topic
Author

fiter help needed

Naresh Chaurasia
Ranch Hand

Joined: May 18, 2005
Posts: 356
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.


SCJP 1.4, SCWCD1.4, OCA(1Z0-007)
Richard Green
Ranch Hand

Joined: Aug 25, 2005
Posts: 536
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


MCSD, SCJP, SCWCD, SCBCD, SCJD (in progress - URLybird 1.2.1)
Naresh Chaurasia
Ranch Hand

Joined: May 18, 2005
Posts: 356
yes,
BookShoppingServlet.class exists in the following path:

<Tomcat5>/webapps/<yourwebapp>/WEB-INF/classes/com/sams/learningweblogic7/servlets
Kiran Joshi
Ranch Hand

Joined: Sep 04, 2005
Posts: 54
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

Joined: Sep 04, 2005
Posts: 54
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

Joined: Oct 02, 2002
Posts: 121
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

Joined: Sep 04, 2005
Posts: 54
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

Joined: Dec 04, 2004
Posts: 950
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


Narendra Dhande
SCJP 1.4,SCWCD 1.4, SCBCD 5.0, SCDJWS 5.0, SCEA 5.0
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: fiter help needed