如何在index.jsp上加载servlet [英] How load servlet on index.jsp

查看:209
本文介绍了如何在index.jsp上加载servlet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在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>

另请参见:

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