如何从Struts2应用程序中的外部URL调用Servlet

时间:2015-03-24 10:52:38

标签: servlets struts2

我的web.xml文件:

<filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
<servlet>
      <servlet-name>TestServlet</servlet-name>
      <servlet-class>com.app.servlets.TestServlet</servlet-class>
      <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
      <servlet-name>TestServlet</servlet-name>
      <url-pattern>/viewDoc/*</url-pattern>
  </servlet-mapping>

我的Servlet是:

package com.app.servlets;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TestServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {

    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        performTask(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
            IOException {
        performTask(request, response);
    }

    private void performTask(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String accountNo = (String)request.getParameter("accountNo");
        System.out.println("accountNo: "+accountNo);
        response.setContentType("text/html;charset=UTF-8");
        ServletOutputStream  outs =  response.getOutputStream ();
        //---------------------------------------------------------------
        // Set the output data's mime type
        //---------------------------------------------------------------
        response.setContentType( "application/pdf" );  // MIME type for pdf doc
        //---------------------------------------------------------------
        // create an input stream from fileURL
        //---------------------------------------------------------------

        File file=new File("D:\\processFolder\\990000010-6422_02.PDF");


        //------------------------------------------------------------
        // Content-disposition header - don't open in browser and
        // set the "Save As..." filename.
        // *There is reportedly a bug in IE4.0 which  ignores this...
        //------------------------------------------------------------
        response.setHeader("Content-disposition", "inline; filename=" + "Example.pdf");

        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {

            InputStream isr = new FileInputStream(file);
            bis = new BufferedInputStream(isr);
            bos = new BufferedOutputStream(outs);
            byte[] buff = new byte[2048];
            int bytesRead;
            // Simple read/write loop.
            while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
                bos.write(buff, 0, bytesRead);
            }
        } catch (Exception e) {
            System.out.println("Exception ----- Message ---" + e);
        } finally {
            if (bis != null)
                bis.close();
            if (bos != null)
                bos.close();
        }

    }

}

我的错误是:

  

HTTP状态500 -

     

输入例外报告

     

消息

     

description服务器遇到阻止它的内部错误   完成此请求。

     

例外

     

显示java.lang.NullPointerException     org.apache.struts2.impl.StrutsActionProxy.getErrorMessage(StrutsActionProxy.java:69)
    com.opensymphony.xwork2.DefaultActionProxy.prepare(DefaultActionProxy.java:185)
    org.apache.struts2.impl.StrutsActionProxy.prepare(StrutsActionProxy.java:63)
    org.apache.struts2.impl.StrutsActionProxyFactory.createActionProxy(StrutsActionProxyFactory.java:39)
    com.opensymphony.xwork2.DefaultActionProxyFactory.createActionProxy(DefaultActionProxyFactory.java:58)
    org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:501)
    org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:432)

     

注意Apache中提供了根本原因的完整堆栈跟踪   Tomcat / 7.0.30日志。 Apache Tomcat / 7.0.30

请帮我解决这个问题

0 个答案:

没有答案