[英]how to take data from a Java servlet and put it on an existent html page
[英]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.