I have a web application -
But when I navigate from menus and other links, the address bar displays folder and file name.
What I would like to have is whatever navigation the user do, the address bare should only display
http://domain:port/daswebapp
rather than
http://domain:port/daswebapp/admin/index.jsp
Can anybody help on this. I don't use any other framework.Its a pure MVC pattern.
Thanks n Regards
Noufal
Use url rewrite filter such as urlrewritefilter, for JSF based app go for Pretty faces
The main things it is used for are:
URL Tidyness / URL Abstraction - keep URLs tidy irrespective of the underlying technology or framework (JSP, Servlet, Struts etc).
Browser Detection - Allows you to rewrite URLs based on request HTTP headers (such as user-agent or charset).
Date based rewriting - Allows you to forward or redirect to other URL's based on the
date/time (good for planned outages).
Moved content - enable a graceful move of content or even a change in CMS.
Tiny/Friendly URL's (i.e. blah.com/latest can be redirected to blah.com/download/ver1.2.46.2/setup.exe)
A Servlet mapping engine (see Method Invocation)
you can use RequestDispatcher,
RequestDispatcher dispatcher = getRequestDispatcher("daswebapp/admin/index.jsp");
dispatcher.forward( request, response );
you can use something like this try out and let me know
RequestDispatcher reqDisp = getServletContext().getRequestDispatcher("/index.jsp");
reqDisp.forward(request, response);
Related
I have the following problem. I want to use a controller, same for every page in my application. It is a common form included via include jsp in every page and it sends a mail to a predefined email account. The problem is, when I am posting the form, I get redirected to a blank page, with the url being my RequestMapping value despite the method called is actually void. So I need now to redirect me, after sending the mail to the page where I came from. How do I get access to the url link of the page that redirected me, into sending the email? Thanks
When returning void and one isn't handling writing the response yourself Spring MVC delegates detection of which view to render to a RequestToViewNameTranslator for which there is a single implementation the DefaultRequestToViewNameTranslator. Which basically takes the incoming URL, strips any suffixes (like .html etc.) and uses that as the viewname. The behavior you see now.
I guess you have 3 possible solutions
Add a hidden form attribute which contains the current page
Use the referer request header to determine where the request came from (not a 100% solution, See Alternative to "Referer" Header)
Submit the form through ajax so that you stay on the current page
For option 2 add the HttpServletRequest as a parameter to your request handling method and retrieve the header.
public String foo(HttpServletRequest request) {
String referer = request.getHeader("Referer");
return "redirect:" + referer;
}
I would probably go for option 3 and maybe add option 1 as a fallback when no javascript is available.
I have a problem with redirection - it simply does not work for valid paths.
Right now I use page forwarding in the Servlet, but I need redirection in a filter.
All the pages reside in the 'pages' folder and have a .jspx extension
I've tried the following (this path works with forwarding):
httpResponse.sendRedirect("/pages/login.jspx");
browser url is http://[localhost]/pages/login.jspx, and it shows Tomcat's 404 page, the context path (in my case it's '/hotel') is missing from the url, so, if I add it:
httpResponse.sendRedirect("/hotel/pages/login.jspx");
redirect does not happen, browser url does not change, and I'm shown the browser's 404 page (This program cannot display the webpage).
What am I doing wrong?
The filter which is used to test this has the following mapping:
#WebFilter(filterName = "SecurityFilter", urlPatterns = "/*")
The redirected URL is indeed relative to the initially requested URL. To dynamically prepend the context path it's recommended to use HttpServletRequest#getContextPath() instead of hardcoding it, because the context path value can be changed externally by server-specific configuration.
As to your concrete problem, I'm not sure if I understand "browser's 404 page" properly, perhaps you mean the browser-default error page which can occur when the server is unreachable or when the request has been redirected in an infinite loop (that should however have been made clear in the actual message of the browser default error page, at least Chrome and Firefox do that).
Given that your filter is mapped on /*, it's actually redirecting in an infinite loop because the request URL of the login page in turn also matches the URL pattern of the filter.
You'd need either to put the filter on a more specific URL pattern which does not cover the login page, e.g. on /secured/* where all restricted pages are been moved in there (or map it on /pages/* and put the login page outside there), or to fix your filter as follows:
String loginURL = request.getContextPath() + "/pages/login.jspx";
if (needsToRedirect && !request.getRequestURI().equals(loginURL)) {
response.sendRedirect(loginURL);
}
else {
chain.doFilter(request, response);
}
1 - Have you got logging or some other observable event in your servlet code that confirms it's definitely running?
2 - Redirects can fail if you write any actual response content prior to the redirect - have you anything doing that?
3 - Another option, set up a page in the root directory, even a "hello.html" static page, and see if you can redirect to that using either of "/hello.html" and "hello.html".
Just some ideas I would use in my own debug approach, hope something helps!
Hey,
Maybe the title is not the best choice, but I really don't know how to better describe the problem.
The thing is when you point your browser to url that contains #
http://anydomain.com/test/elsem/1234#dogeatdog
and for some reason (ie. there is a business logic) you want to redirect to other page
http://anydomain.com/test/els/1234
the #dogeatdog will be added to new url.
I found this behavior while developing wicket app, but just now I tested it with simple pure java servlet. Can someone explain it to me?
Here is the code just in case I'm doing something wrong:
private void process(HttpServletRequest req, HttpServletResponse res)
{
res.setContentType("text/plain");
try
{
HttpSession session = req.getSession();
Object as = session.getAttribute("as");
if (as == null)
{
log.info("redirecting");
session.setAttribute("as", 1);
res.sendRedirect("/test/");
}
else
{
log.info("writing");
PrintWriter out = res.getWriter();
out.write("after redirect "+as);
out.flush();
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
Hash fragments (#a_hash_fragment) never leave the browser, they are not part of HTTP request.
What the web server gets in this case is GET /test/elsem/1234, and it responds with redirect 3xx code and the new url /test/els/1234, which your browser picks and appends #dogeatdog. Makes sense now?
UPDATE: Thanks to Zack, here's a W3C document that exactly explains how this (should) work:
http://www.w3.org/Protocols/HTTP/Fragment/draft-bos-http-redirect-00.txt
From the sendRedirect Javadoc:
Sends a temporary redirect response to the client using the specified
redirect location URL. This method can accept relative URLs; the
servlet container must convert the relative URL to an absolute URL
before sending the response to the client. If the location is relative
without a leading '/' the container interprets it as relative to the
current request URI. If the location is relative with a leading '/'
the container interprets it as relative to the servlet container root.
Because of repetitive use of "relative" in the Javadoc, I suspect the new URL is using what it can from the old URL and then building from there...
In the brief amount of what I've read, forwarding should be used if possible instead of redirect.
See this for a good explanation of forward verses redirect.
See this for straight-forward examples of forwarding requests to Servlets or JSPs.
Of course, with forwarding, the original URL will remain intact so that may not be what you're looking for...
EDIT
With information from milan, I found some more information regarding URL fragments (the stuff after "#" - I didn't know that was their official name until corresponding with milan).
There's another SOF post that has some good information concerning this and possibly the best answer: URL Fragment and 302 redirects
I have "+1'd" milan for giving good direction on this...
Is it possible to get the user's browser id number using JSF? I use JBoss 7 for application server.
The browser's user agent string is available as HTTP request header with the name User-Agent. The request headers are in JSF available by ExternalContext#getRequestHeaderMap():
ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
String userAgent = externalContext.getRequestHeaderMap().get("User-Agent");
No need to haul the raw Servlet API from under the JSF hoods. Always look at the javadoc of ExternalContext first whenever you need to have access to the HTTP servlet request or response.
Keep in mind that request headers (as everything else in a HTTP request) are fully controllable by the enduser. So never assume the information to be correct and valid. Use it for statistics only. If you need to do feature detection, strongly prefer client side languages like JavaScript and/or CSS if possible. They can do that much more reliably.
You can read user-agent header from request to get the detail about the browser
((HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest()).getHeaders();
HI,
This question is from previously answered question by BalusC. The excerpt of the answer as follows:
This happens only if under the covers
a forward by
RequestDispatcher#forward() takes
place. In a forward, the
servletcontainer basically reuses the
same HTTP request/response for a view
(JSP/XHTML page). It does not
force/instruct the webbrowser to send
a brand new request.
What this means is every new view is rendered using the forward. The following are my questions:
If the above is the case, then all the views displayed with the same request?. Because, we are always seeing the same URL in the address bar.
Is it that the values in the previous request is retained for the new request?
In this case, if every request is same then is it like storing in the session, for long time. I am bit confused on the view handling by JSF. Want to understand more internal work flow of JSF.
When we use the <redirect/> in faces-config.xml, will the URL in address bar get changed?
If the above is the case, then all the views displayed with the same request?. Because, we are always seeing the same URL in the address bar.
If it concerns a HTTP POST request and the JSF bean action navigates to a different view, then per saldo you will indeed have two different views in the same request. One for the initial view which is been used to gather/convert/validate the necessary request parameters and update the model values and other for the result view which is been used to show some result.
Is it that the values in the previous request is retained for the new request?
In a forward there's no means of a new request. It's the same request.
In this case, if every request is same then is it like storing in the session, for long time. I am bit confused on the view handling by JSF. Want to understand more internal work flow of JSF.
This is definitely not the case. As to your confusion, it may help to put JSF aside for a while and go playing with plain vanilla JSP/Servlet (which is what JSF is using under the covers). I think the following links may help in getting new insights about how basic JSP/Servlet works and how the average MVC framework on top of JSP/Servlet works:
Servlets tag info page
Design patterns Java web applications
When we use the <redirect/> in faces-config.xml, will the URL in address bar get changed?
Yes. A HTTP redirect sends a HTTP location: newpage.jsf header which in turn instructs the webbrowser to fire a new HTTP GET request on the given location. This get reflected in the browser address bar. You may want to install a HTTP debugging tool like Firebug or Fiddler2 to track HTTP traffic. You'll see that a forward happens inside the same request and a redirect comes with a new request.
If the above is the case, then all the views displayed with the same request?. Because, we are always seeing the same URL in the address bar.
if the url is the same in the web-browser then there can be two cases. either the same request is being forwarded as he mentioned OR new GET request is issued with same URL [which is lesser the case]
Is it that the values in the previous request is retained for the new request?
request life cycle will be from request to response. so after response all the managed bean with request scoped will get destroyed.
When we use the in faces-config.xml, will the URL in address bar get changed?
Yes it will instruct browser to issue new GET request for new URL.