2016-10-03 32 views
2

我試圖做一個簡單的代碼的servlet接受來自用戶的輸入並打印。 這是我的Servlet clas-:Servlet的歡迎-file列表不工作

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> 

現在eventhough我的網頁。 xml包含歡迎文件列表,它仍然不起作用。 我在eclipse oxigen中使用Tomcat 8.5。

請幫忙。

+0

爲什麼在同一個web.xml文件中有兩個''元素?這是故意的嗎? – ujulu

回答

0

你在你的映射有錯誤不歡迎文件列表,如果你改變簡單/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> 

,因爲這是不是一個好的做法

爲了使它工作:)

+0

我試圖這樣做,但它仍然沒有工作:( – user3262269

+0

什麼樣的錯誤你得到 – 2016-10-03 12:20:02

+0

沒有錯誤的只是裝?「HTTP://本地主機:8015/T3 /歡迎」 – user3262269