Java 基本Servlet不';行不通
我试图从书中创建一个简单的servlet,但没有成功 我使用GlassFish服务器开源版本3.1.2.2、jdk1.7.0_10、记事本 root\WEB-INF\classes\net\ensode\glassfishbook\formhandling\FormHandlerServlet.class:Java 基本Servlet不';行不通,java,servlets,glassfish,Java,Servlets,Glassfish,我试图从书中创建一个简单的servlet,但没有成功 我使用GlassFish服务器开源版本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.Print
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();
}
}
}
<?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>
<!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文件复制到“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文件,但我复制了书中的所有代码,不知道该怎么办。书中的代码不起作用,因为dataentry.html是在文件夹WEB-INF中创建的,我纠正了这个错误:
<welcome-file-list>
<welcome-file>/dataentry.html</welcome-file>
</welcome-file-list>
/dataentry.html
现在,我的html文件在根目录中。它可以工作。您编译Java文件了吗?这一步骤似乎缺失了。另外:尝试查看Glassfish日志。@jan.vdbergh,是的,我编译了它并获得了请求工作!日志:[AutoDeploy]已成功自动部署:E:\glassfish3\glassfish\domains\domain1\AutoDeploy\formhandling.war
<welcome-file-list>
<welcome-file>/dataentry.html</welcome-file>
</welcome-file-list>