加载索引页面时调用Servlet [英] Calling a Servlet when loading the index page

查看:74
本文介绍了加载索引页面时调用Servlet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从数据库中加载一些数据的列表,并将其显示在索引JSP页面上.我的方法是像这样在索引页面中调用Servlet.

I need to load a list of some data from a database and show them on my index JSP page. My way of doing that is call a Servlet in the index page like this.

<body>
    <jsp:include page="listAll" />
    <fieldset>

        <legend>Search Here</legend>
        <input type="text" id="search" name="search"/>
        <input type="button" value="Search" id="searchBtn"/>

    </fieldset>

</body> 

我将在listAll servlet中进行编码,并向请求中添加一个属性.我需要知道的是,这种方法正确吗?我可以那样做吗? 如果没有,该怎么办?

I am going to do my coding in listAll servlet and add an attribute to the request. What I need to know is, Is this way correct? Can I do what I need like that? If not, How to do that?

推荐答案

在Java Web App中,有一个名为welcome-file-list的web.xml文件的属性.

In a Java Web App, there is an attribute of the web.xml file called the welcome-file-list.

welcome-file-list告诉Web应用程序,如果未指定更具体的内容,则要拉出哪些URL.

The welcome-file-list tells the web app what URLs to pull up if nothing more specific is specified.

通常,该值是为index.jsp配置的,但是它可以是Web应用程序中的任何映射.

Typically, this value is configured for index.jsp, but it can be any mapping within the web application.

如果要让servlet响应而不是index.jsp,则可以正确地映射servlet,然后在welcome-file-list中使用该引用.

If you wanted to have a servlet respond, instead of index.jsp, then you would map the servlet properly, and then use that reference in the welcome-file-list.

考虑:

<welcome-file-list>
    <welcome-file>index</welcome-file>
</welcome-file-list>

<servlet>
    <servlet-name>IndexServlet</servlet-name>
    <servlet-class>pkg.IndexServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>IndexServlet</servlet-name>
    <url-pattern>/index</url-pattern>
</servlet-mapping>

在这里,我们已将pkg.IndexServlet映射到/index.我们还告诉应用程序index是此应用程序的欢迎文件.因此,在内部,当应用程序看到http://host.com/webapp时,它将自动将index附加到其上,然后对其进行适当的路由,这会将其引导至映射到/index的servlet.

Here we have the pkg.IndexServlet mapped to /index. We also tell the application that index is the welcome file for this application. So, internally, when the application sees http://host.com/webapp, it will automatically append index to it, and then route that appropriately, which will lead it to the servlet mapped to /index.

一旦正确地映射了该映射,就想在@Matthias上做一个模式,servlet在该模式下收集数据,然后转发到JSP.

Once you have this mapped properly, you want to do a pattern much @Matthias did here, where the servlet gathers the data, and then forwards to the JSP.

这篇关于加载索引页面时调用Servlet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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