问题描述
是否可以在index.jsp
上调用servlet?我的欢迎文件是index.jsp
.打开index.jsp
时,我需要通过servlet填充下拉列表值.
Is there is any way to call a servlet on index.jsp
? My welcome file is index.jsp
. I need to populate dropdown list values by a servlet when index.jsp
is opened.
我试图在web.xml
中设置<load-on-startup>
,但是没有任何效果.如何获得欢迎文件index.jsp
来调用servlet?
I tried to set <load-on-startup>
in web.xml
, but it didn't have any effect. How do I get the welcome file index.jsp
to call the servlet?
推荐答案
只需将欢迎文件URL更改为servlet之一即可.
Just change the welcome file URL to be the one of the servlet.
给出此servlet映射,
Given this servlet mapping,
<servlet-mapping>
<servlet-name>indexServlet</servlet-name>
<url-pattern>/index</url-pattern>
</servlet-mapping>
只有此欢迎文件列表:
<welcome-file-list>
<welcome-file>index</welcome-file>
</welcome-file-list>
不要忘记将/index.jsp
移到/WEB-INF
文件夹中,以防止最终用户猜测它的URL而直接访问它(也不要忘记更改索引servlet中的前向调用以指向).
Don't forget to move the /index.jsp
into /WEB-INF
folder to prevent it from being accessed directly by endusers guessing its URL (and don't forget to alter the forward call in the index servlet to point to /WEB-INF/index.jsp
).
或者,如果您仅打算拥有主页servlet"而不是索引servlet",则将servlet映射到空字符串URL模式,而不是作为欢迎文件.
Or if you solely intend to have a "home page servlet" and not an "index servlet", then map the servlet to the empty string URL pattern instead of as welcome file.
<servlet-mapping>
<servlet-name>indexServlet</servlet-name>
<url-pattern></url-pattern>
</servlet-mapping>
另请参见:
- 如何在jsp页面加载时调用servlet?
- servlet映射URL模式中/和/*之间的区别
- How to call a servlet on jsp page load?
- Difference between / and /* in servlet mapping url pattern
See also:
这篇关于如何在index.jsp上加载servlet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!