gpt4 book ai didi

java - web.xml使用tomcat server 8.0的问题

转载 作者:行者123 更新时间:2023-11-29 00:02:20 25 4
gpt4 key购买 nike

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

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

这是我的类(class):

首先打包:

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、class和web.xml,告诉我问题出在哪里?

最佳答案

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

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

关于java - web.xml使用tomcat server 8.0的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28508193/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com