简单的测试Servlet在GoDaddy服务器上托管时无法正常工作,为什么?

时间:2011-07-29 18:55:53

标签: java servlets

我试图通过GoDaddy服务器在共享主机上托管来测试servlet。我正在使用Java SDK 5_0_22(GoDaddy服务器的SDK version)编译代码。我在LOCAL Tomcat5.0.27(Godaddy的服务器verision)上测试了相同的代码,文件夹结构。

但是我能够运行JSP文件;这证明我的帐户启用了JAVA。请帮忙。 附:每次出现404错误。 index.jsp工作正常。

FolderStructure: -

WebApplication-- |
                | -META-INF
                | -WEB-INF --- |
| -classes-- | --test.class
| -web.xml

的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">
    <servlet>
        <servlet-name>test</servlet-name>
        <servlet-class>test</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>test</servlet-name>
        <url-pattern>/test</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
        </welcome-file-list>
    </web-app>

test.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author SAM
 */
public class test extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            // TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet test</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet test at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");

        } finally { 
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}

2 个答案:

答案 0 :(得分:2)

运行无包servlet并不适用于所有情况。默认包中的类是通过规范对真实包中的类不可见/不可移植的。据我所知,只有某些Tomcat + JVM + Windows版本组合允许这样做。但你永远不应该依赖环境依赖。您应该始终将servlet(最好是所有其他Java类!)放在一个包中。

package com.example;

// ...

public class TestServlet extends HttpServlet {

    // ...

}

编译的类应该以{{1​​}}结尾,并且servlet映射应该更新如下:

/WEB-INF/classes/com/example/TestServlet.class

另请注意,我已将您的类名修改为以大写符合Java naming conventions开头。


无关具体问题,我知道你刚开始使用servlet,而且你的servlet只是一个虚拟的测试例子。我只强调在servlet中以这种方式发出HTML被认为是一种不好的做法。应该使用JSP。另请参阅our servlet wiki page

答案 1 :(得分:2)

GoDaddy似乎不支持共享主机空间网站上的java:

http://support.godaddy.com/help/article/65/adding-a-java-servlet-to-your-web-site

也许使用专用服务器就可以做到。