【问题标题】:html form not rendering in java servlethtml表单未在java servlet中呈现
【发布时间】:2012-04-27 22:46:51
【问题描述】:

我正在做一个非常简单的教程来学习java servlet 编程。出于某种原因,在 Eclipse 中执行教程时,浏览器没有呈现我正在编写的 html 表单中的提交按钮。我已经检查了很多次代码,包括尝试了许多小的更改,甚至通过谷歌搜索确认我确实记得我的基本 html。但问题依然存在。

具体来说,当我从 Eclipse 中在本地 Tomcat 服务器上运行 SimpleForm.html 时,它会呈现 userName 输入框,但之后它只会输出所有 html 代码的文本版本,从提交的输入标记开始按钮。

这是重现我的问题所需的信息。谁能告诉我如何解决这个问题?

教程在这个链接,大约九分半钟:http://www.youtube.com/watch?v=MnUJl3NYRRc&feature=endscreen&NR=1

eclipse中打开了三个文件,包括:

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-    app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>SimpleServletProject</display-name>
    <servlet>
    <servlet-name>xmlServlet</servlet-name>
    <servlet-class>org.koushik.javabrains.XmlServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>xmlServlet</servlet-name>
    <url-pattern>/xmlServletpath</url-pattern>
</servlet-mapping>
</web-app>

XmlServlet.java

package org.koushik.javabrains;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class XmlServlet extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    String userName = request.getParameter("userName");
    out.println("Hello! "+ userName);   
}   
}

SimpleForm.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
    <form action='xmlServletPath'>
        <input name='userName' />
        <input type='submit' />
    </form>
</body>
</html>

【问题讨论】:

    标签: java html xml servlets forms


    【解决方案1】:

    您在 HTML 文件的表单操作中遗漏了一个“/”。应该是

    <form action='/xmlServletPath'>
    

    【讨论】:

      【解决方案2】:

      我认为属性应该用双引号引起来。试试这样改

      <form action="/xmlServletPath">
              <input name="userName" />
              <input type="submit" />
      </form>
      

      【讨论】:

      • @Guillaume Polet 解决了这个问题,然后删除了他的答案,因此它现在没有出现在这篇文章中。我在/xmlServletPath 之前缺少/。现在我做了他的建议。我确实必须在 Eclipse 工作之前重新启动它。
      • @CodeMed 我实际上对您的消息感到困惑,因为您说 HTML 被呈现为文本。很高兴它对你有用
      【解决方案3】:

      在您的 web.xml url-pattern 中是“xmlServletpath”,但实际上是“xmlServletPath” java 区分大小写,请尝试将 xmlServletpath 放入您的操作中。所以:

      <form action='xmlServletpath'>
      

      应该是正确的。

      【讨论】:

        猜你喜欢
        • 2023-03-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多