3

I have an HTML file and I want to use javascript to call a JSP file.

It doesn't have to be javascript, I'm just looking for the easiest way to call the JSP file from the HTML file.

How can I do this?

Thanks.

1
  • I should have added that I want to call the JSP file in the background. Commented Apr 6, 2010 at 20:00

3 Answers 3

6

HTML/CSS/JavaScript runs at the client side. Java/JSP runs at the server side. The client and server are two distinct environments which usually runs at physically different machines, connected with each other by a network with the communication protocol being HTTP.

When the client requests a specific URL at the server, the server will run specific Java/JSP code and return a HTML/CSS/JS response back to the client. The client (webbrowser) will in turn execute the HTML/CSS/JS.

Knowing this fact, it should be obvious that the only way to let JavaScript access/invoke some Java/JSP code is to send a HTTP request to the server side. This can be done in several ways: using window.location to do a synchronous GET request, or form.submit() to do a synchronous GET or POST request, or XMLHttpRequest#send() to do an asynchronous (ajaxical) request.

But you after all don't need JavaScript for this at all. A simple HTML link or form is also sufficient.

<a href="page.jsp">link</a>

or

<form action="page.jsp">
    <input type="submit">
</form>

This will open the JSP file. If you'd like to run some business stuff prior to opening the JSP page, then better let the URL point to a Servlet like <a href="page"> which in turn forwards the request to the JSP page like

request.getRequestDispatcher("/WEB-INF/page.jsp").forward(request, response);

To learn more about the wall between Java/JSP and JavaScript you may find this article useful.

Sign up to request clarification or add additional context in comments.

Comments

1

I think you are talking about the Ajax. Where u have background JSP page to do the processing in that case try this link link text

Once u understand this u can switch to JQuery of Prototype's ajax which is way better :)

Comments

1

You have to make a some kind of form and in the action attribute of your form to put yourPage.jsp.Something like that

<form action="index.jsp" method="post" accept-charset="utf-8">

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.