Solved

How can we configure that all the request should pass through dispatcher servlet except a single request which goes to a servlet defined in web.xml.

Posted on 2013-02-05
13
1,051 Views
Last Modified: 2013-11-11
Hi,

How can we configure that all the request should pass through dispatcher servlet except a single request which goes to a servlet defined in web.xml.

I have configured in web.xml file the below code which dispatches the request to the spring dispatcher servlet.

<servlet-mapping>
&#9;&#9;<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
&#9;&#9;<url-pattern>/</url-pattern>
&#9;</servlet-mapping>


Below is the servlet code which i want to receive a particular request

<servlet-mapping>
&#9;&#9;<servlet-name>CrystalReportViewerServlet</servlet-name>
&#9;&#9;<url-pattern>/CrystalReportViewerHandler</url-pattern>
&#9;</servlet-mapping>
0
Comment
Question by:Rocking
  • 3
  • 2
  • 2
  • +2
13 Comments
 
LVL 7

Expert Comment

by:gudii9
ID: 38856964
I believe Servlet Filter can help you in this scenario.
0
 
LVL 35

Expert Comment

by:mccarl
ID: 38857531
I believe the above configuration that you have posted should work. Is it not? Is there any error messages or further detail that you are able to provide us?
0
 
LVL 20

Expert Comment

by:Sathish David Kumar N
ID: 38858372
<servlet-mapping>
            <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
                <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <url-pattern>/</url-pattern>
      </servlet-mapping>

Open in new window


Add servlet class it will work
0
 

Author Comment

by:Rocking
ID: 38860373
what i actually doing is configuring the crystal report in spring.
Whole application is working fine with the correct entry for dispatcher servlet. the url is something like below

http://localhost:8080/projectname/

but when i click on the crystal report viewer which handles the servlet "CrystalReportViewerServlet" the url is like below.

http://localhost:8080/CrystalReportViewerServlet

The problem is when Report call the servlet CrystalReportViewerServlet, the spring dispacher don't find it so how I can make spring dispacher to call a default servlet that is declare on my Web.xml.

what i need is DispatcherServlet to redirect to the CrystalReportViewerServlet.




Web.xml file is given below





