6.2.2. HTTP Response Handler

This handler calls the servlet API and sends a response to the client according to HttpResponse returned by the subsequent handler. There are four response methods.

Servlet forward
Forward to the servlet and draw the response. Mainly used for response using JSP.
Custom response writer
The custom response writer(described later) is used to perform arbitrary response output processing.Mainly used for response using external library such as template engine.
Redirect
Returns a response to redirect to the client.
Direct response
Direct response using getOutputStream method of ServletResponse.

The process flow is as follows.

../../../../_images/flow33.png

6.2.2.2. Module list

<dependency>
  <groupId>com.nablarch.framework</groupId>
  <artifactId>nablarch-fw-web</artifactId>
</dependency>

6.2.2.4. How to convert the response

This handler changes the method of response to the client based on the scheme [1] returned from the subsequent handler and status code [2].

The conversion conditions and response method are as shown in the table below.

Conversion condition Response method
When the scheme is a servlet When the custom response writer determines that the process is to be processed, it is transferred to the custom response writer. In other cases, the process is forwarded to the servlet by content path.
When the scheme is a redirect Redirects to specified URL
When the scheme is a http or https Redirects to specified URL
If scheme is other than the above and status code is 400 or more Displays an error screen that matches the status code.
Other than the above Responds with the result HttpResponse#getBodyStream().
[1]The “scheme” here refers to the return value of getScheme() method of ResourceLocator acquired with HttpResponse#getContentPath() returned by the subsequent handler.
[2]The “status code” here refers to the return value of the getStatusCode() method of HttpResponse class returned by the subsequent handler.

6.2.2.5. Custom response writer

By configuring the implementation class of CustomResponseWriter to the customResponseWriter property of this handler, an any response output process [3] can be executed.

[3]A specific example is the case where a response is output using the template engine instead of JSP. Web Application Thymeleaf Adapter is an implementation provided by Nablarch.

6.2.2.6. Change the HTTP status code

This handler changes part of the status code and configures in the response to the client.

The conversion condition that determine the HTTP status code and error code of the response are as shown in the table below.

Conversion condition Error code
For Ajax requests Returns the original status code without change
When the original status code is 400 Returns status code 200
Other than the above Returns the status code result without change

6.2.2.7. Switching the content path for each language

This handler has a function to dynamically switch the forwarding destination based on the language configuration included in the HTTP request. By using this function, the function to switch the JSP to be forwarded according to the language selected by the user can be realized.

When using this function, configure one of the following classes in contentPathRule property of this handler.

Class name Description
DirectoryBasedResourcePathRule

A class that
uses the directory directly under the context root for switching the language.

# Placement example when supporting /management/user/search.jsp
# with Japanese (ja) and English (en)
# Create a directory for each language directly under the context root.
# The directory name is the language name.
Context root
├─en
│  └─management
│      └─user
│           search.jsp
└─ja
    └─management
        └─user
             search.jsp
FilenameBasedResourcePathRule

A class that uses file names to switch the languages.

# Placement example when supporting /management/user/search.jsp
# with Japanese (ja) and English (en)
# Create a file for each language.
# Add the suffix "'_' + language name" to the file name.
Context root
└─management
        └─user
             search_en.jsp
             search_ja.jsp

The configuration example for this is as follows.

<!-- Resource path rules -->
<component name="resourcePathRule" class="nablarch.fw.web.i18n.DirectoryBasedResourcePathRule" />

<!-- HTTP response handler-->
<component class="nablarch.fw.web.handler.HttpResponseHandler">
  <property name="contentPathRule" ref="resourcePathRule" />
</component>

To switch contents by a method other than the above, create a class that inherits ResourcePathRule class and configure the created class in the resourcePathRule property as above.

Tip

This function cannot be used when the response is output by the custom response writer. This is to ensure that it is not mixed with the multilingual function of the template engine.

6.2.2.8. How to handle fatal errors that occur in this handler

When the following events occur in the processing in this handler, it is determined that a normal response cannot be returned and a fixed response with status code 500 is returned to the client.

  • When a ServletException occurs during servlet forward
  • When an exception of RuntimeException and its subclass occurs
  • When an exception of Error and its subclass occurs

The response for these cases is the following HTML.

<html>
  <head>
    <title>A system error occurred.</title>
  </head>
  <body>
    <p>
      We are sorry not to be able to proceed your request.<br/>
      Please contact the system administrator of our system.
    </p>
  </body>
</html>

Important

The above HTML response is fixed and cannot be changed with configuration.

This response is used only in rare cases where an exception occurs in this handler. This specification does not usually cause a problem, but for systems where this response should not be issued under any circumstances, consider preparing handlers with reference to this handler.