javax.faces.application.ViewExpiredException and error page not working - ajax

I set up my web app as follows:
<error-page>
<exception-type>javax.faces.application.ViewExpiredException</exception-type>
<location>/WEB-INF/include/viewExpired.html</location>
</error-page>
so that the above error page be displayed when the viewExpiredException is thrown.
However, when I restart the server and submit an ajax request on an "left-open" page, the exception is thrown in the console and my error page is not displayed.
Here is that stack trace from the console:
javax.faces.application.ViewExpiredException: viewId:/contact.jsf - La vue /contact.jsf na pas pu être restaurée.
at com.sun.faces.lifecycle.RestoreViewPhase.execute(RestoreViewPhase.java:200)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.RestoreViewPhase.doPhase(RestoreViewPhase.java:111)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:312)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at com.ocpsoft.pretty.PrettyFilter.doFilter(PrettyFilter.java:118)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:630)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:436)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:374)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:302)
at com.ocpsoft.pretty.PrettyFilter.doFilter(PrettyFilter.java:110)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter.doFilterInternal(OpenEntityManagerInViewFilter.java:113)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at com.jeanbaptistemartin.util.DisableUrlSessionFilter.doFilter(DisableUrlSessionFilter.java:70)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:525)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:662)
anyone has got any clue please?
Thanks in advance,
J.

So you want to handle Ajax errors on the standard JSF 2.0 implementation? The standard JSF implementation doesn't provide a ready-to-use component for this. In PrimeFaces for example, you could use <p:ajaxStatus> component for this. The JSF 2.0 specification however outlines the following in chapter 13.3.6.2:
13.3.6.2 Handling Errors For All Ajax Requests
The JavaScript API provides the jsf.ajax.addOnError function that can be used to register a JavaScript function that will be notified when an error occurs for any Ajax request/response. Refer to Section 14.4 “Registering Callback Functions” for more details. The jsf.ajax.addOnError function accepts a JavaScript function argument that will be
notified when errors occur during any Ajax request/response cycle. [P1-start-event] The implementation must ensure the
JavaScript function that is registered must be called in accordance with the errors outlined in Section TABLE 14-5
“Errors”
You can find here a blog of one of the Mojarra developers which contains basic examples how to show the ajax status. Here's an extract of relevance:
<h3> Status:</h3>
<textarea id="statusArea" cols="40" rows="10" readonly="readonly" />
A simple textarea, not even hooked
into the backend server data model.
Then in our javascript (for the demo,
in a separately loaded file, though it
could just as easily be in page) we
have:
var statusUpdate = function statusUpdate(data) {
var statusArea = document.getElementById("statusArea");
var text = statusArea.value;
text = text + "Name: "+data.source.id;
if (data.type === "event") {
text = text +" Event: "+data.name+"\n";
} else { // otherwise, it's an error
text = text + " Error: "+data.name+"\n";
}
statusArea.value = text;
};
// Setup the statusUpdate function to hear all events on the page
jsf.ajax.addOnEvent(statusUpdate);
jsf.ajax.addOnError(statusUpdate);
You could it as well to redirect to an error page using JS.
window.location = '/errors/500.xhtml';

Related

Execute two AJAX calls in commandButton onclick bootsfaces jsf

