0

I wish to call a java class method from JSP whenever I submit my form. My JSP file will have a file tag. User will browse a file from his local machine. And click on Submit. This file object should be available in a Java class where I will have my business logic.

Is this possible without using struts?

<s:form action="**direct call to Action method here**" method="post" enctype="multipart/form-data" >
<s:file name="userImage" label="User Image" /><s:submit />

Please help me with the ways of doing this.

thanks.

DarkKnightFan
  • 1,913
  • 14
  • 42
  • 61

1 Answers1

0

Value of action attribute should be URL. The given URL specifies an address to which the data from the form should be sent when a form is submitted.

Use following html code in your jsp page:

<form action="uploadFile" method="post"
                    enctype="multipart/form-data">
<input type="file" name="file" />
<input type="submit" value="Upload file" />

Define mapping of servlet that responsible for loading a file in your web.xml file:

 <servlet>
    <servlet-name>UploadServlet</servlet-name>
    <servlet-class>package.UploadServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>UploadServlet</servlet-name>
    <url-pattern>/uploadFile</url-pattern>
</servlet-mapping>

And implement the method doPost in given servlet in which call the method from your business logic:

protected void doPost(HttpServletRequest request, HttpServletResponse response) {
    yourMethod();
}

You can specify any JSP page in attribute action if you are using in your application only JSP:

<form action="upload_file.jsp" method="post" enctype="multipart/form-data">

This page will process a upload-file request in this case.

kapandron
  • 3,546
  • 2
  • 25
  • 38
  • I tried the above steps but it didnt work for me. I know I might be missing something here. Below is my code: JSP:
    Web.xml: UploadServlet com.action.UploadAction Class: public class UploadAction extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) { syso("There"); } }
    – DarkKnightFan Aug 09 '12 at 10:58
  • What exception do you get? Do you have the `servlet-mapping` tag in your config file? – kapandron Aug 09 '12 at 11:12
  • It says _HTTP Status 404 - There is no Action mapped for namespace / and action name uploadFile._ **Web.xml:** ` UploadServlet com.action.UploadAction UploadServlet /uploadFile ` – DarkKnightFan Aug 09 '12 at 11:20
  • Do you use Struts? The error message is saying that the class isn't available. This proposed architecture works without Struts. – kapandron Aug 09 '12 at 11:45
  • Thanks. I corrected my web.xml and now the flow is reaching the action class. One last doubt, I am not getting the file object in the request. I did `request.getAttribute("file");` but it gives me null object. Any help with this? – DarkKnightFan Aug 09 '12 at 11:49
  • Sure such realization is incorrect. An uploaded file is included in the binary stream. The standard Servlet API doesn't provide capability for its parsing. There are several ways to solve this issue. I would advise you to use [Apache Commons FileUpload](http://commons.apache.org/fileupload/). You can find an excellent example of its use and a detailed description [here](http://balusc.blogspot.com/2007/11/multipartfilter.html) and [here](http://stackoverflow.com/questions/2422468/how-to-upload-files-to-server-using-jsp-servlet/2424824#2424824). – kapandron Aug 09 '12 at 12:56