0

我正在使用 Eclipse IDE,下面的代码在浏览器上显示了空白屏幕。我不知道为什么它显示空白屏幕。有什么想法吗?提前致谢。

/**
 * Servlet implementation class Ch1Servlet
 */
public class Ch1Servlet extends HttpServlet {
    private static final long serialVersionUID = 1L;


    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        // TODO Auto-generated method stub
        PrintWriter out=response.getWriter();
        Date dat=new Date();
        System.out.println(dat);
        out.println("<html>" + "<body>" + "<h1>Heello world</h1>" + dat + "</body>" + "</html>");       
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}

请找到部署描述符文件。网页.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>Monte</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <description></description>
    <display-name>Ch1Servlet</display-name>
    <servlet-name>Ch1Servlet</servlet-name>
    <servlet-class>Ch1Servlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Ch1Servlet</servlet-name>
    <url-pattern>/Ch1Servlet</url-pattern>
  </servlet-mapping>
</web-app>
4

1 回答 1

0

尝试这样的事情:

  package package;


 /**
   *
   * @author 
   */
   public class Ch1Servlet  extends HttpServlet {



/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
 * @param request servlet request
 * @param response servlet response
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    try {
        //you code here
    } catch (Exception exc) {
        throw new MyException(exc);
    }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
 */
@Override
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
 */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    processRequest(request, response);
}

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

上面的代码接受 get 和 post

于 2013-02-20T19:30:54.893 回答