【问题标题】:Problems with web.xml using tomcat server 8.0使用 tomcat 服务器 8.0 的 web.xml 问题
【发布时间】:2015-04-15 00:03:12
【问题描述】:

我制作了一个简单的 tomcat servlet,完全遵循 Youtube tutorial

我创建了一个名为 XmlServlet 的类,它处理简单的 GET 和 POST 请求。

这是我的课:

打包优先:

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");
        String FullName = request.getParameter("FullName");

        System.out.println("hello u" + UserName);
    }

    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        String UserName = request.getParameter("UserName");
        String FullName = request.getParameter("FullName");
        String location = request.getParameter("location");
        String prof = request.getParameter("prof");
        out.println("hello u from dpost" + UserName + "u full name" + FullName
                + "address" + location);

    }

}

和 Servlet:

package First;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class SimpleServlet
 */
@WebServlet(description = "My First One", urlPatterns = { "/SimpleServletPath" })
public class SimpleServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public SimpleServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


    }

}

还有我的 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    id="WebApp_ID" version="3.1">
    <!--  
    <servlet>
        <servlet-name>XmlServlet</servlet-name>
        <servlet-class>First.Xmlservlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>XmlServlet</servlet-name>
<url-pattern>/SimpleServletPath</url-pattern>
    </servlet-mapping>
-->
</web-app>



and i create simple html page to represent this 
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <form method="get" action="SimpleServletPath">

        user name<input name="UserName" /> full name<input name="FullName" />
        <br>
         Work <input type="radio" name="prof" value="developer">developer</input>
        <input type="radio" name="prof" value="IT">IT</input>
         <select name="location" multiple size=3 >
            <option value="here">here</option>
            <option value="home">home</option>
            <option value="school">school</option>
            <option value="football">football</option>
            <option value="chelsea">Chelsea</option>
        </select> 
        </br> 
        <input type="Submit" />
    </form>
</body>
</html>

我无法从 HTML 页面获取任何数据。一切似乎都很好, 但我收到有关 web.xml 的错误。 有人可以帮我检查一下我的 servlet、类和 web.xml 并告诉我问题出在哪里吗?

【问题讨论】:

  • 我想你在没有评论 web.xml () 中关于 servlet 的信息的情况下尝试了这个?
  • 假设没有它 我忘记删除它,我把 放在我的代码中,因为我谈论 @CoqPwner 的问题
  • @Ascalonian web.xml 的问题,我的 html 页面无法获取任何数据

标签: java xml tomcat servlets


【解决方案1】:

所以,当您在 HTML 页面上提交时,它会转到 servlet,您会得到一个空白页面,对吗?好的,那是因为您的 servlet 在其 doGet 方法中实际上没有任何内容。请注意您的表单 actionSimpleServletPath 而不是 XmlServlet。但是等等,情况会变得更糟!在您的 SimpleServlet java 文件中,您使用注释将路径映射到 SimpleServletPath。但是随后您还将第二个 servlet,XmlServlet 映射到与 web.xml 相同的路径(!)......所以它一团糟。 (也许你实际上得到了一个错误页面,因为双重映射。)

因此,为了解决这个问题,请将每个 servlet 映射到它自己的 URL,而不是同时映射到同一个 URL。然后确保无论您从 HTML 表单实际提交到哪个 servlet,实际上都会将某些内容打印到响应中。此外,最好决定是使用注释还是使用 web.xml 将 servlet 映射到 URL,而不是混合使用这两种方法。

【讨论】:

  • XmlServlet 是类而不是 servlet,所以我只有一个 servelt 并将这个类映射到那个 url 对不起,我只是 java EE 和 servlet 的初学者,我和那个教程一样@developerwjk youtube.com/…跨度>
  • @AhmedMahrous 如果你这么认为,那么你真的很困惑,因为你没有通过 web.xml 映射常规类,而且你的 java 文件显示public class XmlServlet extends HttpServlet:你为什么要扩展 HttpServlet 如果它不是一个servlet?也许您应该考虑学习 Java Web 应用程序课程(或购买一本书)。不是所有的东西都可以通过观看 youtube 视频来学习。
  • 我和那个教程一样,所以为什么它适用于他而不是我?
  • @AhmedMahrous 视频无法学习如何编码。它对他有用,而不是你,因为它显然比他所展示的要多。或者你错过了什么。
  • 我不知道我只是一步一步地像他一样,我想学习教程,现在这个问题摆在我面前,我能做些什么
猜你喜欢
  • 2013-08-06
  • 2011-11-24
  • 2011-06-12
  • 2017-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-08
  • 1970-01-01
相关资源
最近更新 更多