I have a modal with a form, and what I want is first to close the modal from bean (method "cerrarModal") and after execute the AJAX that makes the insert in database (method "crearUsuario"). I saw this question but it didn't work for me. this is my button:
<b:commandButton value="Guardar" ajax="true"
update="formTblUsuarios:growlMsg formTblUsuarios:tblUsuarios rowModal"
id="btnGuardarUsuario" look="primary"
onclick="ajax:admUsuariosBean.cerrarModal();admUsuariosBean.crearUsuario()" />
And these are my bean methods:
public void cerrarModal() {
RequestContext.getCurrentInstance().execute("$('#usuarioModal').modal('hide');");
}
public void crearUsuario() {
try {
if (this.fachada.crearUsuario(getUsr())) {
this.reestablecerClave();FacesMessages.info("User created.");
} else {
FacesMessages.warning("The user was not created.");
}
} catch (Exception e) {
FacesMessages.error("The user was not created.");
}
But server throws this error:
abr 04, 2018 11:02:14 AM com.sun.faces.lifecycle.InvokeApplicationPhase execute
ADVERTENCIA: Failed to parse the expression [#{admUsuariosBean.cerrarModal();admUsuariosBean.crearUsuario()}]
javax.el.ELException: Failed to parse the expression [#{admUsuariosBean.cerrarModal();admUsuariosBean.crearUsuario()}]
at org.apache.el.lang.ExpressionBuilder.createNodeInternal(ExpressionBuilder.java:145)
at org.apache.el.lang.ExpressionBuilder.build(ExpressionBuilder.java:171)
at org.apache.el.lang.ExpressionBuilder.createValueExpression(ExpressionBuilder.java:216)
at org.apache.el.ExpressionFactoryImpl.createValueExpression(ExpressionFactoryImpl.java:66)
at net.bootsfaces.component.ajax.AJAXBroadcastComponent.evalAsValueExpression(AJAXBroadcastComponent.java:74)
at net.bootsfaces.component.ajax.AJAXBroadcastComponent.executeAjaxCalls(AJAXBroadcastComponent.java:123)
at net.bootsfaces.component.ajax.AJAXBroadcastComponent.broadcast(AJAXBroadcastComponent.java:52)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at com.abcpagos.otis.beans.Filtro.doFilter(Filtro.java:44)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
Caused by: org.apache.el.parser.ParseException: Encountered " "; "" at line 1, column 32.
Was expecting one of:
"}" ...
"." ...
"[" ...
">" ...
"gt" ...
"=" ...
"ge" ...
"
I'm using BootsFaces 1.2.0, with PrimeFaces 6.1, JSF 2.2, (XAMPP) Apache Tomcat 7.0.56.Thanks.
First of all, the answer of #Holger is (mostly) right: your particular use case doesn't require an AJAX call. However, there are use cases requiring two successive AJAX calls, so I'll ask your original question nonetheless.
I've implemented the parser of BootsFaces in a very simple way. The onclick handler may consist of three parts: a JavaScript part that's executed before the AJAX call, the AJAX call, and a second JavaScript part that's executed after sending the AJAX request to the server. Note that the second JavaScript bit is almost certainly executed before the Java code.
In theory, I could have implemented the AJAX engine with multiple AJAX calls in mind. However, I didn't see the point to do so: It's easier to call a Java method calling two methods instead of triggering two AJAX calls, each calling a Java method and each updating the DOM.
But then... never say never. If you really need two consecutive AJAX calls, you can implement this using <b:remoteCommand>. Such a <b:remoteCommand> is a JavaScript function calling a Java method via AJAX and updating the DOM. So you can call the <b:remoteCommand> in the onComplete handler to trigger the second AJAX request.
There's that. However, you simply want to close the modal, so I recommend to implement it like so:
<b:commandButton value="Guardar"
update="..."
onclick="$('#usuarioModal').modal('hide');ajax:admUsuariosBean.crearUsuario()" />
You don't need the bean to close the modal form.
<b:commandButton value="Guardar" ajax="true"
update="formTblUsuarios:growlMsg formTblUsuarios:tblUsuarios rowModal"
id="btnGuardarUsuario" look="primary"
actionListener="#{admUsuariosBean.crearUsuario()}"
oncomplete="$('#usuarioModal').modal('hide')"
/>

partial-response on session expire

We have a JSF 2.0, Primefaces 5.0, Spring Security 3.2.3.RELEASE application.
To handle session timeout, I am using primefaces idleMonitor and p:dialog & javascript to display a countdown popup and redirect them back to login page.
I have also implemented a custom CacheControlPhaseListener so that the pages are not cached. I set the no-cache in the response headers in the CacheControlPhaseListener.
<lifecycle><phase-listener id="nocache">com..filter.CacheControlPhaseListener</phase-listener></lifecycle>
I also have error handling configured in my web.xml:
<error-page>
<exception-type>javax.faces.application.ViewExpiredException</exception-type>
<location>/error.jsf</location></error-page>
I have also implemented a ViewExpiredHandler that extends ViewHandlerWrapper
#Override
public UIViewRoot restoreView(FacesContext ctx, String viewId)
{
UIViewRoot viewRoot = super.restoreView(ctx, viewId);
try
{
if (viewRoot == null)
{
viewRoot = super.createView(ctx, viewId);
ctx.setViewRoot(viewRoot);
}
}
catch (Exception e)
{
e.printStackTrace();
}
return viewRoot;
}
The problem I am still having is:
1. When the session expires on a idle page (E.g. Search page) and if some ajax action is triggered on a page, even though I logout, when I navigate back to the page (e.g. Login-> Home-> Search page). I see a partial-response xml error:
<partial-response><changes><update id="blGridId"><table id="blGridId" style="width:100%;">
<tbody>
<tr>
<td><div id="blTableId" class="ui-datatable ui-widget ui-datatable-scrollable ui-datatable-resizable"><div id="sublTableId_paginator_top" class="ui-paginator ui-paginator-top ui-widget-header ui-corner-top" role="navigation"><span class="ui-paginator-prev ui-state-default ui-corner-all ui-state-disabled"><span class="ui-icon ui-icon-seek-prev">p</span></span><span class="ui-paginator-next ui-state-default ui-corner-all ui-state-disabled"><span class="ui-icon ui-icon-seek-next">p</span></span></div><div class="ui-widget-header ui-datatable-scrollable-header"><div class="ui-datatable-scrollable-header-box"><table role="grid"><thead id="blTableId_head"><tr role="row"><th id="blTableId:j_idt101" class="ui-state-default ui-resizable-column" role="columnheader" style="width:34px; #width:37px;"><span class="ui-column-title"><span style="word-wrap: break-word;white-space: normal;">Client </span></span></th><th id="blTableId:j_idt104" class="ui-state-default
2. If I hit a browser refresh, it loads back the page and I can continue with the actions.
Please let me know what I need to do in addition to the above to resolve the partial-response error. Do I need to add a servlet filter to invalidate the session?
I would really appreciate any help and feedback on this as it is high priority.
I had got the same issue when session had been expired. I thought it was too late, but maybe would be helpful for others who has issues like me.
The root cause is Spring Security saves the last request before redirecting client to do the authentication. After then, Spring security would try to perform the request again when user visits the page of last request. Unfortunately, the request was ajax/partial and its view was expired -> partial xml content was returned.
Easy way to get rid of this issue is removing the saving behavior of Spring Security. SavedRequestAwareAuthenticationSuccessHandler class is used to handle these kind of behaviours. Configure as:
<bean id="authenticationFilter" class="org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter"
p:authenticationManager-ref="authenticationManager"
p:authenticationFailureHandler-ref="authenticationFailureHandler"
p:authenticationSuccessHandler-ref="authenticationSuccessHandler"
p:usernameParameter="username"
p:passwordParameter="password">
</bean>
...
<bean id="authenticationSuccessHandler" class="org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler"
p:defaultTargetUrl="/"
p:alwaysUseDefaultTargetUrl="true"/>
Hope it would help.

Displaying error messages in jsp without reloading page

I have a program using jsp and servlets that checks the database for an email address. First the jsp page sends the email to the doPost method of the servlet, then the servlet checks the database, if email exists I use request.setAttribute set a success message and then forward it to the same jsp page, if it doesn't exist it forwards an error message.
The problem i am facing is that it refreshes the jsp page, whereas when I used php for another program i was able to display just by echo without refreshing the entire page.
You can use JQuery Ajax request to do this:
Servlet:
response.setContentType("text/plain");
response.setCharacterEncoding("UTF-8");
response.getWriter().write("good email or not?");
Jsp:
$('#emailButton').click(function() {
$.post('mySevlet', function(responseText) {
if (responseText == 'good'){
$('#statusDiv').text('you are good to go.');
}else{
$('#statusDiv').text('Stop Righ there!');
}
});
});

Struts2 IllegalStateException on first visit

I have a protected page that is supposed to redirect to the login screen when not logged in. The issue is that when i try to view that page, instead of redirecting to login it crashes with a IllegalStateException error. The kicker is that if I manually visit the login page it loads, then I try to visit the protect page again and it will redirect as expected. It must be some sort of session caching issue because the problem will never happen again unless i restart the browser. After a browser restart the issue will continue until I manually visit the login page again. And of course the error reporting is super vague to what the issue actually is
Aug 20, 2013 4:36:40 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet default threw exception
java.lang.IllegalStateException
at org.apache.catalina.connector.ResponseFacade.sendError(ResponseFacade.java:407)
at org.apache.struts2.dispatcher.Dispatcher.sendError(Dispatcher.java:852)
at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:534)
at org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.__invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java)
at org.apache.catalina.core.StandardHostValve.__invoke(StandardHostValve.java:127)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:662)
My Interceptor that I use to validate if they are logged in and redirect when neccessary. the very first return BaseAction.Login hits like its supposed to, and when I enable logging the struts2 loggers indicate a redirect to viewLogin is going to happen, but it doesn't. I just crashes instead:
public String intercept(ActionInvocation invocation) throws Exception {
BaseAction action = (BaseAction)invocation.getAction();
LoginManager loginManager = (LoginManager)action.getSession().get("loginManager");
Boolean loggedIn = false;
final ActionContext context = invocation.getInvocationContext();
ActionMapping mapping = (ActionMapping)context.get(ServletActionContext.ACTION_MAPPING);
if(loginManager == null && action.isLoginRequired()) {
//user is not logged in and login required, redirect them to login module
return BaseAction.LOGIN;
} else if(loginManager != null && action.isLoginRequired()) {
//logged in, make sure they are accessing a valid resource
String loginKey = action.getSessionLoginVarable(mapping);
if(loginManager.getLoginPaths().containsKey(loginKey)) {
return invocation.invoke();
} else {
//return to login which will redirect them to appropriate path
return BaseAction.LOGIN;
}
} else {
//public access to continue with action
return invocation.invoke();
}
}
Struts-config file
<package name="student" namespace="/student" extends="struts-default-custom">
<default-action-ref name="viewLogin" />
<global-results>
<result name="login" type="redirectAction">viewLogin</result>
</global-results>
<action name="*Login" method="{1}" class="controller.shared.StudentLogin">
<interceptor-ref name="noLoginStack"/>
<result type="redirectAction">viewSearchAdvisors</result>
<result name="input">/student/student-login.jsp</result>
<result name="error" type="redirectAction">viewLogin</result>
</action>
So just to summaraize. First visit to this protected page on browser startup it crashes instead of redirect to login. I manually visit login page, then try to visit protected page no crash and it redirects back to login like its supposed to. It will continue to work fine until i restart the browser.
any ideas??
This issue is resolved. I have absolutely no idea why, but when I changed my struts xml files to use this DTD file
http://struts.apache.org/dtds/struts-2.3.dtd
instead of
http://struts.apache.org/dtds/struts-2.1.dtd
the issue went away. I was grasping at straws checking every configuration file against config files of other apps i have that use the same process without issue and saw this. I decided what the hell and made the change...and it works.

