servlet filter

O

odelya

Hello,
I wrote the followint doFilter method:
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
if (filterConfig == null)
return;
HttpServletRequest request = (HttpServletRequest) arg0;
HttpServletResponse response = (HttpServletResponse) arg1;
String contentType = (String)
request.getAttribute("JPServletContentType");
if (contentType != null){
//response.setContentType(contentType);

}
String conType =
(String)filterConfig.getServletContext().getAttribute("JPServletContentType");
if (conType!=null) response.setContentType(conType);
String expires = (String) request.getAttribute("JPServletExpires");
if (expires != null)
response.setHeader("Expires", expires);
String cacheControl = (String)
request.getAttribute("JPServletCacheControl");
if (cacheControl != null)
response.setHeader("Cache-Control", cacheControl);
response.setContentType("text/plain");
long startTime = System.currentTimeMillis();
arg2.doFilter(request, response);
long stopTime = System.currentTimeMillis();
System.out.println("Time to execute request: " + (stopTime -
startTime) +
" milliseconds");



{
when i started the server, I saw a debug message :
2007-08-29 11:56:25,759 DEBUG [tomcat.localhost./servlet.Context]
Starting filter 'jpServletFilter'

But it doesn't seem to run the code, since I wrote the
System.out.print and id doesn't print to the log!

b
 
M

Manish Pandit

Hello,
I wrote the followint doFilter method:
public void doFilter(ServletRequest arg0, ServletResponse arg1,
when i started the server, I saw a debug message :
2007-08-29 11:56:25,759 DEBUG [tomcat.localhost./servlet.Context]
Starting filter 'jpServletFilter'

But it doesn't seem to run the code, since I wrote the
System.out.print and id doesn't print to the log!

b

Did you put the 'Starting filter..' message in the init() method ? Can
you paste the init() body ? You are supposed to keep the filterConfig
that you get in the init() method. Apparently the line where you check
filterConfig == null is true, and the rest of the method does not
execute as the filter returns. Put a log.info or system.out.println
there to be sure.

-cheers,
Manish
 
O

odelya

Hello,
I wrote the followint doFilter method:
public void doFilter(ServletRequest arg0, ServletResponse arg1,
when i started the server, I saw a debug message :
2007-08-29 11:56:25,759 DEBUG [tomcat.localhost./servlet.Context]
Starting filter 'jpServletFilter'
But it doesn't seem to run the code, since I wrote the
System.out.print and id doesn't print to the log!

Did you put the 'Starting filter..' message in the init() method ? Can
you paste the init() body ? You are supposed to keep the filterConfig
that you get in the init() method. Apparently the line where you check
filterConfig == null is true, and the rest of the method does not
execute as the filter returns. Put a log.info or system.out.println
there to be sure.

-cheers,
Manish
Hi,
I still have a problem:
I wrote in the jsp file:
<input name='JPServletContentType' type='hidden' value='text/plain'>
and in the filter:
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) {
.....some code...
HttpServletRequest request = (HttpServletRequest) arg0;
HttpServletResponse response = (HttpServletResponse) arg1;
String contentType = (String)
request.getAttribute("JPServletContentType");
if (contentType != null) {
response.setContentType(contentType);
System.out.println(contentType);
} else
System.out.println("noContentType");
but the variable contentType returns null.
so i tried:

String conType = (String)
filterConfig.getServletContext().getAttribute("JPServletContentType");
if (conType != null)
{
response.setContentType(conType);
System.out.println(conType);

} else
System.out.println("noContentType2");

and still the conType returned null.
What's the way to get an attribut and send it?

Thank you!
 
M

Manish Pandit

<input name='JPServletContentType' type='hidden' value='text/plain'>
and in the filter:
request.getAttribute("JPServletContentType");

JPServletContentType will come in as a request Parameter, not request
Attribute. That is why you're seeing a null. Why are you looking for
it in ServletContext ? If you feel you are unclear on the scopes
(request/context/session/page), attribute vs. parameters, read the
Sun's JSP/Servlet tutorial which can give you a nice overview.

Try request.getParameter("JPServletContentType") instead of
request.getAttribute(..) and you should see 'text/plain' as you set in
the JSP.

-cheers,
Manish
 
O

odelya

JPServletContentType will come in as a request Parameter, not request
Attribute. That is why you're seeing a null. Why are you looking for
it in ServletContext ? If you feel you are unclear on the scopes
(request/context/session/page), attribute vs. parameters, read the
Sun's JSP/Servlet tutorial which can give you a nice overview.

Try request.getParameter("JPServletContentType") instead of
request.getAttribute(..) and you should see 'text/plain' as you set in
the JSP.

-cheers,
Manish

Hi,
I did it as you mentioned.
But it doesn't help, it still get the null.
Is there a way to get the whole html text that is sent to the server
and search in it for values?

Thank you!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top