<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

      <display-name>GMCS Web Application</display-name>

      <!-- - Loads the root application context of this web app at startup. -
            The application context is then available via - WebApplicationContextUtils.getWebApplicationContext(servletContext). -->

      <context-param>
            <param-name>crystal_image_uri</param-name>
            <param-value>resources/crystalreportviewers</param-value>
      </context-param>
      
                  <context-param>
            <param-name> </param-name>
            <param-value>/CrystalReportViewerHandler</param-value>
            </context-param>
 <!--       <context-param>
            <param-name>crystal_image_use_relative</param-name>
            <param-value>webapp</param-value>
      </context-param>
 -->
      <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:application-context.xml</param-value>
      </context-param>


      <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>

      <!-- - Publishes events for session creation and destruction through the
            application - context. Optional unless concurrent session control is being
            used. -->


      <!-- Reads request input using UTF-8 encoding -->
      <filter>
            <filter-name>characterEncodingFilter</filter-name>
            <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
            <init-param>
                  <param-name>encoding</param-name>
                  <param-value>UTF-8</param-value>
            </init-param>
            <init-param>
                  <param-name>forceEncoding</param-name>
                  <param-value>true</param-value>
            </init-param>
      </filter>

      <filter-mapping>
            <filter-name>characterEncodingFilter</filter-name>
            <url-pattern>/*</url-pattern>
      </filter-mapping>



      <!-- Session time out is defined for the application -->
      <session-config>
            <session-timeout>30</session-timeout>
      </session-config>

      <!-- Handles all requests into the application -->
      
      <servlet-mapping>
            <servlet-name>CrystalReportViewerServlet</servlet-name>
            <url-pattern>/CrystalReportViewerHandler</url-pattern>
      </servlet-mapping>
      
      <servlet>
            <servlet-name>CrystalReportViewerServlet</servlet-name>
            <servlet-class>com.crystaldecisions.report.web.viewer.CrystalReportViewerServlet</servlet-class>
      </servlet>
      
      <servlet>
            <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                  <param-name>contextConfigLocation</param-name>
                  <param-value>
                        /WEB-INF/spring/appServlet/servlet-context.xml
                  </param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
            <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
            <url-pattern>/</url-pattern>
      </servlet-mapping>
      
      <welcome-file-list>
            <welcome-file>/welcome.jsp</welcome-file>
      </welcome-file-list>

      <error-page>
            <error-code>400</error-code>
            <location>/WEB-INF/views/errors/pageNotFound.jsp</location>
      </error-page>

      <error-page>
            <error-code>404</error-code>
            <location>/WEB-INF/views/errors/pageNotFound.jsp</location>
      </error-page>

      <error-page>
            <error-code>403</error-code>
            <location>/WEB-INF/views/errors/accessDeniedPage.jsp</location>
      </error-page>

      <error-page>
            <error-code>500</error-code>
            <location>/WEB-INF/views/errors/errorPage.jsp</location>
      </error-page>

</web-app>
0
 
LVL 7

Expert Comment

by:gudii9
ID: 38861544
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 35

Expert Comment

by:mccarl
ID: 38862038
With that web.xml configuration, if you access the rest of your app via the...

http://localhost:8080/projectname/

URL, then for your Crystal Reports, you would have to use a URL such as...

http://localhost:8080/projectname/CrystalReportViewerHandler

(Note that "projectname" is in there)

This is something that I see people get confused about quite a bit (I struggled with it too, early on). Think about it heirarchically, you have a "host:port" that identifies your Tomcat instance, then you have a "context" name which identifies which (or possibly many) webapps that are loaded into yout Tomcat instance, and then you have a "servlet" name which (can) identify that servlet within your webapp with your Tomcat instance that you are targetting.

So with the URL for the Crystal Reports that you posted above, you are missing the "context" part of the above, or in other words, it it trying to find the default servlet in a webapp called "CrystalReportViewerHandler" which obviously doesn't exist. You need to target your "projectname" webapp first, and then the "CrystalReportViewerHandler" servlet inside that webapp.


Hope that makes things clearer!
0
 
LVL 20

Expert Comment

by:Sathish David Kumar N
ID: 38862686
Use spring security to move one project  another project.

your link says its diffrent project .

http://localhost/projectName
http://localhost:8080/CrystalReportViewerServlet

if its not diffrent project  use @mccarl suggestion
0
 

Author Comment

by:Rocking
ID: 38863806
Thanks experts for your valuable suggestion,but i understand that i need to modify the web,xml file to provide the context path otherwise it would not work.
Bu the time i pass the context path i am able to view the report earlier also,but couldn't go to the next page or other except the initial or say first page.

Please refer the below link

http://stackoverflow.com/questions/9680548/how-to-customize-crystal-servlet-uri

Hope so u can get my question clearly.

What i need is there anyway i can bypass the dispatcher request to avoid looking in context and directly in the webapp location for this particular request (CrystalReportViewerHandler).

I am badly stuck in this..
0
 
LVL 26

Expert Comment

by:mrcoffee365
ID: 39001750
web.xml does not have a lot of logic associated with it, so you can't use web.xml to do precisely what you're asking.

However, several of the experts have explained other methods.  Leave web.xml with the rule to send all requests to the same dispatcher servlet.  Then put a filter in your web app which recognizes the CrystalReport request and direct it to the servlet you want.

Or, list every single url for your webapp, and send one to the CrystalReport servlet, and the rest to your dispatcher servlet.
0
 

Author Comment

by:Rocking
ID: 39018447
@mrcoffee365

Thanks a lot for suggesting an idea.

"Then put a filter in your web app which recognizes the CrystalReport request and direct it to the servlet you want."



Could u please provide some url or example with respect to above
0
 
LVL 26

Accepted Solution

by:
mrcoffee365 earned 500 total points
ID: 39019546
This is a pretty good tutorial on how to use a filter in tomcat:
http://viralpatel.net/blogs/tutorial-java-servlet-filter-example-using-eclipse-apache-tomcat/

You don't have to worry about the eclipse part unless you are using eclipse.

In your case, you would check to see if the url matches your CrystalReport url, and in that case forward to the specific servlet, otherwise let it fall through to its normal url.

Writing filters is considered fairly advanced.  If you are not very experienced with tomcat, servlets, etc., you might want to look at tools for doing url rewriting in tomcat, such as the Tuckey url rewriter:
http://tuckey.org/urlrewrite/
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now