<p>I have a struts-based webapp, and I would like the default "welcome" page to be an action. The only solutions I have found to this seem to be variations on making the welcome page a JSP that contains a redirect to the action. For example, in <code>web.xml</code>:</p>

<pre><code>&lt;welcome-file-list&gt;
    &lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt;
&lt;/welcome-file-list&gt;
</code></pre>

<p>and in <code>index.jsp</code>:</p>

<pre><code>&lt;% 
  response.sendRedirect("/myproject/MyAction.action");
%&gt; 
</code></pre>

<p>Surely there's a better way!</p>

[{'answer_id': 39599, 'author': 'bpapa', 'author_id': 543, 'author_profile': 'https://Stackoverflow.com/users/543', 'pm_score': 1, 'selected': False, 'text': '<p>It appears that a popular solution will not work in all containers... <a href="http://www.theserverside.com/discussions/thread.tss?thread_id=30190" rel="nofollow noreferrer">http://www.theserverside.com/discussions/thread.tss?thread_id=30190</a></p>\n'}, {'answer_id': 40745, 'author': 'Georgy Bolyuba', 'author_id': 4052, 'author_profile': 'https://Stackoverflow.com/users/4052', 'pm_score': 1, 'selected': False, 'text': '<p>I would create a filter and bounce all requests to root back with forward responce. Hacks with creating home.do page looks ugly to me (One more thing to remember for you and investigate for someone who will support your code). </p>\n'}, {'answer_id': 42614, 'author': 'Damien B', 'author_id': 3069, 'author_profile': 'https://Stackoverflow.com/users/3069', 'pm_score': 3, 'selected': False, 'text': '<p>"Surely there\'s a better way!"</p>\n\n<p>There isn\'t. Servlet specifications (Java Servlet Specification 2.4, "SRV.9.10 Welcome Files" for instance) state:</p>\n\n<blockquote>\n  <p>The purpose of this mechanism is to allow the deployer to specify an ordered\n  list of partial URIs for the container to use for appending to URIs when there is a\n  request for a URI that corresponds to a directory entry in the WAR not mapped to\n  a Web component.</p>\n</blockquote>\n\n<p>You can\'t map Struts on \'/\', because Struts kind of require to work with a file extension. So you\'re left to use an implicitely mapped component, such as a JSP or a static file. All the other solutions are just hacks. So keep your solution, it\'s perfectly readable and maintainable, don\'t bother looking further.</p>\n'}, {'answer_id': 43879, 'author': 'Martin McNulty', 'author_id': 4507, 'author_profile': 'https://Stackoverflow.com/users/4507', 'pm_score': 5, 'selected': False, 'text': '<p>Personally, I\'d keep the same setup you have now, but change the redirect for a forward.  That avoids sending a header back to the client and having them make another request.</p>\n\n<p>So, in particular, I\'d replace the </p>\n\n<pre><code>&lt;% \n  response.sendRedirect("/myproject/MyAction.action");\n%&gt;\n</code></pre>\n\n<p>in index.jsp with</p>\n\n<pre><code>&lt;jsp:forward page="/MyAction.action" /&gt;\n</code></pre>\n\n<p>The other effect of this change is that the user won\'t see the URL in the address bar change from "<a href="http://server/myproject" rel="noreferrer">http://server/myproject</a>" to "<a href="http://server/myproject/index.jsp" rel="noreferrer">http://server/myproject/index.jsp</a>", as the forward happens internally on the server.</p>\n'}, {'answer_id': 318846, 'author': 'Craig Wohlfeil', 'author_id': 22767, 'author_profile': 'https://Stackoverflow.com/users/22767', 'pm_score': 4, 'selected': False, 'text': '<p>As of the 2.4 version of the Servlet specification you are allowed to have a servlet in the welcome file list. Note that this may not be a URL (such as /myproject/MyAction.action). It must be a named servlet and you cannot pass a query string to the servlet. Your controller servlet would need to have a default action.</p>\n\n<pre><code>&lt;servlet&gt;\n  &lt;servlet-name&gt;MyController&lt;/servlet-name&gt;\n  &lt;servlet-class&gt;com.example.MyControllerServlet&lt;/servlet-class&gt;\n&lt;/servlet&gt;\n&lt;servlet-mapping&gt;\n  &lt;servlet-name&gt;MyController&lt;/servlet-name&gt;\n  &lt;url-pattern&gt;*.action&lt;/url-pattern&gt;\n&lt;/servlet-mapping&gt;\n&lt;welcome-file-list&gt;\n  &lt;welcome-file&gt;MyController&lt;/welcome-file&gt;\n&lt;/welcome-file-list&gt;\n</code></pre>\n'}, {'answer_id': 1775500, 'author': 'Nischal', 'author_id': 69542, 'author_profile': 'https://Stackoverflow.com/users/69542', 'pm_score': 3, 'selected': False, 'text': '<p>Something that I do is to put an empty file of the same name as your struts action and trick the container to call the struts action.</p>\n\n<p>Ex. If your struts action is welcome.do, create an empty file named welcome.do. That should trick the container to call the Struts action.</p>\n'}, {'answer_id': 4842983, 'author': 'Srikanth', 'author_id': 483628, 'author_profile': 'https://Stackoverflow.com/users/483628', 'pm_score': 4, 'selected': False, 'text': '<p>This is a pretty old thread but the topic discussed, i think, is still relevant. I use a struts tag - s:action to achieve this. I created an index.jsp in which i wrote this...</p>\n\n<pre><code>&lt;s:action name="loadHomePage" namespace="/load" executeResult="true" /&gt;\n</code></pre>\n'}, {'answer_id': 14811399, 'author': 'Navathej', 'author_id': 2061197, 'author_profile': 'https://Stackoverflow.com/users/2061197', 'pm_score': -1, 'selected': False, 'text': '<p>This works as well reducing the need of a new servlet or jsp</p>\n\n<pre><code>&lt;welcome-file-list&gt;\n&lt;welcome-file&gt;/MyAction.action&lt;/welcome-file&gt;\n&lt;/welcome-file-list&gt;\n</code></pre>\n'}, {'answer_id': 15551450, 'author': 'gavenkoa', 'author_id': 173149, 'author_profile': 'https://Stackoverflow.com/users/173149', 'pm_score': 1, 'selected': False, 'text': '<p>Here two blogs with same technique:</p>\n\n<ul>\n<li><a href="http://technologicaloddity.com/2010/03/25/spring-welcome-file-without-redirect/" rel="nofollow">http://technologicaloddity.com/2010/03/25/spring-welcome-file-without-redirect/</a></li>\n<li><a href="http://wiki.metawerx.net/wiki/HowToUseAServletAsYourMainWebPage" rel="nofollow">http://wiki.metawerx.net/wiki/HowToUseAServletAsYourMainWebPage</a></li>\n</ul>\n\n<p>It require Servlet API >= v2.4:</p>\n\n<pre><code>&lt;servlet-mapping&gt;\n    &lt;servlet-name&gt;dispatcher&lt;/servlet-name&gt;\n    &lt;url-pattern&gt;/&lt;/url-pattern&gt;\n    &lt;url-pattern&gt;/index.htm&lt;/url-pattern&gt;    &lt;&lt;==  *1*\n&lt;/servlet-mapping&gt;\n&lt;welcome-file-list&gt;\n    &lt;welcome-file&gt;index.htm&lt;/welcome-file&gt;   &lt;&lt;== *2*\n&lt;/welcome-file-list&gt;\n</code></pre>\n\n<p>so you no longer need <code>redirect.jsp</code> in non-<code>WEB-INF</code> directory!!</p>\n'}, {'answer_id': 15905476, 'author': 'John Solomon', 'author_id': 932723, 'author_profile': 'https://Stackoverflow.com/users/932723', 'pm_score': 0, 'selected': False, 'text': '<p>Just add a filter above Strut\'s filter in <code>web.xml</code> like this:</p>\n\n<pre><code>&lt;filter&gt;\n    &lt;filter-name&gt;customfilter&lt;/filter-name&gt;\n    &lt;filter-class&gt;com.example.CustomFilter&lt;/filter-class&gt;\n&lt;/filter&gt;\n&lt;filter-mapping&gt;\n    &lt;filter-name&gt;customfilter&lt;/filter-name&gt;\n    &lt;url-pattern&gt;/*&lt;/url-pattern&gt;\n&lt;/filter-mapping&gt;\n</code></pre>\n\n<p>And add the following code in doFilter method of that CustomFilter class</p>\n\n<pre><code>public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,\n        FilterChain filterChain) throws IOException, ServletException {\n    HttpServletRequest httpRequest = (HttpServletRequest)servletRequest;\n    HttpServletResponse httpResponse = (HttpServletResponse)servletResponse;\n    if (! httpResponse.isCommitted()) {\n        if ((httpRequest.getContextPath() + "/").equals(httpRequest.getRequestURI())) {\n            httpResponse.sendRedirect(httpRequest.getContextPath() + "/MyAction");\n        }\n        else {\n            filterChain.doFilter(servletRequest, servletResponse);\n        }\n    }\n}\n</code></pre>\n\n<p>So that Filter will redirect to the action. You dont need any JSP to be placed outside WEB-INF as well. </p>\n'}, {'answer_id': 16113198, 'author': 'Lund Wolfe', 'author_id': 1247753, 'author_profile': 'https://Stackoverflow.com/users/1247753', 'pm_score': -1, 'selected': False, 'text': "<p>This worked fine for me, too:</p>\n\n<pre><code>&lt;welcome-file-list&gt;\n&lt;welcome-file&gt;MyAction.action&lt;/welcome-file&gt;\n&lt;/welcome-file-list&gt;\n</code></pre>\n\n<p>I was not able to get the default action to execute when the user enters the webapp using the root of the web app (mywebapp/).  There is a bug in struts 2.3.12 that won't go to the default action or use the welcome page when you use the root url.  This will be a common occurrence.  Once I changed back to struts 2.1.8 it worked fine.</p>\n"}, {'answer_id': 16638674, 'author': 'siva', 'author_id': 1830486, 'author_profile': 'https://Stackoverflow.com/users/1830486', 'pm_score': 0, 'selected': False, 'text': '<p>I have configured like following. it worked perfect and no URL change also...</p>\n\n<p>Create a dummy action like following in struts2.xml file. so whenever we access application like <code>http://localhost:8080/myapp</code>, it will forward that to dummy action and then it redirects to index.jsp / index.tiles...</p>\n\n<pre><code>&lt;action name=""&gt;\n    &lt;result type="tiles"&gt;/index.tiles&lt;/result&gt;\n&lt;/action&gt;\n</code></pre>\n\n<p>w/o tiles</p>\n\n<pre><code>&lt;action name=""&gt;\n    &lt;result&gt;/index.jsp&lt;/result&gt;\n&lt;/action&gt;\n</code></pre>\n\n<p>may be we configure some action index.action in web.xml as <code>&lt;welcome-file&gt;index.action&lt;/welcome-file&gt;</code>, and use that action to forward required page...</p>\n'}, {'answer_id': 18162786, 'author': 'msangel', 'author_id': 449553, 'author_profile': 'https://Stackoverflow.com/users/449553', 'pm_score': 1, 'selected': False, 'text': '<p><a href="https://stackoverflow.com/a/43879/449553">there are this answer above</a> but it is not clear about web app context\nso\ni do this:</p>\n\n<pre><code>&lt;welcome-file-list&gt;\n    &lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt;\n&lt;/welcome-file-list&gt;\n&lt;servlet&gt;\n    &lt;servlet-name&gt;TilesDispatchServlet&lt;/servlet-name&gt;\n    &lt;servlet-class&gt;org.apache.tiles.web.util.TilesDispatchServlet&lt;/servlet-class&gt;\n&lt;/servlet&gt;\n&lt;servlet-mapping&gt;\n    &lt;servlet-name&gt;TilesDispatchServlet&lt;/servlet-name&gt;\n    &lt;url-pattern&gt;*.tiles&lt;/url-pattern&gt;\n&lt;/servlet-mapping&gt;\n</code></pre>\n\n<p>And in <code>index.jsp</code> i just write:</p>\n\n<pre><code>&lt;jsp:forward page="index.tiles" /&gt;\n</code></pre>\n\n<p>And i have index definition, named <code>index</code> and it all togather work fine and not depends on webapp context path.</p>\n'}, {'answer_id': 33825842, 'author': 'WesternGun', 'author_id': 4537090, 'author_profile': 'https://Stackoverflow.com/users/4537090', 'pm_score': 0, 'selected': False, 'text': '<p>I am almost sure that the OP is the best solution(not sure about best practice, but it works perfectly, and actually is the solution my project leader and I prefer.)</p>\n\n<p>Additionally, I find it can be combined with Spring security like this:</p>\n\n<pre><code>&lt;%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%&gt;\n&lt;%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %&gt;\n\n&lt;sec:authorize access="isAnonymous()"&gt;\n    &lt;% response.sendRedirect("/myApp/login/login.action?error=false"); %&gt;\n&lt;/sec:authorize&gt;\n&lt;sec:authorize access="isAuthenticated() and (hasRole(\'ADMIN\') or hasRole(\'USER\'))"&gt;\n    &lt;% response.sendRedirect("/myApp/principal/principal.action"); %&gt;\n&lt;/sec:authorize&gt;\n&lt;sec:authorize access="isAuthenticated() and hasRole(\'USER\')"&gt;\n    &lt;% response.sendRedirect("/myApp/user/userDetails.action"); %&gt;\n&lt;/sec:authorize&gt;\n</code></pre>\n\n<p>By this, not only we have control over the first page to be the login form, but we control the flow <strong>AFTER</strong> user is login in, depending on his role. Works like a charm.</p>\n'}, {'answer_id': 36919549, 'author': 'Ravi MCA', 'author_id': 1588361, 'author_profile': 'https://Stackoverflow.com/users/1588361', 'pm_score': 0, 'selected': False, 'text': '<p>Below code can be used in struts.xml to load welcome page.</p>\n\n<p>Execute some Action before loading a welcome page.</p>\n\n<pre><code>&lt;!-- welcome page configuration -begin --&gt;\n    &lt;action name="" class="com.LoginAction"&gt;\n        &lt;result name="success"&gt;login.jsp&lt;/result&gt;\n    &lt;/action&gt;\n&lt;!-- welcome page configuration -end --&gt;\n</code></pre>\n\n<p>Return directly some JSP without execution of an Action.</p>\n\n<pre><code>&lt;!-- welcome page configuration -begin --&gt;\n    &lt;action name=""&gt;\n        &lt;result name="success"&gt;login.jsp&lt;/result&gt;\n    &lt;/action&gt;\n&lt;!-- welcome page configuration -end --&gt;\n</code></pre>\n\n<p>No <code>&lt;welcome-file-list&gt;</code> is not needed in web.xml</p>\n'}]
['https://Stackoverflow.com/questions/39399', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3913/']