0

i have a struts2 web app and I want to use a servlet in my project,but struts2's filter does not allow to call servlets I have looked into this solution, but unfortunately it didn't work out and the problem still stands.

Any ideas?

servlet code:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.print("morteza");
            OutputStream o = response.getOutputStream();
            InputStream is = new FileInputStream(new File("d:/Desert.jpg"));
            byte[] buf = new byte[32 * 1024]; 
            int nRead = 0;
            while( (nRead=is.read(buf)) != -1 ) {
                o.write(buf, 0, nRead);
            }
            o.flush();
            o.close();
            return; 
    }

struts.xml:

<struts>

    <constant name="struts.enable.DynamicMethodInvocation" value="true" />
    <constant name="struts.devMode" value="false" />
    <constant name="struts.custom.i18n.resources" value="ApplicationResources" />
    <constant name="struts.action.excludePattern" value="Ser"/>
</struts>

web.xml:

      <servlet>
    <description></description>
    <display-name>Ser</display-name>
    <servlet-name>Ser</servlet-name>
    <servlet-class>Ser</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Ser</servlet-name>
    <url-pattern>/Ser</url-pattern>
  </servlet-mapping>

webpage:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
fap
<img src="Ser" height="200" width="400"/>


</body>
</html>
Community
  • 1
  • 1
  • What didn't work? Elaborate. Why do you map the Struts filter to everything if you don't want it to intercept everything? – JB Nizet Jan 14 '12 at 12:31
  • my project is in struts framework and I have excluded that servlet according to that answer – alavinejad Jan 14 '12 at 12:38
  • Yes, I understood that. What did happen when you tried this. Any exception? What's the mapping of your servlet? What did you type in the struts.xml file? Why don't you use a Struts action instead of this servlet? A Struts action can do everything a servlet does. – JB Nizet Jan 14 '12 at 12:41
  • how can i display image via struts2? – alavinejad Jan 14 '12 at 12:45
  • 1
    Use a stream result. See http://struts.apache.org/2.x/docs/stream-result.html. Or do the same thing as what you do in your servlet: write the image data to the response output stream, and return ActionSupport.NONE or null from the action, as explained here: http://struts.apache.org/2.x/docs/result-configuration.html – JB Nizet Jan 14 '12 at 12:55
  • @alavinejad Use the stream result type. Why didn't you ask that question in the first place? – Dave Newton Jan 14 '12 at 12:55
  • Did you see either of the comments that answer your question? – Dave Newton Jan 14 '12 at 13:27
  • I posted my codes;image is not displayed ! but this codes works in another project which is not on struts – alavinejad Jan 14 '12 at 13:33
  • @alavinejad Can you hear us? Btw, unless you use a `@` and a name, nobody will know you're talking to them. – Dave Newton Jan 14 '12 at 13:43
  • @alavinejad (Plus you need to say which version of S2 you're using.) – Dave Newton Jan 14 '12 at 13:46
  • @ JB Nizet and @ Dave Newton I posted my codes;image is not displayed ! but this codes works in another project which is not on struts – alavinejad Jan 14 '12 at 13:48
  • The struts.action.excludePattern must be, as its name indicates, a pattern. Not the name of a servlet. You must thus use the same pattern as the one used to map your servlet: /Ser. And you really need to listen to what people say (you should use a Struts action for this, and not a servlet). And you should also improve your diagnostics and the way you tell them. It doesn't work is a very vague description of what happens. – JB Nizet Jan 14 '12 at 13:55
  • I used struts action and the problem is solved,tnx all – alavinejad Jan 14 '12 at 14:16

1 Answers1

0

you can use struts2 Stream Result for display the image

 public class ImageAction extends ActionSupport{

        public InputStream getImage() throws FileNotFoundException{
            return new FileInputStream("f://test.jpg");
        }

    }


    <action name="getImage" class="com.project.ImageAction">
                <result name="success" type="stream">
                    <param name="contentType">image/jpeg</param>
                    <param name="inputName">Image</param>
                    <param name="contentDisposition">attachment;filename="image"            </param>
                    <param name="bufferSize">1024</param>
                </result>
    </action>

if your project url like http://localhost:8080/project

<img src="http://localhost:8080/project/getImage" height="200" width="400"/> 

or

<img src="getImage" height="200" width="400"/>
Arun
  • 1,167
  • 18
  • 37