2014-03-05 50 views
0

我是java的Web开发新手,刚刚开始使用带有“Welcome Servlet”项目的servlet。它有3个文件,1.默认包中的ColServlet.java。 2.在WebContent文件夹中的index.html。 3. WEB-INF文件夹中的web.xml。 ColServlet.java是:Servlet没有显示从html页面获取的数据

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> 

NOW:问题是每当我选择任何颜色,下一页不显示选定的一个。

+0

中号使用Eclipse和Tomcat 7.0 –

+0

什么信息显示在下一页中? –

+0

在地址栏中输入:http:// localhost:8080/ColServlet?col = orange On网页: -------------------------------------------------- ------------- 类型状态报告 消息/ ColServlet description请求的资源不可用。 ----------------------------------------------- --------------------------------- Apache Tomcat/7.0.52 –

回答

0

@SotiriosDelimanolis' comment开始,您应该更改<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"> 
相关问题