1

I am a PHP developer but am transitioning to Java. (very new to Java at this point)

Is there a way to make an ajax call to a Servlet, and respond with the output of a separate .jsp file (as opposed to html or json created directly in the Servlet)?

Here is an example of what is common practice w/ Zend Framework, which is what I would like to do with Java if possible:

public function myAjaxCallAction(){
    $this->view->someVar = 'whatever';
    $this->view->hello = 'world';
    $output = $this->view->render('someViewScript.phtml'); // the above vars are in this view
    echo $output;
}

Again very new to java, any advice pertaining to this type of situation would be much appreciated!

Jason Fingar
  • 3,358
  • 1
  • 21
  • 27
  • A JSP file is basically just a HTML template. You're not required to put the full block of `...` in a JSP. Just a part of it like `
    ...
    ` is also absolutely fine.
    – BalusC Aug 05 '12 at 04:00

3 Answers3

2

Just try to load the .jsp that you want. Normally you will use a JSP fragment (.jspf). If you want to load its contents, you can do something like:

Your page:

... content ...
<div id="container"></div>
... content ...

Javascript of the page above (using jQuery):

$(function(){
    $( "#container" ).load( "pathToYoutJsp/file.jsp", { someVar: "whatever", hello: "world" } );
});

The JSP that will be loaded will look something like:

... content ...
${param.someVar} foo foo foo ${param.hello}
... content ...
davidbuzatto
  • 9,207
  • 1
  • 43
  • 50
0

Yes, If i get your question correctly then what you want to do is a simple AJAX request to a JSP file and get reaponse. Do it as follows:

In client Javascript:

var xmlHttp = new XMLHttpRequest(); 
xmlHttp.open( "GET","ajaxreq.jsp?query=John", false ); 
xmlHttp.send(); 
return xmlHttp.responseText;
//returns the response from server. here it is "Hello John"

In server ajaxreq.jsp :

<% 
..              //import required libraries and other application logic
out.print("Hello "+request.getParameter("query"));
..
%>

Hope this helps.

vivek_jonam
  • 3,237
  • 8
  • 32
  • 44
  • Ajax without browser check? Scriptlet in a JSP? – davidbuzatto Aug 05 '12 at 15:52
  • @davidbuzatto not a perfect code. just giving some skeleton.. will add now. – vivek_jonam Aug 05 '12 at 16:09
  • You didn't understood what I said. I said that because your code have bad practices. You need to use Ajax through a library thta will do the browse check, unless you do it by yourself. Using scriplets is not a goot thing too. – davidbuzatto Aug 05 '12 at 16:14
0

Yes you can, by using request.setAttribute before dispatch and then dispatch to your separate page

request.setAttribute("someVa", "whatever");

RequestDispatcher requestDispatcher; 

requestDispatcher = request.getRequestDispatcher("/thankYou.jsp");

requestDispatcher.forward(request, response);

See the following link redirect jsp from servlet RequestDispatcher

Community
  • 1
  • 1
mohamed sulibi
  • 526
  • 3
  • 12