我试图从书中创建一个简单的servlet,但无济于事。
我使用GlassFish Server开源版3.1.2.2,jdk1.7.0_10,记事本。
root \ WEB-INF \ classes \ net \ ensode \ glassfishbook \ formhandling \ FormHandlerServlet.class:
package net.ensode.glassfishbook.formhandling;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class FormHandlerServlet extends HttpServlet
{
protected void doPost(HttpServletRequest request,HttpServletResponse response)
{
String enteredValue;
enteredValue = request.getParameter("enteredValue");
response.setContentType("text/html");
PrintWriter printWriter;
try
{
printWriter = response.getWriter();
printWriter.println("<p>");
printWriter.print("You entered: ");
printWriter.print(enteredValue);
printWriter.print("</p>");
}
catch (IOException e)
{
e.printStackTrace();
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
{
try
{
response.setContentType("text/html");
PrintWriter printWriter = response.getWriter();
printWriter.println("<h2>");
printWriter.println("If you are reading this, your application server is good to go!");
printWriter.println("</h2>");
}
catch (IOException ioException)
{
ioException.printStackTrace();
}
}
}
根/ web.xml中:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<welcome-file-list>
<welcome-file>dataentry.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>FormHandlerServlet</servlet-name>
<servlet-class>
net.ensode.glassfishbook.formhandling.FormHandlerServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FormHandlerServlet</servlet-name>
<url-pattern>/formhandlerservlet</url-pattern>
</servlet-mapping>
</web-app>
root / dataentry.html:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=UTF-8">
<title>Data Entry Page</title>
</head>
<body>
<form method="post" action="formhandlerservlet">
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td>Please enter some text:</td>
<td><input type="text" name="enteredValue" /></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Submit"></td>
</tr>
</table>
</form>
</body>
</html>
我通过控制台中的以下命令创建WAR文件:
cd e:\root\
jar cvf formhandling.war *
接下来,我将我的WAR文件复制到'autodeploy'目录。
The link: http://localhost:8080/formhandling/
gives me the error: HTTP Status 404, description - The requested resource () is not available.
The link: http://localhost:8080/formhandling/dataentry.html
gives me the error: HTTP Status 404, description - The requested resource () is not available.
The link: http://localhost:8080/formhandling/formhandlerservlet
gives me the right response: “If you are reading this, your application server is good to go!”
Glassfish似乎无法找到 dataentry.html 文件,但我从书中复制了所有代码,但不知道该怎么做。
答案 0 :(得分:0)
本书中的代码无效,因为dataentry.html是在WEB-INF文件夹中创建的,我更正了这个错误:
<welcome-file-list>
<welcome-file>/dataentry.html</welcome-file>
</welcome-file-list>
现在,我的html文件位于根目录中。它有效。