How to call a function written in JSP from a html page? - java

How to call a function written in a JSP from a HTML page? I have declared a function to load an image from server in JSP page. Now I want to show that image in another html page by calling that JSP function in HTML page.

i have declared a function to load a image from server in jsp page. now i want to show that image another html page by calling that function in html page.
That's not how it works. Webbrowser sends HTTP request to webserver. Webserver executes some Java/JSP/Servlet code based on the HTTP request (URL, parameters, pathinfo, etc). Java/JSP/Servlet code produces a bunch of HTML code (which can contain CSS/JS code as well). Webserver sends HTML code back to webbrowser as HTTP response. Webbrowser displays HTML. If you rightclick page in webbrowser and choose View Source, then you should not see any line of Java/JSP/Servlet code.
You just need to write your Java/JSP/Servlet code so that it produces exactly that HTML you want. Displaying images in HTML is to be done by <img> tag whose src attribute should point to the URL of the image.
<img src="foo.png" />
Just put that as-is in the JSP. With the above example, put the image file in same folder as the JSP as well.
If the images are however to be retrieved from an external resource as for example a database, then you need to create a Servlet which obtains an InputStream of the image from the external resource based on the parameters/pathinfo provided by the HTTP request and writes it to the OutputStream of the HTTP response along a set of correct response headers (content type, length, etc). Finally let the URL in the src attribute of the HTML <img> element point to the servlet instead.
<img src="imageservlet/foo.png" />
You can find a more detailed example of the servlet in this answer.

Via an HTTP Request - i.e. submitting the page to the web container where the JSP executes. This is a very normal pattern.

Related

How can I tell the browser to load a html file with a Java servlet?

