I need know to execute one ajax function before than one jsp:include puts in same jsp.
I try put one session parameter with this ajax function, this parameter is used for include, but first jsp execute include and parameter is not put and imposible to use.
Someone know fix my problem?, thanks guys!
AJAX and JSP are completely different technologies and are run on different systems and at different times.
JSP processors are executed when the page is generated, i.e. on the server side.
AJAX functions are triggered in the browser by Javascript (hence AJAX) which is available after the page has been generated (using JSP) and delivered.
If the AJAX function triggers a JSP on the server side, the JSP or Servlet should read the needed parameter from the request first (the AJAX call would put it there), e.g. by accessing the implicit request object in an expression within the <jsp:include> tag.
Related
I am using jsp with script tag (<% %>) .And I use ajax to update some contend of jsp which have Java code with in script. In the script tag I get some session variable to process. And in ajax call I update that session variable from the action class but I can't get updated value for the front end jsp variable(inside the script tag).
Can any one tell me how do I do this?
You are trying to use javascript to update some Java variables which, fortunately or unfortunately, have already been processed by your application server and are no longer present on your output HTML page.
I think you mixed up server side and client side programming. JSP code is executed at server side and a your session data will remain at server only. All the output generated from server will be sent to browser and displayed. Although you make a ajax call, the session data will only update at server side but not update at browser unless your ajax call can handle the result from server and update the content accordingly.
I have a .jsp page where I've coded a form that submits some input to a servlet. The servlet manipulates the input and then sends a message to the same page. This is working fine, however, when I get back to the .jsp page, or in other words, when I get the answer from the servlet, the url that is visible in the browser contains the path to the servlet (e.g http:/index.jsp/servlet_name ), so, when I refresh the page, the browser redirects to the servlet and it get stuck because there was no get/post submission, so doGet() or doPost() are never active.. Is it possible to handle no get/post request in the servlet ? If not, how should I handle this problem ?
Ps: I'm using jquery mobile to build the pages (in the event of a possible solution with this framework)
I think you got it wrong. Even when you refresh, normally either doGet() / doPost() is activated. Your browser often asks you first if you're refreshing post requests.
What could be happening is your servlet is serving another request without form data, hence you get the impression it's not doing anything
I have a jsp page(calendar) with lots of JSTL tags in it.
I set the attributes in my servlet and get them in my jsp page thanks to JSTL, EL.
When I press nextweek, I open a xmlhttp which sends a GET to my servlet(Ajax).
All my attributes renew so i want to get them again in my jsp page.
I do not want to dispatch the servlet to my jsp page because of performance latency.
I don't want to fetch servlet results because they are attributes.
I just want to refresh my JSTL & EL so they will get the new values (without refreshing the page).
Is this an illogical way of thinking? but anyway, how can I refresh my JSTL,EL, scriptlets so the new values will appear?
I just want to refresh my JSTL & EL so they will get the new values (without refreching the page)
This is impossible. Note that EL and JSTL run on server side in view build time, so once they're applied when generating the server response, they can't be updated in the page until the server generates new content using the view (basically, your JSP with JSTL, EL and other components)1.
You should look into AJAX requests to your servlet (or the controllers you're using) and probably handling a JSON response to resolve the behavior of your JSP page.
More info:
How to use Servlets and Ajax?
1 Scriptlets also fall in server side category but I omit them since you should not use them for being highly discouraged to use in modern Java web development. More info How to avoid Java code in JSP files?
HttpServlets, JSP, JSTL, EL, and scriptlets are all server side components, ie. get executed on the server to produce an HTTP response. Javascript and AJAX are client side components, ie. work on the returned HTTP response.
You cannot refresh my JSTL & EL on the client side because they simply do not exist.
A possible (and common) solution is to have the request you make with AJAX produce a JSON response which you use to populate/replace HTML elements, where your EL had previously been used to set a value.
I am new to servlets, and would like to follow the Model2 paradigm by keeping all my "code" in servlets, and html/beans in jsp pages. But, is there a way to run a servlet when I access a jsp page without using a form submission. For example, I have a login page. If the user logs in and then somehow goes back to the login page I want to check for the existance of their session and automatically move them on to their welcome page. This is one real world example, but it seems it would come in handy to run code without having to submit a form for a multitude of reasons.
you dont have to submit a form to invoke a servlet. All you have to do is have the browser hit the url that is mapped to the servlet. That could happen when submitting a form, clicking a link, invoking an xhr, using curl or wget from the command line, etc.
Also, keeping all code in servlets is not good design. Your servlets should handle incoming requests, invoke business logic implemented in separate classes (for good modularity and testing purposes), and return the appropriate response.
If I recall correctly, in Model2, the user never navigates to (JSP) pages - only controllers (servlets). Trying to access a lower layer of code (a servlet) direcltly from a view (the page) is a violation of MVC/Model2.
I need to get userPrincipal inside a piece of Javascript code. My app currently uses Dojo on the client side and servlets on the server side. I am not using JSF, nor JSP (and if possible I would try to avoid using it only for this purpose), nor JQuery (I would avoid mixing JQuery with Dojo).
I have read this interesting post Mixing JSF EL in a JavaScript file . Following the idea #1, I have written a Bean but I am not sure on how to embed the call to the Bean in my (quite long) js file.
If you don't use JSF or JSP or anything similar, just bare servlets, I recommend creating a servlet which takes the userPrincipal name from the HttpServletRequest (or any other place) and returns it in JSON format. Then you can do an AJAX call to the servlet and find out what you need. I don't know Dojo too well, but I believe you can insert this AJAX call in your event chains pretty easily.
I don't recommend mixing managed beans with plain-old servlets.
If AJAX is not an option for you because of its asynchronous nature, you cand create a servlet that serves text/javascript content which offers the principal's name in the form of javascript code and then add a script tag to your page.