2

Right now, every time I type mydomain.com it would automatically go to mydomain.com/projectname/home.jsf, which is my login page. Not sure why, I know I still in session meaning that I can navigate to a restricted page without login in again. So how do I make so that if I type mydomain.com, I go to mydomain.com/projectname/CentralFeed.jsf instead of login page if the user still in session. Here is my rough design

In my web.xml

<welcome-file-list>
    <welcome-file>CentralFeed.jsf</welcome-file>
</welcome-file-list>          
<login-config>
    <auth-method>FORM</auth-method>
    <realm-name>jdbc-realm-scholar</realm-name>
    <form-login-config>
        <form-login-page>/home.jsf</form-login-page>
        <form-error-page>/LoginError.jsf</form-error-page>
    </form-login-config>
</login-config>
 <filter>
    <filter-name>MyFilter</filter-name>
    <filter-class>com.scholar.servlet.MyFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>MyFilter</filter-name>
    <url-pattern>/CentralFeed.jsf</url-pattern>        
    <url-pattern>/TextBook.jsf</url-pattern>
    ...
</filter-mapping> 

I am not sure if you guys need to know this, but I also have a Filter call MyFilter, which map to restricted page, and check if the user still in session, if so then just chain.doFilter(req, res);, if not then redirect to login page home.jsf

Thang Pham
  • 38,125
  • 75
  • 201
  • 285

2 Answers2

4

put index.jsp

and in it put

<% response.sendRedirect("desired URL"); %>

remove

<welcome-file-list>
    <welcome-file>CentralFeed.jsf</welcome-file>
</welcome-file-list>    

from web.xml

jmj
  • 237,923
  • 42
  • 401
  • 438
  • yes. by `response.sendRedirect()` you will just make client to `GET` desired `URL` which you want to redirect from root – jmj Mar 17 '11 at 17:58
  • I hope you dont mind if I ask this, so CentralFeed.jsf is the page I want to always go first. If I change that page to index.jsf, will I get the same result, like what you suggest above? – Thang Pham Mar 17 '11 at 18:04
  • Dont think it work: Here is my Exception: `javax.servlet.ServletException: Error Parsing /index.xhtml: Error Traced[line: 12] The content of elements must consist of well-formed character data or markup.` I am using JSF Facelets btw – Thang Pham Mar 17 '11 at 18:13
  • nvm, it has to be a .jsp file instead of .xhtml – Thang Pham Mar 17 '11 at 18:17
4

The <welcome-file> has to point to a physical file on the disk, not to some servlet mapping. Since you've a CentralFeed.xhtml file, a <welcome-file> of CentralFeed.jsf isn't going to work.

There are two solutions (apart from the scriptlet hack as suggested by Jigar):

  1. Create an empty file CentralFeed.jsf file next to the CentralFeed.xhtml. This fools the server that the file is physically present.

  2. Map the FacesServlet on *.xhtml instead of *.jsf. While this was impossible in JSF 1.x because it would run in an infinite loop, this works fine on JSF 2.0. Your question history confirms that you're using JSF 2.0. This way you can just set the <welcome-file> to CentralFeed.xhtml.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555