I need to call small part of JSP page(like only div load) when a method is invoked? I know we can load whole JSP page from Servlet. Is this possible or any other solution for this?
know we can load whole JSP page from Servlet. Is this possible
Yes it is possible, you forward your request to jsp from servlet and it will render that jsp, here is an exact same example
1) Set some session or request variable equal to some value, say boolean myVar = true.
2) Redirect from your jsp page from your servlet.
3) In the jsp page, check for particular condition for the variable myVar. If condition is fulfilled, using scriptlets or JSTL display only the relevant part of you jsp.
You can use AJAX(Asynchronous Javascript and XML) for this purpose
For example, Suggestions, that appear without any refresh for auto-complete in google search, are using Ajax. JSP code will contain the Ajax code and will send the information to the servlet in form of XmlHttpRequest object and then takes the response from servlet as xmlhttp.responseText. The result can be written at JSP page by using DOM.
To initiate this process, you need to use the onkeyup in input tag like this:
<input type="text" onkeyup="methodName(this.value)"
Learn more about Ajax
To use AJAX with Java, try this
Ajax for Java Web Applications
$('#myDiv').load('serverPage.jsp #server_div_id');
OR
$.ajax({
url: 'serverPage.jsp',
success: function(data) {
data=$(data).find('div#id');
$('#mydiv').html(data);
}
});
Related
Is there an easy way to send HTML from a servlet to a JSP, using AJAX.
I've already figured out how to make AJAX work with servlets dynamically, but now I want to press a button on a form and generate HTML based on text-input.
Is it possible, and if so, how, to send just pieces of HTML to an existing HTML page?
Example,
I have a basic form where you can input your age, and based on the age the text has a different size/color. So, you send for example, 25 as your age to the servlet, and it send back a piece of HTML like this <p STYLE="font-size: age;"> to the page.
Through ajax call you can get the output result either a string, html or a Json object that will be parsed and results can be displayed over JSP/HTML. So for sure you can send html code segment from servlet to jsp through ajax call.
For example you can use this approach--
1. Take a string variable in your servlet.
2. Put appropriate html string as per your conditions in this string variable
3. send this string as a response from servlet like:
response.setCharacterEncoding("UTF-8");
response.getWriter().write("your string variable here");
4. In your ajax call do like this:
success : function(dataString) {
document.getElementById("containerId").innerHTML=dataString;
},
where containerId is the id of html element (like div or span) where you want to display output html.
The easiest approach, without client-side javascript libraries, would be to point an HTML form to an iframe, just like
<iframe name="myIframe"...>
<form target="myIframe"...>
And submit your form as many times as necessary. The HTML returned by the servlet would load itself in the iframe element.
If you like AJAX and client-side javascript libraries, you can find very easy programmatical ways to do this in jQuery and similar libraries.
Basically your servlet can generate any kind of content, e.g. JSON, HTML etc.
You'd then send that content back to the client and integrate it into the page.
How that is done depends on the type of content.
Example:
You issue an AJX request (e.g. by using jQuery's ajax functionality) and your servlet generates plain html. When your JavaScript receives the anser you just replace the relevant part, e.g. by replacing the content of some defined element.
If you used JSON instead, your servlet might send data only instead, e.g. a font size based on the age as in your example. You'd then use JavaScript to access that JSON data and perform relevant operations, e.g. by changing the style of the paragraph.
In my JSP page I have DIV.
<div id="100">
ALI
</div>
When I click on this DIV...
$("#100").click(function(){
});
...I need to send the value of the id 100, to a servlet, so that the servlet makes some database java codes, and returns for example, either 1 or 0. How can I do that? and is this the proper way?
Using Ajax, you should call your server using a URL similar to this:
http://localhot:8080/youAppContext/yourServer?id=100
Then, in the servler side, you should retrieve the value that will be in the request with the name "id"
There are out there many tool as jQuery that can help you to do the Ajax petition.
Edited
Well, here you can find a very simple Ajax example using jQuery. In the example, instead call a file (test1.txt) you should invoke a URL (as I described above). Of course, you will need to write some JS code to build your URL (where id be a variable). Once task is done in servlet side, you can return whatever, for example: "done" and display or not this information the HTML as it is done in the example.
Take a look to this Web, there are many links that can help you.
get the value using
var value = $("#100").html();
and pass it to servlet using AJAX
I have index page,which contain div section.i want to call different pages into this div section on the basis of her-link click,this is done. when calling pages that may also using ajax call,in this case ajax call not work for this page.please tell me it is possible to call ajax inside an ajax.if yes then please help me.
Update Edit:
I have created a gist where i have include all used pages,script https://gist.github.com/2786811.page service_status called the view.jsp using ajax call every 5 second,on hand view.jsp interact with database and prepare view.when we do ajax call from index.jsp, service_status called but it doesn't called view.jsp file.
Thanks
You can make ajax call on page load using $(document).ready(function () {...}); in target page you are calling.
If you are trying to dump a <script> tag with innerHTML, it won't work.
However, you could (as an example) return a JSON object with two properties:
{
html: "All your <b>HTML</b> here",
js: "alert('JavaScript to be run');"
}
Then, use the html property to put content on the page, and then eval the js property.
This isn't the best solution, since it uses the evil eval, but unless you want to rewrite your whole basic structure to use callbacks properly then it's probably your best bet.
I am working in a web application using struts. I am performing some operations in struts action class and using session.setAttribute i am setting result value. My struts action class sends result to jsp page where i am displaying result. I have to display message after loading jsp page, hence i am trying to use jquery. But i am not getting how to handle http sesion request in jquery.
Earlier i was taking one property in sturts-config.xml and setting the value instead of sending http session request parameter. But it doesn't work for me, because every time when i refresh my page it takes initial value from struts-config.xml. i am taking struts property as hidden field in jsp page by writing:
<html:hidden property="propname" styleId="id_propname" name="formname" />
for setting property value null I am writing following code in jquery.
<script type="text/javascript">
$(document).ready(function()
{
alert("some text");
$("#id_propname").val("");
});
</script>
Is there any way where i can directly get the http session request in jquery ? please reply me if you can give some suggestions I thanks to all valuable suggestions in advance.
IMHO there is no simple way to pass your session to the jquery . What you can do is to set the value of some hidden field and declare a global variable on the script and access the value and check for the valid condition if any .
Although its ugly and highly discouraged to use scriptlet inside jsp , this might work :-
<% String sessionVal = session.getAttribute("yourSessionValue").toString() %>
<html type="hidden" id="id_propname" name="propname" value=<%=sessionVal> >
In your jquery :
<script type="text/javascript">
var sessionVal;
$(document).ready(function()
{
sessionVal = $("#id_propname").val();
alert("this is the session value at Script " + sessionVal );
});
</script>
Null check for session is not done.
Or
If you are using tag libraries you can set the session value on form bean and use something like this in jsp :-
<logic:equal name="yourFormBean" property="yourProperty" value="someDefinedValue">
Do your action here
</logic:equal> <br />
Well http Sessions and Requests, in the form of java Objects and related to servlets, live in the server side. JQuery on the other hand will "normally" live in the client side... therefore your problem description is a bit "off".
The parameters you "send" in the httpsession or/and in the httprequest are for the jsp generation where jQuery does not play any part. What then gets sent to the browser is the HTML generated by the jsp engine. This HTML when in the browser can then be used by jQuery.
By the way the framework name is Struts and not Sturts.
Taking in account this information I don't really understand what in fact you want to do.
I want to send a JSP page which consists of some divs and table as part of AJAX response from spring framework, is there any way to send JSP as a response of AJAX call
Sending JSP by AJAX makes no sense, it is basically the HTML generated by a JSP that is sent over to the browser through AJAX, as rightly pointed out by the lost.
You do not need any server-side coding for this. All you need is to write some JavaScript on client-side to receive your HTML asynchronously. For this, I would recommend using some JavaScript framework like jQuery, otherwise, it would make your life hell.
Assuming that the page you want to access by AJAX has the link http://domain:port/mypage.htm. First you need to include jQuery in your base JSP (JSP in which former page has to load by AJAX):
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery.js"></script>
Then you need to call jQuery's AJAX function:
$(document).ready(function(){
$.ajax({
type:"GET",
url: "http://domain:port/mypage.htm",
success: function(data) {
// Now you have your HTML in "data", do whatever you want with it here in this function
alert(data);
}
});
});
Hope it helps!
Yes, there's no magic to this though. In your Java AJAX handler just return a forward or redirect to the JSP page you want. The response will then be available as the responseText in your AJAX callback.
You can use JSP to generate just the elements you need as a sort of incomplete HTML fragment then returns this from your server-side handler. Then in your JavaScript callback you can insert the fragment into your existing HTML like this
element.innerHTML = resp.responseText
//element is the parent you want to insert to
//resp is the parameter supplied to your callback