【问题标题】:Display new HTML page in servlet在 servlet 中显示新的 HTML 页面
【发布时间】:2014-01-07 07:13:58
【问题描述】:

我有一个 HTML 页面,它将其表单数据(数据库凭据)发布到 servlet。在 servlet 中,我使用凭据创建连接。

现在我想显示一个 HTML 页面,该页面将接受要输入的记录的详细信息。然后我想检索这些数据并将其输入数据库。 我知道如何使用 response.getWriter() 来显示一个新的 HTML 页面,但现在我是否设置这个页面再次将其数据发布到同一个 servlet?这是如何运作的?我应该如何编写 servlet?

这第二篇文章会启动一个新的 servlet 实例吗?没看懂,求帮助,谢谢!

【问题讨论】:

    标签: java html jsp servlets jstl


    【解决方案1】:

    你不应该让 Servlet 写完整的 HTML 作为响应,你应该使用 JSP,将 servlet 转发请求中的数据设置为 JSP,它会用你的动态内容呈现 HTML

    完整示例请参见servlet wiki 和说明

    【讨论】:

      【解决方案2】:

      总的来说,你的方法很笨拙,你可以稍微修改一下。

      I have an HTML page which posts its form data (DB credentials) to a servlet. 
      In the servlet, I create a connection using the credentials.
      

      建立连接。检查连接是否建立

          Now I want to display an HTML page which will accept the details of a record to be entered. 
         I want to then retrieve this data and enter it into the DB
      

      如果建立将其重定向到第二个 html 页面,您需要创建一个 html 页面来接受用户的输入并将其发布到 servlet。您甚至可以将其发布到同一个 servlet 并使用输入字段进行处理,或者您甚至可以将其发布到另一个 servlet

        Will this second post start a new servlet instance?
      

      Web 容器不会每次都创建一个新对象,而是在应用运行之前共享同一个实例

      而且我不知道您为什么要在 servlet 中显示 html 内容。 JSP 仅用于此目的。

      希望对你有帮助!

      【讨论】:

        猜你喜欢
        • 2016-02-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-04
        • 1970-01-01
        相关资源
        最近更新 更多