The idea is that you can add something to a database, which goes from browser -> java code -> JSP -> java code -> database, and you are then redirected to a page containing the information you sent. The servlets are in place but I cannot redirect to the HTML page from a get request.
I have a servlet to PrintWriter().print() the data in a Json object, but that servlet is called from the javascrit within the HTML page. How can I send the HTML page? Should I parse the HTML page and PrintWriter().print() each line? Is there a more proper way of doing this?
Keep in mind that sending HTML straight from JSP is not an option, and I can't change the structure of the system.
edit: Sorry, I typed that in a rush.
As a preface, the system is similar to StackOverflow, whereby you can submit a 'request' which prompts the community to crowd-source learning material.
Right now, the structure of the system is JS/HTML on the browser side, which communicates with a mySQL DB through an API written in Java. The API goes through JSP which communicates with an inner Java API for accessing the DB. The catch is that I must return Json objects from the API. I know that JSP is essentially useless and I could interface the two APIs without JSP, but this is a first year college project so I don't have the choice.
When you submit something to the database using the url /addrequest (or similar), the system puts the text into the database and then redirects you to /request/idnumber. When you access the /request/* URL, another servlet runs. I want this servlet to tell the browser to open my "request_display.html" page. Then the javascript on that page will call another url to get the Json object through the API, and then it will build the page.
I don't know how to tell the browser to open a html page. Should I just parse the html file and then use response.GetWriter().print() to do send the HTML?
If you are in a Servlet:
response.sendRedirect("pathOf YourHTMLPage");
If you are in a JSP page, try using a form or a "a" element. Like this:
<form action="nameOfYourServlet"></form>
or
Can't really understand what you are looking for but if you want to redirect user to an html page using servlet this can be done using response.sendRedirect("path to html");
It would be nice if you could explain via some code as your English is hard to understand.
response.sendRedirect("redirect.html");
Alternative way
ServletContext sc = getServletContext();
sc.getRequestDispatcher("/redirect.html").forward(request, response);

MIME Issue: When RequestDispatcher is used to forward a request from a Servlet to a JSP page, the CSS and Javascript of that JSP page aren't loading

I'm calling a servlet from a JSP page and setting some Attributes in servlet and finally calling ANOTHER JSP using RequestDispatcher's forward method. The second JSP is loading, showing the attributes that I've passed to it but none of the STYLESHEETS or jQuery/JAVASCRIPT files are loading into the page.
Error Msg: Upon Element Inspection in Mozilla of the forwarded JSP page I'm getting following error msg .
The stylesheet .../java/tutorial/mystyle.css was not loaded because
its MIME type, “text/html”, is not “text/css”.
And for javascript its showing SYNTAX ERROR msg.
Without this forwarding if the JSP page is run, both the CSS and JavaScript called on this page are loading.
I've tried adding mime-mapping in my web.xml for css but still its not working.
I tried calling the second JSP page separately without servlet forwarding everything is working fine, stylesheets and javascripts are all loading.
How can I load my CSS and Javascript during Request forwarding ??
Thanks in advance

Redirect parent page from a ifram in JSP by JAVA

A same question can be found here . and some equal questions found.
They are about javascript (answer :-window.top.location.href = "http://www.site.com"; )
But
I want to know how to redirect it by Java in a jsp page. A usual redirect can be done as..
response.sendRedirect("newUrl.jsp");
But it redirects the source page inside the iframe not the parent page. The task is that the source file of the iframe get refreshed and do some stuff(checking session.. getting attributes. )and if the source page meets a particular logical stage,then the parent page might be redirected to another page.But not in javascript, I want to know it in JAVA( That means the jsp/servlet container decides and redirects to another page in the server).
My current opinion is that, such things might be handle with javascript.Your all info is highly appreciated.
Instead of sending a 301 or 302 redirect, send a JSP page (or write to the output stream of the response) with html content that has the javascript redirect in it set to execute on load. That way you can do your checking in the java side of things and just send the javascript redirect when appropriate, otherwise send regular content.
I have scenario like:
We have an application deployed on WebLogic and one application deployed on tomcat.
we have embeded tomcat application to run in WebLogic application.
somewhere we want to redirect the page on WebLogic application URL which is the parent application. for that if you use:
response.sendRedirect("newUrl.jsp") it will redirect to inner application URL(tomcat).
to resolve it I have used JavaScript for redirection
<%
boolean isRedirect = true // check if redirection required, I have made it true for testing
String redirectHomepage = "home.jsp";
if(isRedirect){
%>
<script type="text/javascript">
window.parent.location.href = "<%=redirectHomepage%>"
</script>
<%
return;
}
%>

Can't return a CSS html from servlet

I'm trying to return an html, page using out.print(); from a servlet, and I can do it successfully the thing is that this is a group project and this other guy send me an html response that I have to mount in the servlet, he used css, and images and I tried to put all of what he send me in a out.print(); but I dont get images or color or whatever. How should it be done?
The CSS and images are most likely intended to be separate resources, each with their own distinct URLs. If you are going to output the resource contents from your servlet as you are currently doing, your servlet code has to look at the details of the HttpServletRequest and output the appropriate resource depending on the requested URL. When the user's browser requests the HTML page, when it requests an image, you give it that image and so on.
There are probably better ways to do this ...

does Jsp runs under the Servlet code?

if we write a code in jsp that will convert to servlet code and get runs. is it true? your suggestions are more thankful.
From http://java.sun.com/developer/technicalArticles/javaserverpages/servlets_jsp/
How Do JSP Pages Work?
A JSP page is basically a web page
with traditional HTML and bits of Java
code. The file extension of a JSP page
is .jsp rather than .html or .htm,
which tells the server that this page
requires special handling that will be
accomplished by a server extension or
a plug-in.
When a JSP page is called, it will be compiled (by the JSP engine) into a
Java servlet. At this point the
servlet is handled by the servlet
engine, just like any other servlet.
The servlet engine then loads the
servlet class (using a class loader)
and executes it to create dynamic HTML
to be sent to the browser, as shown in
Figure 1. The servlet creates any
necessary object, and writes any
object as a string to an output stream
to the browser.
(source: sun.com)
HTH

Categories

Resources