Servlet --x--> Ajax: Ajax code not receiving servlet response

I'm unable to figure out what is going on here in this ultra simple example.
Problem Summary: I have a simple servlet that appears to run just fine if I drive it manually... by issuing its URL from the browser. By 'just fine' I mean: I can see in the browser HTML page whatever I write in the servlet response.
However, if I issue the very same URL via Ajax code, the servlet processes the request fine and even 'appears' to be writing out the response fine ... but, just that I do not see any response on the Ajax client code side and thus neither in my browser HTML page.
Further, if I make my XHR request syncrhonous, the browser error console shows the following exception:
Error: uncaught exception: [Exception... "Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [nsIXMLHttpRequest.send]" nsresult: "0x80004005 (NS_ERROR_FAILURE)" location: "JS frame :: file:///home/sd/Desktop/test.html :: callServlet :: line 35" data: no]
Environment:
Browser: Firefox 3.5.3
Servlet container: Tomcat 6.0.20
OS: Linux / Fedora 11
Ajax code:
<!-- test.html -->
<html>
<head>
<script>
var req;
function $(id) {
return document.getElementById(id);
}
function servletCallback() {
var field = $("debugHtmlId");
field.innerHTML += "readyState='" + req.readyState + "'<br> ";
field.innerHTML += "status='" + req.status + "'<br> ";
field.innerHTML += "responseText='" + req.responseText + "' | <br> ";
}
req = new XMLHttpRequest();
req.onreadystatechange = servletCallback;
function callServlet() {
// With async mode off, I get the
// Exception listed above.
// req.open("GET", "http://localhost:8080/aaa/bbb?f=test", false);
req.open("GET", "http://localhost:8080/aaa/bbb?f=test", true);
req.send(null);
}
</script>
</head>
<body>
<input id="callserv" type="submit" value="Call Servlet" onclick="callServlet();" />
<span id="debugHtmlId"></div>
</body>
</html>
Servlet code:
// servlet code
public class MyServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse rsp)
throws ServletException, IOException {
rsp.setContentType("text/html");
String val = req.getParameter("f");
if(val.equals("test")) {
// Increment value.
++_count;
// Return value.
PrintWriter out = rsp.getWriter();
out.printf("%d\n", _count);
out.close();
// This shows up fine in servlet log.
System.out.printf("%d\n", _count);
}
}
// This variable is incremented and returned on each call to doGet().
private int _count = 0;
}
EDIT:
Including the result: Here's what I see for example as the value of my innerHTML of my debugHtmlId element.
readyState='1'
readyState='1'
readyState='2'
status='0'
responseText='' |
readyState='4'
status='0'
responseText='' |
Strange behavior: Notice also that my readystatechange handler is getting re-entered! I mean, I was expecting to see readyState='...' status='...' responseText='...' triads for every state change...
The problem was:
I had loaded the above HTML in my browser not from the Tomcat/web server but from my local file system. I thought, for development I won't need to deploy the HTML to the server.
Since what I originally wanted works fine now, I'm not so worried now about the exception I was getting in the sync mode of ajax.
I was working through this same problem. It's definitely a cross domain issue and in my case the javascript file was the same server as the servlet and i was downloading and and calling the script from an html page that was not. Just like Harry I was loading the html locally and getting a 0 status.
The solution for me was to add
resp.addHeader("Access-Control-Allow-Origin", "*");
to the SERVLET code - this allows the javascript to call the servlet from outside the domain. It was killing me since i knew the servlet was getting hit, the server debugger was logging the GET - but the response was empty and the status was 0. all of the code on the question will work fine, if you add that header to the servlets response.
Try this: Don't call out.close(); on the PrintWriter. Instead, call out.flush();.
I look at the code and did two things to work on my CentOS 5 Linux dev box with Tomcat5:
Modified the code and rename my servlet to eServlet:
req.open("GET", "http://localhost:8080/aaa/bbb?f=test", true);
to
req.open("GET", "eServelet?f=test", true);
Modified WEB-INF/web.xml and added servlet mapping
<servlet>
<servlet-name>eServletApps</servlet-name>
<servlet-class>eServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>eServletApps</servlet-name>
<url-pattern>/eServlet</url-pattern>
</servlet-mapping>
Compiled with command:
# javac -classpath $CATALINA_HOME/common/lib/servlet-api.jar eServlet.java
Loaded it and it Works without any problems

Resources