Faces Servlet Application URL JSF/Glassfish - java

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.

Related

How to Configure Subdomains to Work For Java Servlets?

Not managed to find an answer to this anywhere yet, I'm getting close to having this implemented, just one final hurdle that I've not managed to crack.
Single server. Split up using WHM into individual cPanel accounts. 1 instance of Tomcat 7 running. Servlet class files and JSP files deployed.
www.first-website.com - All works perfectly.
www.second-website.com - www. points to another server completely (non-Apache Tomcat / Java), so please ignore that part
sub-domain.second-website.com - Manages to load main Welcome File correctly which is being processed as a Servlet. I'm getting a 404 error though when I try and access sub-domain.second-website.com/about-us/ for example, for all of the sub-directory pages (i.e. url-patterns from web.xml) - this all works on my local machine, so this is something that I've missed configuring the server. The 404 page that it is showing is a server level 404 page, not an Apache Tomcat 404 page which leads me to believe this is an issue somehow related to all of this, http://tomcat.apache.org/tomcat-7.0-doc/virtual-hosting-howto.html - which I'm struggling to understand what this page is actually talking about.
Pointers?
Short answer to the specific problem above, add the following to your .htaccess file within your document root for the sub-domain;
RewriteEngine on
SetHandler jakarta-servlet
SetEnv JK_WORKER_NAME ajp13
Long answer, this is how to do it all, https://www.contradodigital.com/2017/05/24/how-to-install-servlet-support-on-a-subdomain-on-apache-tomcat-7-using-command-line-cpanel-and-whm/

In Spring MVC, does using 'redirect:process' or 'redirect:/process' differ regarding causing a lost session or session value?

I am experiencing strange behavior in my Spring MVC 3.2 application, and I noticed that this only happening when the redirect is done in an alternate way; so my questions are:
Is doing 'redirect:/process' any different from
'redirect:process' for redirecting to an internal controller ?
Does the added slash make any difference, such as affecting session handling ?
What are the reasons for a lost session (or lost session attributes) ?
There's a value I read through my application; even thou I do redirects in many cases, when I add a slash before the Controller URI, on production I am sometimes losing this value.
Any clue on how to troubleshoot the lost session value ?
Note: I am using methods httpRequest.getSession().setAttribute and httpSession.getAttribute for accessing the session.
with '/' you are declaring a path from root, which is your servlet context path. without '/', usually it's going to a path relative to your current sub path. for example, if you are at '/go/url', your are pointing to '/go/url/next', not '/next'.
I didn't check spring source code but that's how it works in web browser usually.
EDIT:
I'm sorry, in Spring MVC, you must always provide full path, not just relative path. So you should do "redirect:/full/path".
Just a note: / is a slash, backslash is \.
Except for external causes because of other errors, neither redirect:process not redirect:/process should change anything as long as the session is concerned.
But those 2 redirects are not supposed to do the same thing, unless you are on the root application page. Assuming that your application runs on HTTP port 80 on server.domain, with a servlet context of myapp, and you are processing a request to http://server.domain/myapp/local/part, redirect:process would ask browser to request http://server.domain/myapp/local/part/process, when redirect:/process would ask browser to request http://server.domain/myapp/process.
What happens next depends on your controller mappings.
You probably want to analyse, what is causing the lost session attributes. One approach would be to implement your own HttpSessionAttributeListener and log in public void attributeRemoved(HttpSessionBindingEvent event) implementation. Also keep spring log level to debug.

Still have Eclipse EE 404 on Mac after following advice on this issue

