I want to get the URL accessed by the Struts2 web application. For instance, if user accesses http://www.webpage.com or https://www.webpage.com, I need the protocol; http or https. I tried implementing the action with ServletRequestAware and using request.isSecure() or request.getScheme(). However, I am getting false and 'http' even if I access 'https://..' link.
Is there any way to get the protocol ?
You can extract it from the header:
System.out.println(request.getHeader("referer"));
OUTPUT:
https://www.webpage.com
I think you forgot to try one more thing.
Implement Action with ServletRequestAware and use
request.getProtocol();
Else you can use the following code in your action class:
HttpServletRequest request=ServletActionContext.getRequest();
String protocol=request.getProtocol();
System.out.println("Protocol Used::"+protocol);
Related
How can I get the request header in Struts2 actions I can not use request.header("") method because in struts2 request is just a Map<String,Object> thus this method is not available.
After checking in the internet, I found that I need my action to implement ServletRequestAware, which lets an interceptor to push the HttpServletRequest in your action. later I can use the same. Thanx all for replying .. :)
public class CategoryAction implements ServletRequestAware{
// Your code goes here...
}
Thanks in advance for reading my question.
Question: I want original url in my strut's interceptor.
E.g:
From the browser, suppose i called... http://www.fakedomain.com/mycode/test.html
But this url is a url without rewrite...
Actually, Struts framework rewrite this into http://www.fakedomain.com/CreateMyCode.action
So, sceanario is...
When i fired the url from the browser...
First, Strut's filter comes in a picture and it determines weather url rewrite is required or not. If it requires then
Again same strut's filter is called modifdies into another url.
Finally, strut's interceptor comes in a picture. But now when i tried to get url... i got modified url.
But i want the original url (which is not rewrite by struts...and which is actual called from the browser).
How can i get this?
My Try:
I have created one new filter... which comes before urlrewrite filter. And set some attribute like...
HttpServletRequest httpReq = (HttpServletRequest) request;
String uri = httpReq.getRequestURI();
httpReq.setAttribute("URLBeforeRewrite", uri);
But in interceptor when i tried to get the value like...
req.getAttribute("URLBeforeRewrite")
i got modified url.
now, how can i solve this???
I'm using Symfony 1.4 and i want to forward in my controller to another controller and action with some parameters.
After creating a "bike" with "bike/create" i want to forward to "bike/show/id/X" with the id i got from my new bike instance.
$forwardString = 'bike/show/id/'.$bike->id;
$this->forward($url);
This does not work :-(
Maybe you can help! :)
Greetings!
The method definition for forward is public function forward($module, $action) the request should be preserved and if theese are things not currently in the request you will have to add them first.
Also you might even need redirect instead so the url changes and where you just give it the url public function redirect($url, $statusCode = 302) so usage would be $this->redirect('bike/show?id=' . $bike->id);
is there a way to access URL parameters in a Jetspeed2 Portlet/Portal?
like: www.bla.com/portal/page.psml?param=12345
I can only find some tools for liferay (PortalUtil.java) to access the httpservletrequest, but as far as i know there is no such thing for jetspeed?
I thought the public render parameters can be used for such thing, but i'm a little confused here? Didn't anyone had this problem before?
thanks in advance :)
found the answer:
Until version 2.1, Jetspeed merged portal request parameters with portlet specific
parameters, effectively allowing "shared" parameters.
This is not compliant with the JSR-168 PLT.11, so by default this is now disabled.
By setting merge.portal.parameters.with.portlet.parameters=true this feature can
be "restored".
In the situation of portal and portlet parameters with the same name, by default
the portlet parameters will be provided first in the values array, but this too
can be overridden by setting merge.portal.parameters.before.portlet.parameters=true
Setting both these properties to true will deliver the "old" pre-2.1 behavior.
Note: for individual portlets, these global settings can be overridden by setting these properties as metadata in jetspeed-portlet.xml
merge.portal.parameters.with.portlet.parameters=false
merge.portal.parameters.before.portlet.parameters=false
To use public render parameters from the 2.0 spec
Don't know if it works for jetspeed, but you can try getting the httprequest like this:
HttpServletRequest httpRequest = (HttpServletRequest) request.getAttribute("javax.servlet.request");
Then find the query string in a header like this:
String referer = httpRequest.getHeader("referer");
You'll then have the full page referer so you can parse the query string.
I am using Fiddler to debug my MVC application and see all the HTTP requests.
The application is running on http://localhost:51234/mvc
As anybody who has tried to use Fiddler for a site hosted on localhost knows there is an issue - windows won't forward localhost traffic through the proxy if you hit this link directly. You can work around this in several ways, such as my prefered way of using the URL http://ipv4.fiddler:51234/aboutus/contact. This works great.
The problem was I started doing AJAX using :
<% using (Html.BeginForm()) { %>
If you view the source generated it has actually generated this :
<form
action="http://localhost:51234/aboutus/contact"
method="post"
onsubmit="Sys.Mvc.AsyncForm.handleSubmit(this, new Sys.UI.DomEvent(event), {
insertionMode:
Sys.Mvc.InsertionMode.replace,
onFailure:
Function.createDelegate(this,
submitComments_failure), onSuccess:
Function.createDelegate(this,
submitComments_success) });">
Oops!
It generated localhost instead of ipv4.fiddler:51234. So of course when I run the AJAX query Fiddler doesn't see it.
In theory using the machine name should work, but the WebDev.WebServer won't respond if you try to hit directly the machine name http://win-538lf:51234/aboutus/contact
[Fiddler] Connection to
win-538lf failed. Exception
Text: No connection could be made
because the target machine actively
refused it
fe80::81fc:8f0f:457a:27df%12:51234
Is there a workaround for this?
is it possible to configure WebDev.WebServer to respond to the machine name? Or am I going to have to create a virtual directory or fake host ? I'd prefer not to do that, but i guess its not a big deal.
So it turns out that if you use this overload :
using (Ajax.BeginForm(new AjaxOptions()
you get this code generated (the code that 'breaks'):
<form action="http://localhost:51234/aboutus/contact" method="post"
but if you do this and include the action name:
using (Ajax.BeginForm("Contact", new AjaxOptions()
you get this code generated :
<form action="Contact" method="post"
So i'm fine with that for now, but would welcome any other solutions.
Have you tried the Firebug extension for Firefox? It can show the Ajax request and response. I've used it with ASP.NET Ajax. Not sure about MVC Ajax.
You could write a HTTPModule which uses a response filter in order to manipulate the HTML output replacing all "localhost:51234" strings.
The HttpResponse class has a very useful property:
public Stream Filter {get; set;}
MSDN provides a helpful description of this property:
"Gets or sets a wrapping filter object
used to modify the HTTP entity body
before transmission."
Here is a nice article which gives some background how you could do this: Implementing an IIS Application Filter Using .NET HttpModules and Response Filtering (page 3)
As described in the object browser,
the Filter gets or sets a wrapping
filter object used to modify the HTTP
entity body before transmission’. This
is exactly what we need to do in order
to make modifications to the HTML
output of the HttpHandler. The Filter
property is declared as type
System.IO.Stream. In order to assign
our own class to this filter property
we need to define our class as
inheriting from System.IO.Stream:
public class PageFilter : System.IO.Stream
We now have a Stream class,
PageFilter, which can be assigned to
the Response.Filter property. By
attaching PageFilter to the
Response.Filter property, PageFilter
will be notified at critical times as
data is written to the Response
buffer. The most significant event of
course is the Write operation. When
this method is called, you’ll have the
opportunity to modify data as it’s
being written to the Response buffer.
(I combine this with 'Response.Buffer
= true' so that my PageFilter receives the complete response stream in a
single method invocation.):
public override void Write(byte[] buffer, int offset, int count)
In the HttpModule, at the start of the request (I do it in OnBeginRequest) simply attach your HTTP response filter by assigning a new instance to Response.Filter:
httpCtx.Response.Filter =
new PageFilter(httpCtx.Response.Filter)
This other article shows a full working example implementation:
http://aspnetresources.com/articles/HttpFilters.aspx
I hope this helps!