I've developed a site in struts2, but often, there comes many wrong requests. After 20 or 30 hrs, my server is getting stuck. I want to handle this type of error.
There are given some errors displayed in the server:
1)There is no Action mapped for namespace /operators-in-java/operators-in-java/text and action name javascript. - [unknown location]
.........
2)There is no Action mapped for namespace /super-keyword/text and action name javascript. - [unknown location]
3)There is no Action mapped for namespace /operators-in-java/operators-in-java/history-and-features-of-java/text and action name javascript. - [unknown location]
etc.
I've performed global exception handling as given below:
<global-results>
<result name="excepHandler">/handler.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping exception="java.lang.Exception" result="excepHandler"/>
</global-exception-mappings>
But it doesn't handle, this type of error. Thanx in advance.
Probably the easiest way is to use wild cards. Simply make a action for "*" and have that direct to your error page. All actions with more specific names will override that case so it should be straight forward.
Does your clients get the 404, not found error?
You could configure your web.xml to redirect to an error page when somebody requests an invalid resource.
<error-page>
<error-code>404</error-code>
<location>/notFound.html</location>
</error-page>
But, I would try to fix whatever is causing the incorrect requests, instead of creating a catch all action. Apparently you've configured struts2 to handle every request, I would check on that first.
Related
The Failed Request Tracing works fine to log failed rewritings. I can view the log files and at least understand why the rewriting not working.
The regex pattern has passed a matching test OK, but the input URLs I enter in the browser are not even captured. All the URLs mapped to controller actions are captured (my website is an ASP.NET MVC app) however the URL I need to be rewritten is not mapped to any, the page shows 404. But I expect it to be rewritten to some working URL (to test it out, so that instead of showing 404, it should be rewritten to the target url and shows the returned content from there).
My URL rewriting rule uses a Reverse Proxy template.
I hope what I understand here is correct. How can I diagnose this? My purpose is just to do something like turning IIS into a reverse proxy by capturing all requests through my website, check if any matching the defined regex pattern before requesting to the rewritten URL and returning back the content to the original client.
Update
For those who may not know about ASP.NET MVC:
Suppose my website has a controller called HomeController with action Index and the URL mapped to it is /Home/Index.
The URL I mean not mapped to any action here may be /Home/TestProxy
Here the HomeController does not expose any action TestProxy and also there is not any routing rule maps /Home/TestProxy to any action. So in this case the ASP.NET MVC app will shows a 404 page.
As I said above, for the URLs mapped to a valid controller action, they seem to be captured and considered as input URLs (before being checked against the rewriting rule). But for URLs not mapped to any controller action (showing 404), I don't see any logged as input URLs (so of course they will not be checked against the rewriting rule and it won't work).
Update 2
Actually my tested URL is not even mapped to a controller so looks like it's not logged by the Failed Requests Tracing rule I setup. Now I've tried using a URL mapped to a controller but not to any action (it still shows 404) but successfully logged by the Failed Request Tracing rule. I can see this in the log file:
<EventData>
<Data Name="ContextId">{80000034-0000-F000-B63F-84710C7967BB}</Data>
<Data Name="Pattern">testproxy/(.*)</Data>
<Data Name="Input">home/testproxy/index</Data>
<Data Name="Negate">false</Data>
<Data Name="Matched">true</Data>
</EventData>
So it reported a matched pattern, but still the page shows 404, whereas I expected it to return the content from the rewritten url (which should be http://10.0.0.5/index - I confirm that this URL works if requested directly, it's just another simple published website on local network). I don't even know if this is possible now or I did something wrong here.
URL rewrite inbound rule get executed before controller action.It is appreciated if you could post full error message in your Failed request tracing log.
If the request http://10.0.0.5/index but return 404 from IIS. It sounds like your ARR proxy was not enabled at all. First of all, please try to disable reverse proxy outbound rule. Then go to IIS manager->server node->application request routing cache->Server proxy setting->Enable proxy.
Update:
The rewrite rule has worked and it seems that the request was stopped by
[HttpException]: The controller for path '/questions/62078944/iis-url-rewrite-not-working-the-input-url-not-even-captured' was not found or does not implement IController.
at System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType)
at System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory)
at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state)
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step)
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
-->"
I want to know if there is a way to download the generated HTML from a Struts2 action (the final HTML after processing the jsp), using the normal result types. I don't want de page to be displayed, but instead to download it.
What I have tried:
<result name="success" type="stream">
<param name="location">/jsp/dummy.jsp</param>
<param name="contentType">application/text</param>
<param name="contentDisposition">attachment; filename="page.html"</param>
</result>
I donĀ“t want to use something like:
UrlReader.read("generateHTMLAction")
and putting that in an input stream, because I'm having some encoding issues.
Any idea will be apreciated.
Thank you everyone.
The handling of the returned response is mostly determined by HTTP headers and how they are interpreted by the browser. So, you can use the standard dispatcher result type and a JSP, as far as I know. You can use JSP directives to set the appropriate HTTP headers to make the browser treat the response payload in whatever manner you like.
I'm not aware IF or HOW this can be accomplished;
what I can say to you is that:
it does not exist any location param here: it is from other result types (DispatchResult, FreeMarker, etc), not from Stream type. Nice try, but I'm pretty sure there is nothing out-of-the-box among Struts2 result types that fits your needs;
You can't use Interceptor's PreResultListener feature, because it works on the final Result but before it is rendered, then jump off the Interceptors completely;
My 2 cents:
IF you want to do this for debug purpose, and not programmatically, and then the real problem is that you can't use View Source browser's feature because the result is already "contaminated" by the browser parsing, THEN you can try to use the PlainText result type (untested, it's just an idea, I've never used it), to get the raw content of the JSP eventually setting your charSet, and then read the raw JSP with an appropriate editor (Eclipse, Notepad++, etc).
I have 2 packages in struts-config file one for login and online for the rest of the pages, once I have processed the login credentials I want to forward the result to a jsp in the online package. How do I do this?
<result type="redirect" name="overview">overview</result>
I cant simply use overview as overview is in online package and this result type is defined in the login package.
See the results docs on the Struts 2 wiki.
First, you need to use the "redirectAction" result type since you're redirecting to an action. Second, you need to use the "namespace" attribute to explicitly define the result's namespace, or it will default to the current one.
Here is a simple question. Is there any possibility of, if in any case there's an error in an application and the server show us an error page, instead redirect everything to a default page ?
Covering all errors.. is that possible ?
Grails already does this for you. If an exception bubbles up to the container, it gets handled as an HTTP 500 (Internal Server Error). With conf/URLMappings.groovy you can control what happens what happens when error statuses occur.
Here's the default mapping for 500 responses (from conf/URLMappings.groovy):
"500"(view:'/error')
This tells the application to render the error view, which is located in views/error.gsp. If you want to change this, you can. You could redirect to a controller/action if you want:
// will go to 'custom' action of ErrorController, which you would create yourself
"500"(controller: "error", action: "custom")
You can configure this for any HTTP response status. See the URL Mappings documentation. If you need finer control over different exceptions that might be encountered, look at the "Declarative Error Handling" section in the referenced docs above.
I want to get the current request uri from inside a jsp page in a struts2 webapp. I can do this in an action using the following:
ServletActionContext.getRequest().getRequestURI()
... and I supposed technically I could then create a property of the action that just returns that value but I would rathern not write it into the action, is there a way I can access the same value in a jsp using an ognl expression?
EDIT:
After a bit more playing about I have realised that even if I can get the HttpServletRequest.getRequestURI() value in the jsp page by the time I get it, it will have changed to the path of the jsp not the original request URI so will not be what I want.
So instead what I have done is write an interceptor that grabs the value early on before the request is dispatched to the jsp file and then save it in the value stack which I can then refer to it in the jsp. This may be the only solution but if anyone knows different then do enlighten me.
<s:url/>
This question Tiles2 Struts Switch Locale shows an application of the tag and shows how to maintain the parameters on the url (which might be useful for later).