First of all, I am aware that there are other questions regarding Tomcat, Eclipse, and the infamous 404 error. However, none of them manages to resolve the issue. I have spent well over 24 hours on this issue.
To save time, when I configured everything (including Tomcat, and creating a server in Eclipse), I:
• Changed the server location from "workspace metadata" to its correct location by using the "Switch Location" button located in the server's Properties window.
• I chose the "Use Tomcat Installation" option in Server Locations, and saved the choice I made.
In both cases, I restarted the server. If you're curious as to what app I'm currently working on, it's a simple Hello World app, found at: http://theopentutorials.com/examples/java-ee/servlet/how-to-create-a-servlet-with-eclipse-and-tomcat/
• I have included the Java file in the "welcome file" list inside web.xml.
Lastly, out of curiosity, why does the Eclipse browser only go to the project directory, and not the servlet itself? (If I add on the servlet name, then "Hello World" appears).
• Yes, if I enter "http://localhost:8080", the default Tomcat page appears, so no issues there.
Can anyone clue me in, as to why I am still getting 404s after all this, and following advice that has been marked as "Accepted" here at SO, such as the following:
HTTP Status 404 - The requested resource (/) is not available
Thanks in advance for any help, it is greatly appreciated.
The error 404 may occur because of large amount of different reasons. In order to resolve that, you should check your tomcat log file out first. It contains by the path:
%PATH_TO_WORKSPACE%\.metadata\.plugins\org.eclipse.wst.server.core\tmp%SERVER_NUMBER%\
logs
Usually it contains some stacktraces which discribes the problem. If not, then you should check your deployed application out there:
%PATH_TO_WORKSPACE%\.metadata\.plugins\org.eclipse.wst.server.core\
tmp%SERVER_NUMBER%\___YOUR_APP____
It might happen that your application was not deployed correctly by eclipse plugin (happens very often) and you should try this:
Project --> clean
'Right click on your server' --> clean
Or just remove your webapp from the directory I mentioned erlier and redeploy it from scratch.
There is something basic you need to understand regarding using tomcat(or application server for that matter). There is a slight difference between using from Eclipse and using from outside
Using From within Eclipse
What happens here is that Eclipse (by default) uses a copy of your tomcat installation and places it in its metadata workplace. This secondary tomcat is used by Eclipse for all deployments, re-deployments and all. Keep in mind that this is not your original copy of tomcat installation.
The difference in this tomcat installation is that is actually a minimal server, meaning that although it has all the deployment capabilities, it does not have some of the extra features that come with the tomcat installation and one main feature is the tomcat's homepage (the only reason why people out there get the infamous 404 resource not found when they try to run-on-server their application).
Workaround
Although not an issue (nor a bug from the Apache's end), you can still view your application by changing the URL to your application's url, homepage or no homepage ! All you have to do is change the url from http://localhost:8080 to http://localhost:8080/yourApplicationName and voila , the default page of your application will be shown that you mentioned in the welcome-page-list. Keep note that if you didn't specify a default page in your web.xml, you will again wind up with, yet again, the dreaded 404 resource not found page. The reason is that Tomcat has found your application, but it doesn't know what to do at the root context of your application. You can either map your servlet to the root of the application (that way it will always run at http://localhost:8080/yourApplicationName) or you can change the URL to the url-pattern that you mapped with the servlet in the web.xml, it must be something like http://localhost:8080/myApplicationName/myServletMappingPattern

How to Map home page to "root of application" in Java EE?

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)

Spring MVC application context path

My Spring MVC application is runnning on a Tomcat behind an Apache 2 acting as a proxy. I access my app directly in tomcat via an url like http://localhost:8080/myapp. I access my app via proxy with an url like http://localhost/tomcat/myapp.
The second url makes my app behave incorrectly, because it supposes it lives in /myapp context path, but via the proxy it should live in /tomcat/myapp.
Is there a variable in Spring or Servlet API, that holds /tomcat/myapp if I am accessing it via the proxy, and /myapp if I am accessing it directly?
Thanx
I think you need to enable proxy support then. This link might help you or give a little hint in this regards.
http://tomcat.apache.org/tomcat-6.0-doc/proxy-howto.html
Just stumbled upon this post while searching for the config setting for tomcat.
There is a much easier way to configure tomcat to handle the exact situation you are experiencing. See:
http://tomcat.apache.org/tomcat-5.5-doc/proxy-howto.html
Simple configure a connector for the proxy in tomcat, and the servlet/struts context path issues will resolve.
-edit: Obviously I didn't read #2 comment...
I mean when I redirect to "/index.jsp"
it actually redirects to
"http://localhost/myapp/index.jsp"
instead of
"http://localhost/tomcat/myapp/index.jsp"
Redirect to index.jsp instead of /index.jsp
When you redirect to /index.jsp this acts as an absolute url and it gets redirected to myapp/index.jsp. index.jsp is a relative url and will redirect to tomcat/myapp/index.jsp

Categories

Resources