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!

1
  • A JSP file is basically just a HTML template. You're not required to put the full block of <html>...</html> in a JSP. Just a part of it like <div>...</div> is also absolutely fine.
    – BalusC
    Aug 5, 2012 at 4:00

3 Answers 3

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 ...
0
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.

3
  • Ajax without browser check? Scriptlet in a JSP? Aug 5, 2012 at 15:52
  • @davidbuzatto not a perfect code. just giving some skeleton.. will add now. Aug 5, 2012 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. Aug 5, 2012 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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