How to get the url with freemarker template? - java

I want to fetch current URL with free marker.
Also i need to split the url href part.Is there any method similar to JavaScript method location.href.
Ex: http://www.barcamp.com/homepage/overview.go.
Here i need to fetch the url & also to the split the url and get its as /homepage/overview.go
Please help us on this issue

Getting the current URL falls outside FreeMarker's jurisdiction, since it's independent of Servlets and HTTP. It's up to the Web application framework if and how it exposes that information for the templates, so check the documentation of the framework.

Related

how to rewrite the url in java web application?

On Form Submit my url changes from
localhost:8080/Workflow/admin/GetReports?fname=Form1
to
localhost:8080/Workflow/admin/EditReport
Form action is EditReport(Servlet Name).
Now on EditReport i perform the databse operations and forward the request to the GetReports?fname=Formname Servlet using Request Dispatcher.So that i am on the same page which is the first one (1) i started from.
Now Everything works fine on the .jsp page But the url remains unchanged that is the second one (2).
So how to rewrite the url i.e. from admin/EditReport to /admin/GetReports?fname=Form1
Are you using dispatcher.forward because you are setting some Attributes in
the Request?
If not, then you don't need to use Forward. Instead of that, use response.sendRedirect("url for GetReports?fname=Form1")
But If you are setting some Attributes in the request, then I am wondering if your workflow is a correct one because URLs like this "Workflow/admin/GetReports?fname=Form1" should Not be arrived upon after doing some processing. They should be simple HTTP GET requests only.

How getServlet request dispatcher by URL?

I need to populate some request with data and redirect back. Is there Spring RedirectAttributes analog for Java EE? I have searched and found 2 solutions, but they also have limitation:
Response.sendRedirect()
In this case I will lost all destroys request attributes. I can use Session attributes but in this case I need some mechanism that can determine when redirect comes in or when there is no redirect and data must be removed.
getRequestDispatcher(String path).forward(request,response)
The problem with path - I need to send redirect to URL not to give something jsp or Servlet by name. Is there any way to "convert" redirect URL to path? For example how I can go forward to
"http://localhost:8080/WebAppname/"?
You can use the sendRedirect and pass the parameters as part of the query string. So what you would be redirecting can be something like below
http://localhost:8080/WebAppname/myRedirect.action?param1=value1&param2=value2

How do I send a list to the jsp that requested it?

Let us suppose there is a jsp that needs to display the list of files shared shared by a particular IP. Client opens that jsp on his local server and the request is sent to a remote server to fetch the list.
Servlet on the server processes the request and fetches the list of files shared by that IP. Now how do the servlet send that list to the jsp page that requested it ?
JSP :
connection.openConnection(); // Connection sends IP as the query parameter to the
// remote servlet
Servlet :
doGet(..parameters..) {
list = getList(forThatIP);
// NOW HOW DO I SEND THIS LIST ?
}
One method that I thought was to send the whole data as a query string to a servlet on the client side(running a server such as tomcat) and then stash that list onto some file.Later jsp can parse the file and display the data. But that doesn't seem to be a good option.
Note: JSP is invoked when a servlet forwards after successfully sending the IP to remote servlet
You can use request.setAttribute() in the Servlet. Then you can use a JSP tag to retrieve the value in JSP. Investigate a bit on that.
EDIT :
In the Servlet doGet method, you can set the an attribute, say listOfFiles as:
resquest.setAttribute("listOfFiles",list);
Then in the JSP you can retirieve it using the EL expression:
${listOfFiles}
which is an inbuilt feature of JSP.
Alternatively, you can access it using
<% request.getAttribute("listOfFiles")%>
but it is bad to embed Java scriptlets inside JSPs.
If you're passing complex data (lists etc.) over a connection, you can look into using JSON or XML. Your JSP code should be able to parse either easily with the right library.
Actually it depends on how the jsp is invoked.
Do you perform a post onto it ? Does the servlet perform a forward ? Or is a sendRedirect() ?
According to this there are many ways to send data to your jsp. One is using request attributes or better, using request scoped beans. One other can be posting some sort of rappresentation of your list as a post parameter (be it json, xml or some custom format of yours).
Another thing to consider is, what are you using ? JSF ? Some Spring library ? According to that there may be other better (or worse) ways to send datas.
If working with simple JSP/Servlet on a small project I'd personally go with the request.setAttribute() way, indeed this will force me to invoke the jsp via something like
request.setAttribute("myList", yourListObject);
request.getRequestDispatcher("yourjsp.jsp").forward(request, response);
then in the jsp you can:
<% Object myListObj = pageContext.getRequest().getAttribute("myList"); %>
When you call a servlet from code, the best way is to use an HttpServlet and mimic the browser behavior.
Passing the data in the URL is a GET request. Useful only with relatively small chunks of data, but the semantics look like you want ("get a bit of data"). Something like http://myremoteserver.com/myServlet?ip=.... You will need a proper encoding of the parameters (the Java API can handle that).
Passing the data by writting into the stream will be a POST request. It has no limit in the data passed to the server, but the semantics is different ("change something in the system"). You could pass your data as a parameter inside the contents written, I am not really sure how to decode that. Another alternative is using a Servlet (no HttpServlet) and just treat the raw data.
In both cases the response will be returned in the connection output stream. You can use whatever format you like (an standard one like JSON will probably be best, even that defining your own XML). In this case, a viable alternative would be filenames separated by a 'safe' character (like \n or |). Quick, but it will be less flexible in the future.
I would go for a GET request and JSON encoding.

