Results 1 to 4 of 4
  1. #1
    oszc is offline Member
    Join Date
    Aug 2011
    Posts
    20
    Rep Power
    0

    Unhappy help with request$dispatcher problem in jsp custom tag

    hi,everyone
    I write a custom jsp tag to decide weather to display the content below, which depends on the html header property "Referer" .
    Java Code:
    package com.zc.tag;
    import java.io.IOException;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import javax.servlet.jsp.*;
    import javax.servlet.jsp.tagext.*;
    
    public class RefererDemo extends SimpleTagSupport {
    	
    	private String site;
    	private String adPage;
    	public void setSite(String site) {
    		this.site = site;
    	}
    	public void setAdPage(String adPage) {
    		this.adPage = adPage;
    	}
    	@Override
    	public void doTag() throws JspException, IOException {
    		PageContext pc = (PageContext)getJspContext();
    		HttpServletRequest request = (HttpServletRequest)pc.getRequest();
    		HttpServletResponse response = (HttpServletResponse)pc.getResponse();
    		String refererValue = request.getHeader("Referer");
    		if(refererValue==null||!refererValue.startsWith(site)){
    			try {
    				request.getRequestDispatcher(adPage).forward(request, response);
    				throw new SkipPageException();
    			} catch (ServletException e) {
    				e.printStackTrace();
    			}
    		}
    	}
    }
    Java Code:
    <%@page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%@taglib uri="http://oszc.dyndns.info/heima" prefix="mytag" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <mytag:RefererDemo adPage="/1.html" site="http://127.0.0.1"/> 
    <!-- if the adPage linked to a html file err message will be displayed -->
    <!-- java.lang.IllegalStateException: getOutputStream() has already been called for this response -->
    
    <!-- but the adPage change to "somejspfile.jsp" like below then request$dispatch works perfect-->
    <!--  mytag:RefererDemo adPage="/ad.jsp" site="http://127.0.0.1"/ --> 
    <html> 
      <head>
        
        <title>My JSP 'referTest1.jsp' starting page</title>
    
      </head>
      <body>
        test!!!
      </body>
    </html>
    Java Code:
    java.lang.IllegalStateException: getOutputStream() has already been called for this response
    	at org.apache.catalina.connector.Response.getWriter(Response.java:611)
    	at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:198)
    	at org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:125)
    	at org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:118)
    	at org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:188)
    	at org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:118)
    	at org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:77)
    	at org.apache.jsp.referTest1_jsp._jspService(referTest1_jsp.java:85)
    	at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    	at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    	at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388)
    	at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
    	at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
    	at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:291)
    	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
    	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
    	at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    	at java.lang.Thread.run(Thread.java:619)

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default Re: help with request$dispatcher problem in jsp custom tag

    You cannot redirect when the JSP is already being sent.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    oszc is offline Member
    Join Date
    Aug 2011
    Posts
    20
    Rep Power
    0

    Default Re: help with request$dispatcher problem in jsp custom tag

    Quote Originally Posted by Tolls View Post
    You cannot redirect when the JSP is already being sent.
    hi,Tolls

    As i describe in the html annotation, when redirect to a jsp file there is no error , But the exception was thrown when i redirect to a html file,
    I cannot figure out what's the different between jsp and html files when the container execute redirect code ?
    Last edited by oszc; 07-09-2012 at 11:35 AM.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default Re: help with request$dispatcher problem in jsp custom tag

    Oh sorry, I thought that was a redirect, not a forward.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Dispatcher
    By abhinav_sharma in forum New To Java
    Replies: 2
    Last Post: 04-24-2012, 05:41 PM
  2. Replies: 2
    Last Post: 03-29-2012, 10:32 AM
  3. Get Request Problem In Android
    By Ya2 in forum Android
    Replies: 7
    Last Post: 07-26-2011, 08:25 PM
  4. Replies: 2
    Last Post: 02-27-2011, 10:50 AM
  5. The Struts dispatcher cannot be found
    By Md Saim in forum Web Frameworks
    Replies: 0
    Last Post: 12-16-2008, 06:37 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •