load a jsp in a dialog and div - java

I know this might be a duplication question, but I couldn't find the answer for this simple question. I want to load a new jsp file in a dialog and a div.
Structure:
-WebContent
-jsp
-viewfolder
-helloworld.jsp
-helloworldedit.jsp
-newworld.jsp
let's say I have helloworld.jsp which is loaded from request dispatcher. I want to load newworld.jsp in a div tag in helloworld.jsp.
<div id="result"></div>
$('#result').load('/jsp/viewfolder/newworld.jsp');
Tried the above code, didn't work.
I have also tried to load a jsp page into a dialog and this one has failed too.
<button id="button">button</button>
<div id="dialog"></div>
$('#button').on("click", function() {
$('#dialog').load('/jsp/viewfolder/helloworldedit.jsp').dialog();
});
The question I have is, this is the right way to call the jsp page or do I have to load the page from request dispatcher using ajax.
To test if the path is correct, I tried to put a calendar.gif in the same folder and I was able to reach it from the context.
http://localhost:port/.projectcontext/jsp/viewfolder/calendar.gif.

You have to wait for DOM ready event:
$(document).ready(function() {
$('#button').on("click", function() {
$('#dialog').load('/.projectcontext/jsp/viewfolder/helloworldedit.jsp').dialog();
});
});

Try below code :-
Suppose you have a div in newworld.jsp which contains all data which you want to load in another div which is present in helloworld.jsp
newworld.jsp
<!doctype html>
<html>
<body>
<div id="target">
<!-- other HTML controls -->
</div>
</body>
</html>
helloworld.jsp
Click Me
<div id="page"></div>
<script>
function loadPage(){
$('#page').load('newworld.jsp #target');
or
// If you choose this method then give path of JSP to be loaded in
// anchor tag
$('#page').load($(this).attr('href'));
return false;
}
</script>
OR
You can use JSP include tag like this
<div id="page" style="width:300px; height:300px;">
<jsp:include page="path of the page to be loaded"></jsp:include>
</div>

Related

JSP should redirect to a dynamic URL in a new window or tab

I have a jsp page from which i have to redirect to an external URL and this URL has a dynamic field at the end which i bring from another class. The task for me is to open this url in a new tab or window when that jsp is loaded. I tried the target attribute in the HTML tag but it did not work.
<html target="_blank">
<body>
<%
String filename=(String)request.getAttribute("fileToOpen");
response.sendRedirect("http://manuals/example/"+filename+".pdf");
%>
</body>
</html>
have you tried javascript?
you can put a tag (which are always loaded before the HTML).
for example (http://jsfiddle.net/yeLpy4st/)
For your case you can use something like
<%
String filename=(String)request.getAttribute("fileToOpen");
%>
<script>
window.open("http://manuals/example/"+<%= filename %>+".pdf", "_blank");
</script>
you can also change where you want the new page to open (in this case it's '_blank'), a good reference is Window open() Method reference from W3Schools
You could try it with Javascript :
window.open('www.newtab.com','_newtab');
I used the 'target' attribute in the anchor tag. I did not use JavaScript because of some application constraints. It worked!

Why is this simple jsp program is NOT running?

As we know every jsp program there is a servlet behind the jsp page. I have used a jsp page to make a form (its a very small form), and in the same jsp i used scriptlet tags and made a way to get the inserted form data, and display it using out.print(). but the problem is it when i run it, the form is displayed., but when i submit is, it doesn't recognize the servlet page (error coming as "The requested resource is not available"). i will put the code below., please help me friends to solve this problem. thank you.
i did this in netbeans.
jsp page name is- "hello.jsp"
the servlet page name behind the jsp page is: "hello_jsp.java".
<html>
<head><title>IF...ELSE Example</title></head>
<body>
<form action="hello_jsp" method="post">
<input type="text" name="y"/>
<input type="submit" value="submit"/>
<%
if(request.getParameter("y")!=null) {
String s = request.getParameter("y");
if(s.equals("hello")){
out.print("welcome"+s);
}else{
out.print("not welcome");
}}
%>
</form>
</body>
</html>
My guess is that you need to change
<form action="hello_jsp" method="post">
to
<form action="hello.jsp" method="post">
<!-- ^---- change is here -->
The externally-accesible resource is the jsp, not the servlet. (By default, I'm sure some config-fu could change that.)
Or, of course, if the page is supposed to submit to itself, don't include action at all. The default is to submit to the current page.
<form method="post">

Jquery Load function not referring master page script files

I am using jquery load function in my application's masterpage.html and it is working fine. But the problem is i need to put Jquery.min.js in all my content pages in order to use jquery functionality in my content page. I don't want to include it in all pages. It should refer from my master page.
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js">
<script type="text/javascript">
function loadContent(pagename)
{
$("#output").load(pagename);
}
</script>
</head>
<body>
About
<div id="output"></div>
</body>
</html>
about.html
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js">
<div id="content">
This is about page !!
</div>
As you see my about.html again am including jquery in order to use it in my about.html page. How to avoid this in all pages ? Please help
No, it is not possible . You have to include it in all pages where you are using jquery
Instead of referring to the url like below ,
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js">
You can download the jquery library to your project path and use it like
<script src="project_name/folder_name/jquery.min.js">
But it is compulsory to include jquery in all pages
Hope this helps !!
I can't give you a best answer, these is my advice
the jQuery file had better include at the bottom in body tag.
If you use the jQuery,you must include it.

Calling Struts2 Namespace from Normal HTML form

Exception :
Description: The requested resource /Strut2Examples/checkMethods/updateCRUD is not available. How to call the namespace based action from normal HTML for with Struts2. It works with Struts2 Forms. Please help me to understand.
HTML :
<s:form namespace="/checkMethods" action="executeCRUD" >
<s:submit label="execute" value="execute" />
</s:form>
<form name="normalForm" id="normalForm">
<input type="button" value="update" onclick="submitForm()"/>
</form>
Java Script :
<script type="text/javascript">
function submitForm()
{
var myForm = document.getElementById("normalForm");
myForm.action="checkMethods/updateCRUD";
myForm.submit();
}
</script>
If you want to use HTML form tag you should build the URL using the action name and namespace attributes of the url tag. For example
<form action="<s:url namespace="/checkMethods" action="deleteCRUD"/>" method="POST">
The same concern the JavaScript code where you could mix url tag. Like this
myForm.action='<s:url namespace="/checkMethods" action="deleteCRUD"/>';
Actually if you define the action attribute of the form you don't need to construct the URL in the event handler function. Just do submit() .
You should take url tag seriously, because for example you have constructed URL in the action attribute that lacks context path and slash and errors like that you did can't count.

How can I asynchronously show/hide div in JSP using Java

I want to programmatically show a div tag after some processing has completed while rendering a JSP. What's the best way to do this using Java? Using jQuery I would do this:
$('#mydiv').removeClass("hide_me");
...or...
$('#mydiv').show();
How can I do this programmatically in Java while rendering the page?
Assuming you have the standard JSP setup including JSTL and have mapped it to 'c' you could just do:
<c:if test="${myCondition}">
<div id="mDiv">
content
</div>
</c:if>
It does seem from the comments like there is some confusion about rendering JSP on the server vs rendering content in the browser. Everything that happens in the JSP is server side work that has to completely finish before the browser receives the generated document and starts drawing it. You can't use JSP to change content that is already on the user's screen. You need javascript, html5, etc, for that.
With JSP Java runs on the server (unlike JavaScript that runs within browser) so conditionally render your <DIV> using Java if statement within JSP:
<% if( test="true" ) { %>
<DIV>....</DIV>
<% } %>
I think you are looking for something like this:
<div id="loader">Loading / GIF animation</div>
<div id="result" style="display:none;">
Lots of data.
Should be flushed to the browser every now and then.
This will take seconds...
</div>
<script type="text/javascript">
$("#loader").hide();
$("#result").show();
</script>

Categories