Simple example of JQuery Address to manage application state

I'm using the jQuery Address library to re-write my URL depending on what the user is doing in the page. The intention is to allow users to bookmark pages and come back later. The page never refreshes as all server interaction is done via ajax.
jQuery Address is writing URLs like this:
http://localhost:9000/#/u/scott_tiger
I need a to set up a route in Play to be able to route that request through to the appropriate controller. So I set this up:
GET /#/u/{username} Controller.showUser
This doesn't work though, the route definition gets ignored. I've tried loads of things such as trying to escape the "#" and replacing it with a variable that I've populated with Character.toString(35). None of this works.
Does anyone know how I can either define the route properly or get jQuery Address not to write the "#".
EDIT: The "#" doesn't get sent to the server does it. Doh! OK, question is revised.
No. The # and the part of the URL after that is not sent to the server. So your play app on the server will never see such URLs.
HTML5 solution
You need to handle these URLs on the client side using JavaScript. In modern browsers with good HTML5 support, you can modify the address without reloading the page. See Manipulating the browser history on how to do it for these browsers. And see When can I use... for browser support.
#-URLs
On Internet Explorer and older versions of other browsers you need to use # URLs and use JavaScript to load the state (e.g. get the user page /u/scott_tiger in your example). See How to run a JavaScript function when the user is visiting an hash link (#something) using JQuery? for how to do this in JavaScript. Also if a user bookmarks a page with a #-URL you need to reload the state.
See also: What's the shebang/hashbang (#!) in Facebook and new Twitter URLs for?
JavaScript libraries
You may use JavaScript libraries to handle this for you history.js is an example. Also bigger frameworks like Backbone.js handle this.
Does anyone know how I can get jQuery Address not to write the "#".
If you don't write the #-part of the URL, the state can not be linked. So you can not get back to e.g. Scott Tigers profile page if you bookmark the page, because the URL is only http://localhost:9000/ and you will arrive on the front page, while the user though he would arrive on the profile page.
Armed with my new understanding of URLs (thanks #Jonas) I realised that I'd missed half of the story.
I'm using JQuery Address to change the URL depending on what you click in the application. This works great and on lots of browsers. What I was missing was using JQuery Address to watch for external address changes (bookmarks, history, back/forward) and respond accordingly. i.e. set the page up correctly by firing the appropriate Ajax calls and rendering that data appropriately.
Changing the address
$.address.title("new title describing application state");
$.address.parameter("q", "val1");
$.address.parameter("g", "val2");
$.address.update();
Restoring the state
$.address.externalChange(function(event) {
var val1 = event.parameters["q"];
var val2 = event.parameters["g"];
// do something with those values
});

Modify the url of the web application with jboss 4.2.3

i'm need to changed or hide the url of my web application deploy in jboss to the user because it's to long here's an example
my current url is:
www.example.com:8180/blazedsExample/Example/Example.html
i just want that the url was
www.example.com
thansk for the help
You can use many free url shorten services.Goole is also offering such kind of service.
Use http://goo.gl/ and enter your URL there , that will give you a shortened url.
To use this service in your application,call /action/shorten action and pass input text field (id) replaced with your value(www.example.com:8180/blazedsExample/Example/Example.html).That will return shortened URL.You can make above call using java.net.URLConnection class.
Hope this will help.

Categories