繁体   English   中英

Servlet未显示从html页面获取的数据

[英]Servlet is not displaying the data fetched from an html page

我是Java Web开发的新手,刚开始使用带有“ Welcome Servlet”项目的Servlet。 它具有3个文件,其中1.默认包中为ColServlet.java。 2. WebContent文件夹中的index.html。 3. WEB-INF文件夹中的web.xml。 ColServlet.java是:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
   public class ColServlet extends HttpServlet
{

public void doGet(HttpServletRequest request,HttpServletResponse response) 
                throws ServletException, IOException  

 {  
String colname =  request.getParameter("col"); 
response.setContentType("text/html");
PrintWriter info = response.getWriter();

info.println("The color is: ");
info.println(" <HTML>\n" +
            "<HEAD><TITLE>Hello WWW</TITLE></HEAD>\n" +
            "<BODY>\n" +
            "<H1>Hello WWW</H1>\n" +
            "<h1>"+colname+"</h1>"+
            "</BODY></HTML>");
info.close();
}
}

index.html文件是:

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
            <title>Select Color</title>
    </head>
    <body>
        <form method="GET" action="/ColServlet">
            Select the color:
            <select name="col" size="3">
                <option value="blue">Blue</option>
                <option value="orange">Orange</option>
            </select>
            <input type="submit" value="Submit">
        </form>
    </body>
</html> 

web.xml文件是:

<web-app>
    <servlet>
        <servlet-name>Servlet1</servlet-name>
        <servlet-class>ColServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Servlet1</servlet-name>
        <url-pattern>/ColServlet</url-pattern>
    </servlet-mapping>
</web-app>

现在:问题是每当我选择任何一种颜色时,下一页都不会显示所选的颜色。

@SotiriosDelimanolis的注释中 ,您应该在<form>更改操作路径,以也使用应用程序的上下文路径。 您应该在index.html文件中更新以下行:

<form method="GET" action="<applicationName>/ColServlet">

其中<applicationName>是Web应用程序的名称,通常是Web项目的名称,例如“ WelcomeServet”(不带引号)。

如果您恰好使用了JSP文件(就像将index.html重命名为index.jsp一样容易),那么请将此行更新为:

<form method="GET" action="${pageContext.request.contextPath}/ColServlet">

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM