Jsp Ajax File Upload Example
How to Connect Servlet to the Database Oracle with Examplepackagejava. IOException importjava. Print. Writer importjava. Connection importjava. Driver. Manager importjava. Result. Set importjava. Statement importjavax. Servlet. Exception importjavax. Http. Servlet importjavax. Http. Servlet. Request importjavax. Http. Servlet. Response publicclass. Servlet. Database. Connectextends. Http. Servlet protectedvoiddo. PostHttp. Servlet. Request req,Http. Servlet. Response resthrows. Servlet. Exception,IOException Print. Writer pwres. get. Writer res. Content. Typetexthtml Stringtbreq. Parametertable try Class. Nameoracle. jdbc. Oracle. Driver Connection conDriver. OLD/2.1.0.GA/en/GettingStartedGuide/html/images/jsp_application/jsp_application_6.png' alt='Jsp Ajax File Upload Example' title='Jsp Ajax File Upload Example' />Manager. Connectionjdbc oracle thin localhost 1. XE,system,admin Statement stcon. Statement System. Jsp Ajax File Upload Example' title='Jsp Ajax File Upload Example' />Result. Set rsst. QuerySelect from tb pw. Int1lt td lt td rs. String2lt td lt td rs. String3lt td lt tr pw. Exceptione e. Stack. Trace . Understand j. Query Ajax Function Call Code behind C Method. In this article we will learn how to implement an ajax method in j. Query. There is no need to explain that ajax is one of the strong pillars of the future of web development. Since ajax is a concept or technique we can implement ajax with any web development platform. It is possible to implement ajax with PHP or with JSP or with ASP. NET or with many other technologies. Since we are learning. Jsp Ajax File Upload Example' title='Jsp Ajax File Upload Example' />NET, in this article we will explain ajax in the context of C. NET. The experienced developer will say that there are two possible ways to implement ajax in an ASP. NET web application. The first one and the worst one is using an update panel. And the best one is using a j. Query ajax function. Airsoft Buying Laws. Hmm. Many developers will not agree with my straight forward conclusion. But if you search a little in the WWW and talk about. NET savvy then you may agree with me. The reason why a update panel is not feasible is, when we add more than a few update panels in the same page, the performance of the page will decrease automatically. Servlet-File-Download.png' alt='Jsp Ajax File Upload Example' title='Jsp Ajax File Upload Example' />This is a simple example on how to make AJAX calls from a JSP page to a Servlet using JQuery and update the same JSP page back with the response from the Servlet. In. Includes complete PDF version of Sun Microsystems Press servletJSP text. Coverage of servlets 2. JSP 1. 1. Also includes source code from book, available free. So that, people dont like update panels much. Its cool for small ajax implementations. And the next one is using an ajax method of j. Query. In this article we will implement that. Using the j. Query ajax method we can call nearly any function code behind and various services, like a traditional Web service, WCF, WEB API and so on. In this article we will learn each of them one by one. Call function in Code behind. Sometimes, the situation exists where we need to call a C function defined in code behind in the same page using an j. Query ajax method. Lets see how to do that. Step 1 Create an ajax function in an. Use one. aspx page with the following code. Page. LanguageCAuto. Event. WireuptrueCode. BehindJava. Script. InheritsWeb. Application. Java. Script lt DOCTYPEhtml lt htmlxmlnshttp www. POST, url Java. Script. aspxGet. Data, content. Type applicationjson charsetutf 8, data. Type json, success function response Content. Content lt div lt form lt body lt html The code is very simple. Within the head section we are loading the j. Query pulgin from Googles CDN. There is a little advantage in loading the j. Query plug in from the CDN, we will explain them later. Then we have defined the ajax function to call the C function asynchroniously. The ajax function has a few properties, lets explain a few of them. This is nothing but a request type. Here we are making a POST request type. We will explain various types in the following articles. URL This is nothing but the location of a C function. Here we have provided a page namepath at first and then the method name followed by. We can provide the URL of the remote server. This defines in which form the data will pass. In other words, what the encoding scheme is for the data passed in. For example, we can send data in JSON or XML format. Success and failure Both are callback functions, after a successful ajax request the success function will execute and after failure the failure function will execute. Within the success function we are getting data and pushing it to the DIV tag that was defined in the body portion of the HTML page. Step 2 Define C function in code behind. Now we need to define the Get. Data method in code behind. Go to the code behind portion of the same page and define the following method. System using System. Collections. Generic using System. Linq using System. Web using System. Web. UI using System. Web. UI. Web. Controls using System. Web. Services namespace Web. Application publicpartialclass. Java. Script System. Web. UI. Page protectedvoid PageLoadobject sender, Event. Args e Web. Method publicstaticstring Get. Data returnThis string is from Code behind The concept is, we need to specify the method as Web. Method by the same attribute, otherwise the ajax function cannot call it and for that we need to include the Sysyem. Web. Services namespace. One more point to be explained. When we call a C function in code behind, the function must be defined as a static function, otherwise the ajax function cannot call it. Here is sample output. Return JSON data to ajax function. If you observe the preceding example closely, you will find that we are working with JSON data as we specified in the j. Query ajax method but we are returning a string The concept is, the JSON data is nothing but a simple string. So we can treat the string as JSON data lets not consider the format and rule of JSON data. So, in this example we will create one List in the C method and we will convert it to serializable JSON format and them we will return it to the j. Query ajax method. Have a look at the following example. Step 1 Create code behind method in. Here is the modified version of our existing Web Method. Web. Methodpublicstaticstring Get. Data Dictionarylt string, string name new. Dictionarylt string, string name. Add1, Sourav Kayal name. Add2, Ram mishra string my. Json. String new. Java. Script. Serializer. Serializename return my. Json. String The body is very simple, We have just defined the Dictionary with both a key and value as a string. The reason is,the key and value must be a string or an object type when we serialize the Dictionary using the Java. Script. Serializer class, otherwise it will throw an error. Then we are returning a simple JSON string to our j. Query ajax function. Step 2 Define JQuey ajax function. Here is the definition of the j. Query ajax function. The code is very similar to the example above. Page. LanguageCAuto. Event. WireuptrueCode. BehindJava. Script. InheritsWeb. Application. Java. Script lt DOCTYPEhtml lt htmlxmlnshttp www. POST, url Java. Script. aspxGet. Data, content. Type applicationjson charsetutf 8, data. Type json, success function response var names response. Content lt div lt form lt body lt html For the sake of simplicity we have just shown the data in an alert box, anyway we can show it in a formatted style. In a future article we will learn various ways to process JSON data. Here is the sample example. Conclusion. In this article we saw how to implement a simple j. Query ajax method to call a code behind C method. In a future article we will learn to call any service method from the j. Query ajax method.