繁体   English   中英

基本Servlet不起作用

[英]Basic Servlet doesn't work

我试图从书中创建一个简单的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();
        }
    }
}

root / 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文件,但是我从书中复制了所有代码,也不知道该怎么办。

本书中的代码不起作用,因为dataentry.html是在文件夹WEB-INF中创建的,我纠正了这个错误:

<welcome-file-list>
<welcome-file>/dataentry.html</welcome-file>
</welcome-file-list>

现在,我的html文件位于根目录中。 有用。

暂无
暂无

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

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