Set-Up-
I have a JSP - index.jsp and a servlet indexController. indexController forwards request to index.jsp. Execution of index.jsp without execution of indexController is not desired.
Requirements-
'www.mysite.com' should be served by indexController/index.jsp
Any request with invalid url should get redirected to 'www.mysite.com'.
'www.mysite.com/index.jsp' should get redirected to 'www.mysite.com'.
My Solution -
Map indexController to "/" - I read that this is overriding 'default servlet' and I want to avoid that.
Map 'index.jsp' in welcome-file-list: I don't need to explicitly map it science 'index.jsp' is there, it will be automatically taken as welcome file. Doing this doesn't execute indexController. Moreover invalid urls are 'forwarded' not redirected to welcome file.
Map error page to home page- This is cheating with yourself. I don't want to hide errors.
Use two servlets. One with "/*" mapping, which redirects everything to 'mysite.com'. Second mapped to ""(empty) that actually serves request. - Some mysterious issues here(yet to figure out)
I think, everyone wants to achieve same functionality for their home screen and there are many questions at stackoverflow with similar problems.
Is there any standard solution which solves problem for once and for all.
Edit:- There were only 7 views in more than 30 mins. Removed GAE tag, to attract more views.
Simply put a web server before your app server and you can map any URL to any application/page. Also don't publish an app server directly.
Are you using Tomcat I presume, so you probably deployed ROOT application, right? (you should go with the web server anyway)
Related
I have been stuck with this problem for two days now and I cant find a solution.
I have this project structure as above:
I want to forward a request from Survey(it is a Servlet) to survey.jsp.
Now when I deploy this from IntelliJ on localhost the forward is made with success. Now i take the .war and use the Tomcat Apache manager to deploy it to a dedicated Server.
When i call dedicatedserverip:8080 the index.jsp loads properly as in localhost:8080. But when it comes to forward there are two cases:
When the name of .war file is different from survey.war when the forward happens I get error 404 . (In this case I think that the request is forwarded to dedicatedserverip:8080)
When the name of .war file is survey.war when forward happens it happens to load again index.jsp page. (in this case I think the request is forwarded to dedicatedserverip:8080/survey)
Below is the code I use to forward the request:
req.getRequestDispatcher("/survey.jsp").forward(req, resp);
Now my question is: Is there something done wrong? Or is there something that I must understand that I haven't? How can I fix it and get the needed result as in localhost?
Have you tried forwarding relative instead of absolute?
req.getRequestDispatcher("survey.jsp").forward(req, resp);
Then additionally it should be no problem if you change the name of your war file and with it the ServletContext of the application any more.
Finally, I solved the problem. It had nothing to do with the forward, the problem was in the submit form action. Action was action="/survey". I replaced with ${pageContext.request.contextPath}/survey . And it solved the problem. This explains why the index page was called when .war name was survey.warand error page when it was different.
Thank you for your support.
A somewhat unusual scenario perhaps, but we need to redirect in a Spring MVC controller from:
/js/hal-browser/browser.html
to:
/js/hal-browser/browser.html#/some_path/
All my attempted solutions to date have resulted in a redirect loop, as Spring performs the redirect but is then repeatedly matching /browser.html in the redirect URL, regardless of the additional info. What I need to say is 'match /browser.html ONLY if it's the end of the path'.
I have tried setUseSuffixPatternMatch(Boolean.FALSE); on the PathMatchConfigurer to no avail, also tried the following URI template regex pattern in the request mapping itself:
"/js/hal-browser/{file:browser\\.html$}"
..but still get a redirect loop. Ideas appreciated - this is Spring 4.1.6 in a SpringBoot 1.2.3 microservice, by way of context.
Update:
On further investigation and a better understanding of the URL fragment in use by the HAL browser to determine which path it will make a request to within the microservice itself, I believe the solution may lie not in trying to redirect off browser.html, as Spring will map this to the same controller method on every request regardless of the fragment value, but instead either reverting to the default context path for the application (/), which the HAL browser has set as its default entry point, or finding a way to configure the embedded tomcat container to respond with something sensible (not just a 404) on the default context path even though the app is mapped to /some_path.
As further context, we can redirect no problem at all from a convenience path of /browser (or whatever) into the HAL browser with the correct entry point fragment as the context path of the service - that works fine. The issue is the browser itself has a 'Go to entry point' button which, when pulling it in as a webjar, is hardcoded to /. The other alternative is to ditch the webjar and just copy in the static files for the browser and update the entry point.
The part after the # will never be sent to the server so in this case you'll always end-up with the same request within the loop.
What you can do is add a custom header in your controller, and make your controller intercept only the code where the header is not present by using the negation of the headers attribute, here's a pseudo code
#RequestMapping(value=[YOUR MAPPING], method=RequestMethod.GET, headers="!CustomHeader")
You could try not redirecting the user, but sending back the content you intended with some javascript to make the client browser to jump to the anchor on the after it loads, e.g. something like:
window.location.hash="/some_path/";
So I have let's say an application named MyApplication. I deploy it with a virtual server on glassfish and all is well since I set the default welcome page. Let's say the virtual server is to listen on mydomain.com
I goto mydomain.com and i see my index file of my application just fine. Then i go to do a j_security_check login.
And i am then redirected to: http://mydomain.com/MyApplication/page.xhtml
How can I get this to wehre it is: http://mydomain.com/page.xhtml ?
Now if I do take MyApplication out of the URL and try to manually goto that page it is blank, as I believe it is not being processed by JSF.
What I have tried.
* Setting the default glassfish application to my application and setting the context path of my application to / (glassfish complains and i cannot deploy my application)
* Doing the same as above without setting my context path to / and leaving it as is.
Will deploy but same issue.
My main reason for wanting this, is it seems if someone does get redirected to a path without the application name, the session state appears to be different. And causes some sporadic issues with session collision and values not being passed properly. So I either want to force the URLS to use the ApplicationName all the time, OR force them to not use it for the sake of consistancy.
There should be a way to accomplish this since I dont believe we should always have to have the ApplicationName in the URL.
Please help if you can, what I have found by searching seems to take care of it for the initial request but not when doing redirects using the FacesContext extenralContext redirect.
Unless I am not redirecting properly. I am at a loss here.
Thank you for the help.
IMHO you should consider using asap PrettyFaces
Your application URLs will always be elegantly displayed to your users.
I have a project that I can see the JSP's... However Jboss shows me the servlets so in the address bar all i can see is:
"appservleer?=AppDev"
This is an example not the actual address...but with that info only how can I know which JSP is being displayed at that moment by JBoss.
or am I understanding this Jboss, servlet, JSP incorrectly?
Some guidance would be appreciated.
Thanks.
You cannot know which jsp is being displayed unless the developer decided to give hints about it.
A jsp is just a view technology that the servlet uses to render a response. That is, typically, the jsp will be parsed and html will be generated. However, the developer of the servlet may decide to write to the response himself. So nothing can really tell you if what you got came from a jsp or from another source, even if you had a url like www.mydomain.com/page.jsp. Nothing guarantees the response you are seeing in your browser was generated from a jsp.
The only way to know for sure which JSP is used for rendering is to look at the source code of the servlet that is mapped under (in your example) "appservleer".
Look in web.xml which servlet is mapped with this path, and open the source code of this servlet. Then figure out the execution path, starting with the doGet(...) or the doPost(...) method, and see to which JSP it is forwarded in the end...
Good luck.
You should always start with the web.xml file which will eventually lead you to the actual jsp file that is displayed. Start with the servlet mapping and check which servlet is invoked for the url. Then dig into the servlet to find out which jsp it is redirected to. If you are using any application framework then look for the implementation to findout the mapping. For e.g. in spring the spring configuration files or the controller classes tells you where the calls are redirected to.
I am new to J2EE and related stuff in general. I am trying to move a particular web application from a Sun One server deployment on to JBoss. The application is filled with a lot of servlets where each one re-directs to another.
There are way too many servlets for me to enter a mapping between each of these servlet class to a URL-mapping individually in web.xml. The application code has many redirects where they name the servlet class names itself in the redirect-URL. As of now when I run it on JBoss the redirections to URL with servlet classnames in URL don't seem to work on JBoss (it gives a 404: Not Found, probably since there is no mapping in the web.xml). So is there any config setting that I can set to allow this to happen or should manually enter each and every URLpattern-to-Servlet mapping in the web.xml?
Thank you.
There are two solutions.
As we know JBoss uses Tomcat under the hood as a servlet container. You can enable invoker servlet, that would save you from mapping the whole lot in web.xml. But beware, it will be naive to do that, and not at all encouraged.
Secondly, you can write another servlet/filter and map just that in your web.xml for every url pattern, may be. Then that new servlet of your can forward the requests to whatever servlet it should.
I hope you get my point.
Not sure what you mean by this
the application code has many redirects where they name the servlet class names itself in the redirect-URL
Do you have hardcoded urls in the servlet classes? How many servlets? If you have hardcoded urls they may all have broken because the context is slightly different, or the appname, etc.etc. Can you post an example?
Well, there are some hard-coded URLs in the code, but even if i typed in the right URLs in browser directly I still get a 404. There are about 30 servlets (a conservative approximation). Ex: http://FQDN_SERVER.com/?arg1=ABCD&arg2=XYZ
Here the servlet-classname is literally the classname of the servlet without ".class" extension, which may not be a good practice. But the code is full of such redirections and if I have to change this then I have to add a new url-pattern to each of these servlets in the web.xml and construct a new red-rect URL for each of these servlets. So is there anyway I can avoid this or will I have to go through the pain of doing the above mentioned?
Thanks,
Manoj
Sorry the URL-pattern looks this http://FQDN_SERVER.com/servlet-classname?arg1=ABCD&arg2=XYZ