Is there a JSP tag that allows that part of the page to process asynchronously? If not is there a way to create one?
Something like the following:
<Util:Asynchronous>
<%
DataSource source = ...
%>
</Util:Asynchronous>
... rest of page
I don't want to have to wait for the data source to finish before processing the rest of the page.
Thanks.
You need to use AJAX. Also it is bad coding practice to put your data access commands in your JSP pages. Very susceptible to hacks, not to mention performance hits.
The servlet container must wait that you are page is entirely rendered before sending it back to the client. Thus your requirement makes no sense for me.
If a part of the page takes too much time to process, you could use Ajax to load a portion of your page. There is already pages at StackOverflow about Ajax.
Related
I wonder if there is a way to load, interpret and render a .xhtml file from a servlet.
What I want is to have a xhtml file witch contain for example a custom row within a list, to be loaded from a servlet or a bean, to be interpreted, and to obtain the html result.
Sounds to me like you're not in need of Servlets or JSF, but rather JSP (should look familiar, coming from PHP).
Notice that people here will tumble over themselves telling you to not put the logic into the page (and for good reason), but I think with your background, you'll be helped by starting from a JSP only approach. Than start moving the logic to a servlet. You can use a number of techniques to communicate the data between the servlet and the jsp. I like to use
request.setAttribute("someString", valueObject);
In your jsp you than can use:
<%= request.getAttribute("someString") %>
Calling your jsp from the servlet is done via
RequestDispatcher rd = req.getRequestDispatcher("/path.to.your.jsp");
If you really need a .xhtml file (your original question), you can use this last line as well, but it'll be real xhtml, like it would be served from a plain webserver (!= appserver)
I have a web page where a part of the page is loaded via AJAX,
but I also wanted to make it work if JavaScript is not enabled.
so, i wrote the code as
<script>
fetchCitiesAndDisplay ();
</script>
<noscript>
<%
out.print(Cities.getHtml ());
%>
</noscript>
where
fetchCitiesAndDisplay ()
is a javaScript Method, which will get the content (executes the code in Cities.java) and displays it.
Cities.java
is a Java class which will collect the cities from database and generates the html content to be displayed.
When JavaScript is off, everything works great.. as the ajax call will not be executed and only Cities.getHtml () will be called once.
but, when JavaScript is enabled, The Ajax call is also executed and Cities.getHtml () is also called.. even-though the display looks right because of tag, Cities.java will get the call twice for the same data, and it eats up a lot of time...
How to solve this?
I'm wondering if maybe you don't understand how <noscript> works. It doesn't come into play in your server-side template so whatever server-side logic you put in there will ALWAYS execute whether javascript is ultimately enabled or not in the browser.
Therefore, this doesn't make a whole lot of sense to me because the HTML inside <noscript> is always going to be generated whether javascript is on or off. It will always be in the page. So, if it's always in the page, then why fetch it via ajax when javascript is on?
i have to agree with #jfriend00, its not possible to identify if javascript is enabled using 1 page
You can use javascript to set a hidden variable with some value, when the page submits you can check for it and identify if javascript is enabled or not.. but this requires 1 request/response cycle, may be you can have an interim page which forwards the request to the intended page (with the hidden variable i discussed above).
Why not have your javascript code load a a tiny html/image/etc snippet on page load. If javascript is enabled, you can see that file was requested. If it's disabled, there will be no request for that file.
I am working on JSP-servlet application and now I am programming a page which edit information about registrars.
The senario is that there's an ArrayList I send form servlet to JSP page when I load the page. The ArrayList contains information about groups the registrar belongs, the ArrayList is resulted after making multiple SQL statements. When user try to edit some fields and make one required field empty and submit the form, the servlet makes validation and return error to edit page.
The problem I face is that all the groups I sent in the first time fly in the sky. So I have to make connection to DB again and make multiple queries to get the groups again and send it back to JSP page.
Is there's another simple way to make arrayList stable in JSP page ?
EDIT
Here's the code which I make the scope of the ArrayList in the session.
<c:set var="userGroups" value="${userGroups}" scope="session"></c:set>
Either store it in the session or just live with it. I really don't see any issues with that. If the concrete problem is that you have to copypaste the same code again or that the whole code is ugly to have in a Servlet class, then just refactor/hide that into an useable DAO class which you import/call/reuse in the Servlet the usual Java way.
Update: as per your update, this doesn't make sense. You just need to change your servlet code from
request.setAttribute("userGroups", userGroups);
to
request.getSession().setAttribute("userGroups", userGroups);
You don't need <c:set> for this.
I am interested in creating a simple web application that will take in user input, convert it to an XML file and send the file to a database.
Coding wise I feel I am okay, it is just the general setup and what implementation to use I am a bit unsure of.
At the moment I have a JSP page containing a form, the user fills out the form and on submit a POST method is sent to a servlet, in the servlet doPost() method the servlet is instantiating a java object and passing it the user inputted data. The java object then writes that data to an XML file and sends it to the database via REST.
All I would be interested to know is if this the standard/optimal way of creating such a web application.
Any and all feedback is appreciated.
Thanks
For a "simple webapplication" this high level approach looks fine in general. However, if you want more critical feedback, you'd need to give more details about the low-level approach. It may for example happen that it isn't memory efficient and thus may break when the webapp is been used by over 10 users concurrently, just to give an example.
I only question the choice for the GET method. You'd normally only use it to retrieve data (SELECT), not to create/alter data (INSERT/UPDATE/DELETE). For that you'd normally use POST, so that no one can execute it "accidently" by just clicking a (bookmarked) link. Changing GET to POST isn't that hard, add method="post" to the <form> element and rename doGet() to doPost().
As title says, how do I call a java class method from a jsp, when certain element is clicked (by example an anchor)? (Without reloading the page)
If this can be done, how I pass the method, some part of the html code of the page that invokes it?
Im using jsp, servlets, javascript, struts2 and java, over Jboss AS.
What you want to do is have javascript fire off an AJAX request when the said element is clicked. This AJAX request will go to the server which can then invoke any java code you want.
Now you can build this all yourself or you could use one of the many off the shelf solutions. I would recommend Googling around for a JSP Ajax tag library. Like this one http://ajaxtags.sourceforge.net/ .
As Marko pointed out, you might need to read some more about the client/server separation in web programming. If you want a framework to help you do remote Java invocation from Javascript, have a look at DWR.
You can use a Ajax call to do it .
Now when the HTML object is clicked call a java-script. Then in the JavaScript make a Ajax call to the servlet something like this
$.get("Query?ID="+id ,function(RespValue)
{
}
Here Query is my servlet mapping defined in web.xml and Id is the parameter i am passing you can sent multiple parameters too. and RespValue is the response value returned from the servlet.
In the servelt write a do Get method and execute your java code. If you want to return some value use the function(RespValue) else remove it.
You can't execute server side java code in client browser.
What you can do is to perform new http request that will perform some action on server and return the action result.
Given the tone of the question better go read some JSP tutorial. No forum post answer will explain it better.