Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I used eclipse IDE to develop an application which calls servlet POST method from jQuery using $.ajax(). Now I need to deploy this application on server. Untill I use this application in eclipse IDE it was working fine but when I copied all the files to server the jQuery is giving error in ajax request. So I wanted to know what are all the things we should follow in order to call servlet from ajax without using eclipse IDE. Here are my codes which I am using in eclipse and which working correctly. I need to know what modification should I make in order to make this files work properly in server.

My html code :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">

  <head>
    <title>Demo</title>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.10.0.min.js"></script>
    <script type="text/javascript" src="newJs.js"></script>
  </head>
  <body>
    <button type="button" id="search-button" >button</button>
  </body>
</html>

My javascript file which contains jQuery an $.ajax() call

$(function(){
            alert("loaded");
            page.init();

        });

var page = {
    addEvent:function(){
        alert("here");
        page.controls.searchButton.click(page.handlers.onSearchButtonClick);
    },
    handlers:{
        onSearchButtonClick: function(){
            alert("hi");
            var msg = [1,2,3,4];
            $.ajax({
                url : "simpleServer",   

                data : {
                    msg:msg
                },
                type:"POST",

                success : function(data){

                    alert("called "+data);
                },

                error: function(xhr, status){
                    alert("error : "+status);
                },
                complete: function(xhr, status){
                    alert("complete"+status);
                }

            });
        }
    },
    controls:{
    },
    init: function(){       
        page.controls.searchButton = $('#search-button');
        page.addEvent();
    }
};

My servlet code :

 import java.io.IOException;
 import java.io.PrintWriter;
 import javax.servlet.ServletException;
 import javax.servlet.annotation.WebServlet;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;

 @WebServlet("/simpleServer")
 public class simpleServer extends HttpServlet {
private static final long serialVersionUID = 1L; 

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

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

    String[] rcvData = request.getParameterValues("msg[]");

    System.out.println("post method "+rcvData.length);
    for(int i=0;i<rcvData.length;i++){
        System.out.println(rcvData[i]);
    }

    PrintWriter pw = response.getWriter();
    pw.write(" Hi welcome to you \n\n");
   }

  }

and my web.xml file is

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web- app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
 <servlet>
    <servlet-name>simpleServer</servlet-name>
    <servlet-class>simpleServer</servlet-class>     
 </servlet>
 <servlet-mapping>
    <servlet-name>simpleServer</servlet-name>
    <url-pattern>/simpleServer</url-pattern>
 </servlet-mapping>
</web-app>

Please let me know what I should in order to run servlet through ajax call on server.

Thank you

share|improve this question
 
I'm very happy to see people not using eclipse for everything :) Do you export your project as WAR and deploy in Tomcat? What is the error from your web server? –  Yori Kusanagi Jun 17 '13 at 0:42
 
Thank you very much. I am just getting "error" as message. No I did not exported WAR and deploy in Tomcat. I am using this code in different server not in Tomcat. Are there any specific things we need to follow inorder to run ajax and servlet on server ? –  Anup Jun 17 '13 at 1:06
 
Just to double check if we are on the same page. Fist you need a Web Server (e.g Tomcat, WebSphere, JBoss) running a Web Application (you .war file). Next, you can make Ajax calls from a Web Client (your html + javascript/jquery). You do not have to change your code. You need to learn "how to deploy an Web application ARchive"... It is a long subject, but I hope my short description can help as a guide-line. Let me know in each step you are stuck, maybe I can help. –  Yori Kusanagi Jun 17 '13 at 1:39
 
Yes surely it is very helpful. Thank you very much. I have a web server called omega. I think I need to now run my .war file in that server right. Then what is actually developing web application archive. The thing is in eclipse the URL that I will be used in ajax can be mapped to java servlet in web.xml file. Now I need some way to map the url to servlet class so that I can access the method required. –  Anup Jun 17 '13 at 2:27
 
For development you can install Tomcat in you local machine, deploy your .war file there, run your ajax through localhost:8080/YOUR_WAR_FILE_NAME. Later you can deploy your .war to your Omega server (never heard before). Web servers follow standards, if you code locally you should be able to deploy to other web servers, only if you are using "server specific features". Good luck. –  Yori Kusanagi Jun 17 '13 at 2:50
show 1 more comment

1 Answer

up vote 1 down vote accepted

I would suggest these references for you. That is a broad subject, you may need someone to help you in person or find more tutorials on-line. Good luck.

  1. http://help.eclipse.org/indigo/index.jsp?topic=%2Forg.eclipse.wst.webtools.doc.user%2Ftopics%2Ftwcrewar.html

  2. http://help.eclipse.org/indigo/index.jsp?topic=%2Forg.eclipse.wst.webtools.doc.user%2Ftopics%2Ftwcrewar.html

  3. http://www.youtube.com/watch?v=gWc05WCkVNg

share|improve this answer
 
Thank you very much. I will go through these and work on it. –  Anup Jun 22 '13 at 4:34
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.