I am wondering if it is possible to pass an object using Gson (probably) from a JSP page where my app is located to the scope of my app? (In a nice way)
I want to avoid using an AJAX request to do this.
This sounds like the common pattern of inlining your data in JavaScript to avoid making an initial AJAX request.
In your JSP, you can write out the JSON into the body of an Angular service module as array or object hash data. The service module would just return this inline data and then you can make it available to the rest of your Angular app via service injection.
As Will suggested, make the JSP variable a part of the markup, the server will replace the relevant part. Psuedocode follows:
<%String contextPath=request.getContextPath();%>
<script src="<%=contextPath%>/static/js/lib/angular.v1.2.16.min.js"></script>
<script>
app.service('contextService', [function() {
this.getResourcePath = function() {
return '<%=contextPath%>/resources/' ;
};
}]);
</script>
Related
<%!
public void runJavaMethod(int id)
{
%>
<%
try{
String icd = request.getParameter("icd");
String inm = request.getParameter("inm");
String istk = request.getParameter("istock");
String sstk = request.getParameter("sstock");
String upr = request.getParameter("uprice");
String spr = request.getParameter("sprice");
r = s.executeQuery("select * from itemsyncdata");
while(r.next())
{
s.executeUpdate("update itemsyncdata set itemcode='"+icd+"',itemname='"+inm+"',instock='"+istk+"',storestock='"+sstk+"',unitprice='"+upr+"',storeprice='"+spr+"' where id='"+a+"'");
}
}
catch(Exception e)
{
e.printStackTrace();
}
%>
<%!} %>
And I am Calling Function from html like
<input type="submit" id="btnSync" value="Sync" class="button" name="Sync" onclick="<%runJavaMethod(r.getInt(1));%>"/>
So We want runJavaMethod Parameter.
onclick="<%runJavaMethod(r.getInt(1));%>"/>
HTML/Javascript Plays on client side and JSP/Java plays on server side.
Simply you can't. You might misunderstand that JSP and HTML/JavaScript existed on same document. Yes but JSP part compiles on server side itself comes to client.
What you can do is you have to make a server request. Most probably look at Ajax requests.
You are trying to mix up two languages i.e., Java and Javascript/html together i.e., onclick is a Javascript event and you can't call runJavaMethod from Javascript.
In simple words, you can't directly call a Java method (present inside the scriptlet) using Javascript because all of your JSP code produces (becomes) html when it is loaded from the server.
So, if you have to fix the issue, upon onclick, you need to call an URL which hits a servlet/controller method on the server to do the job (i.e., executes the business logic).
One more important point is that Scriptlets are legacy code and I strongly suggest not use them to generate or manage the html content, rather use JSTL so that there will be a clear separation between the concerns (i.e., business logic and user interface requirements).
Also, I strongly suggest you read the JSP best practices from here and follow them.
You are mixing to two different things. JSP is server side code and it's rendered response is the HTML that is send back to the browser.Javascript is pure client side in your case.
If you really want invoke a server side processing than create a simple Java script function with Ajax call and get response back which u can use it.
I suggest send all the logic of JSP in backend class not a good practice to put in the jsp. JSP is ideally for UI design.
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);
}
});
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 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