【问题标题】:Servlet welcome -file-list is not working [duplicate]Servlet 欢迎 -file-list 不起作用[重复]
【发布时间】:2017-02-11 09:19:17
【问题描述】:

我正在尝试为 servlet 编写一个简单的代码来接受用户的输入并打印它。 这是我的 Servlet 类-:

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

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 welcome
 */
//@WebServlet("/welcome")
public class welcome extends HttpServlet {
    private static final long serialVersionUID = 1L;

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

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.getWriter().append("Served at: ").append(request.getContextPath());

        response.setContentType("text/html");  
        PrintWriter pw=response.getWriter();  

        String name=request.getParameter("name");//will return value  
        pw.println("Welcome "+name);  

        pw.close();  
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

我的Html页面代码-:Newfile101.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="./welcome" method="get">  
Enter your name<input type="text" name="name"><br>  
<input type="submit" value="login">  
</form> 
</body>
</html>

我的 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">
  <display-name>T3</display-name>

<welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>



  <servlet>
<servlet-name>son</servlet-name>
<servlet-class>welcome</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>son</servlet-name>
<url-pattern>/welcome</url-pattern>
</servlet-mapping>

  <welcome-file-list>  

   <welcome-file>NewFile101.html</welcome-file>  
  </welcome-file-list>  
  </web-app>

现在即使我的 web.xml 包含欢迎文件列表,它仍然无法正常工作。 我在 eclipse oxigen 中使用 Tomcat 8.5。

请帮忙。

【问题讨论】:

  • 为什么在同一个 web.xml 文件中有两个&lt;welcome-file-list&gt; 元素?是故意的吗?

标签: java xml servlets


【解决方案1】:

如果您更改为简单的/welcome,您的映射不在欢迎文件列表中,它会起作用

或者简单地说删除dot**(.)** from your form where you have added before/welcome`

还有一件事,删除

<welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>

因为这不是一个好习惯

让它工作:)

【讨论】:

  • 我试过了,但还是不行:(
  • 你遇到了什么错误?
  • 没有错误它只是加载“localhost:8015/T3/welcome
  • 你的控制台显示server has been started?
  • 是的服务器已经启动
猜你喜欢
  • 1970-01-01
  • 2021-02-12
  • 1970-01-01
  • 2013-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-16
  • 2012-05-01
相关资源
最近更新 更多