how to get the content of JSP page from the Servlet? - java

I need to get the content of a jsp page from a servlet without redirect or forward, like a file for security issues, I'll process the HTML and write the new generated HTML in the servlet response. How can I do that?
An example of what I'm trying to do:
if (request.getParameter("pageName").equals("index")) {
//get index.jsp content and process it...
}

You can't do that in a servlet, but you could do it in a Filter, in the doFilter method. You would need to provide a wrapper around the response object, something like what they do here.

Related

How to tanslate a JSP InputStream into a ServletResponse?

I'm in a Tomcat and have a String with JSP Content. And I try to get the HttpServletResponse means the HTML Output. In normal case you call the JSP and the WebContainer translate it to an Servlet and generates the Output.
But I have no JSP as File, just a String with the Content of an JSP. Is there a Class or Factory where I can put the Stream and get it processed?
thanks in anticipation
No. there is not readily available solution for this.
But, you can try out this:
Write the JSP string to a file inside the web application content root. Let's say /tmp/jspstring.jsp. Use getServletContext().getRealPath("/tmp/jspstring.jsp") to get the path of the new jsp.
Using RequestDispatcher include the newly created JSP, such that the server will process the JSP using

HTML page as AJAX response?

Can I send an entire HTML page with an AJAX response? If so, how to render that HTML page and what are the pros and cons doing that. The reason why I am asking this question is I found out that if we use response.sendRedirect("index.html") as a reply to an AJAX request in servlet we get the indx.html as AJAX response XML.
Your question doesn't make much sense and I can't quite tell what you're asking - the response to an ajax request will be whatever the server sends back. It could be plain text, XML, HTML, a fragment of an HTML/XML document etc. What you can do with depends on your script. If you're using a library like jQuery, what happens on the client side and what you can do with the response can also depend on how the library interprets the response (Is it a script? It it HTML/XML or JSON?).
if we use response.sendRedirect("index.html") as a reply to ajax request in servlet we get the indx.html as ajax response xml. Can some one pls explain this
An ajax request will behave much like a 'regular' HTTP request. So when you send back a redirect from your server (HTTPServletResponse#sendRedirect), the browser follows the redirect just like it would for any other request. If your ajax request was to a resource that required HTTP BASIC authentication, you'd see a prompt to login, just like you would if you visited the URL directly in a new browser window.
If you want to send HTML as a response, because you want to update divs, tables or other elements, but still want to use the same css or javascript files then it can make sense.
What you can do is to just send it as plain/text back to the javascript function, it can then take that and put it into the inner html element that you want to replace, but, don't do this if you want to replace the entire page, then doing what you want is pointless.
When you make the http request for your ajax call, it has its own response stream, and so when you redirect you are just telling the browser to have that http request go to index.html, as #no.good.at.coding mentioned.
If I get your question, you just want to know whether you could return whole entire HTML with AJAX and know the pro and cons.
The short answer to your question is yes, you could return the entire HTML page with your AJAX response as AJAX is just an http request to the server.
Why would you want to get the entire HTML? That's confusing to me and that is the part that I am not clear about. If you want to want to render the entire HTML (including tags like html, body, etc?), you might as well open it as a new page instead of calling it via Ajax.
If you are saying that you only want to get fragments of HTML to populate a placeholder in your page via AJAX then this is an acceptable practice. jQuery even provides load() function (http://api.jquery.com/load/) to make that task easy for you to use (check the section Loading Page Fragments).
Using this method, you could populate the placeholder using the HTML Fragments that is dictated by your server logic (i.e when the login fails or succeed) including the one via server redirect in your question.

Redirect to different page from inside c:import

Is there any way I can redirect to a different page from a Spring Controller that gets called from a JSP using <c:import>?
Scenario is as follows: I have a Spring WizardFormController, that handles a multi-page form and is included into the website using a JSP and <c:import>. After the wizard is finished, I would like to redirect to a different page, but that seems to be impossible from the Controller. At least, if I could get a message to the surrounding JSP, it would already help.
It seems, the only way is to use JavaScript to create a client-side redirect like this:
<script type="text/javascript>
window.location.href = '<URL of Target>';
</script>

Call from one JSP file to another JSP

I need to call from one JSP to another, do some stuff over there..
I am inside the caller jsp, inside his handleReqeust(HttpServletReqeust request)
method I am trying to forward the request to another JSP, to call the other JSP file to his handleRequest(HttpServletReqeust request) off course with the request object
I tried it:
RequestDispatcher dispatcher = request.getRequestDispatcher("/theSecondJspFile.jsp");
if (dispatcher != null)
dispatcher.forward(request, response);
but to make it work I need for it the object response, but I don't have it,
I am sure I missed something basic, but what?
From my question you can see, I don't have solid backround in java, so please correct me, or refer me to good guide if you feel it necessary
Thanks.
-------------------Edit--------------------------------------
I don't need a redirect, I just want to call another JSP file to his handleRequest method
I think it relate to HTML
What I can make out of your question is that you need to redirect to a secondpage.html from firstpage.html with some data. You can use both GET or POST method to send the data.
For the GET method just redirect to secondpage,html?data=value. The data will be available in the HttpRequest parameter in the controller of the secondpage.html where it can be used as required.
For the POST method you would need to post the data (using a form on firstpage.html) to secondpage.html. In the controller of the secondpage.html the data should be available in a similar way as before.
To include another JSP in a jsp :
<jsp:include page="foo.jsp"/>
You can find some reference material here.
I can not be sure, but what I think you are trying to do is to include a jsp page on to your jsp page and use the the objects and other variables declared in the first jsp page in your second.
<%# include file="mypage.jsp" %> should help you do this.
If this is not what you are looking for, please make your question tad bit more clear. some code will help really.
JSPs are supposed to be used to present the final result. JSPs are not supposed to be part of business tasks leading to this result. There you use normal Java classes for, starting with a servlet class. Let the HTTP request (the one which you enter in browser address bar or specify in some HTML link or form) point to the URL of the servlet instead. This way you can write Java code in the servlet the usual way to invoke other Java classes/methods and finally forward the request to a certain JSP file based on the outcome of the result and then just let that JSP present the final result.
To start with servlets, I'd suggest to read our Servlets info page.
I just added empty Iframe, and set his URL when I needed to call him

How to handle HTML tags in servlet java program?

1) In my servlet program, I have a statement which will be printed using the code as follows,
out.println("<b>This is servlet output</b>");
Instead of getting printed in bold, it just gets printed with the tag in the broswer itself.
How to rectify the same?
2) Also, in the servlet page after the submission of a jsp form, I want to add the below HTML tag inside the java code of the servlet program.
Go to JSP form
How to achieve the same? Please advise.
1) The browser is interpreting your output like text, try adding
response.setContentType("text/html");
This line tells the browser that you're sending HTML and that it should be interpreted that way.
2) The same as bold text
out.println("Go to JSP form");
On a related note, I'd suggest that none of your Servlet class directly write HTML content to the response page. Servlet are made to handle forms, and aren't easy to use when it comes to write HTML responses.
Once thing you can try is to write the response in a JSP page, then forwarding the request to the JSP so it can handle user output.
Here is a sample:
1) servet_output.jsp
<b>My bold test</b>
Go to JSP form
2) Your servlet redirects to the JSP page:
request.getRequestDispatcher("servlet_output.jsp").forward(request, response);
This way, your servlet handles the request, and the JSP takes care of writing the response to the browser.
Don't use servlets for html.jsp is the right place to use that.
just use
request.getRequestDispatcher("name.jsp").forward(request, response);
and write html code there.

Categories