【问题标题】:making a servlet in netbeans and index.jsp在 netbeans 和 index.jsp 中创建一个 servlet
【发布时间】:2011-04-06 00:23:59
【问题描述】:

我正在尝试在 netbeans 中创建一个 servlet。它必须从 html 表单中调用并从中获取一些数据。

我覆盖了 doget 和 dopost,只在其中添加了一些测试。

当我尝试运行 servlet 时,它只显示一个 hello word jsp 页面,我发现这是 servlet 默认拥有的 index.jsp 页面。

我怎样才能使 servlet 运行并实际产生一些输出?除了索引欢迎页面?

我使用哪个 url mast 来从表单调用 servlet?

第 doget 和 dopost 方法如下所示

 public void doPost(HttpServletRequest request,
                  HttpServletResponse response)
    throws IOException, ServletException
    ResourceBundle rb =
    ResourceBundle.getBundle("LocalStrings",request.getLocale());
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();

    out.println("<html>");
    out.println("<head>");

    String title = rb.getString("helloworld.title");

    out.println("<title>" + title + "this is my anser</title>");
    out.println("</head>");
    out.println("</html>");



   protected void doGet(HttpServletRequest request, HttpServletResponse response)
  throws ServletException, IOException {
          this.doPost(request, response);}

【问题讨论】:

    标签: forms jsp servlets


    【解决方案1】:

    我怎样才能使 servlet 运行并实际产生一些输出?除了索引欢迎页面?

    您应该将请求分派到所需的 JSP 页面。您可以使用RequestDispatcher 来执行此操作。

    request.getRequestDispatcher("page.jsp").forward(request, response);
    

    我使用哪个 url mast 来从表单调用 servlet?

    你在web.xml中映射的那个。

    您还应该只将 HTML 放在 JSP 页面中,而不是放在 servlet 中。让doGet() 和doPost() 做同样的事情也不是一个好习惯。 doGet() 只是在 JSP 中显示之前预处理数据。 doPost() 仅用于在 HTML 表单提交后后处理数据。

    在 tag info about [servlets] 标签中,您可以找到一个 hello world 示例和更多关于从 JSP/servlets 开始的链接。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-26
      • 2017-04-20
      • 1970-01-01
      相关资源
      最近更新 更多