0

私はEclipseIDEを使用していますが、以下のコードはブラウザに空白の画面を表示します。なぜ空白の画面が表示されるのかわかりません。何かアイデアはありますか?前もって感謝します。

/**
 * 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 {

    }
}

デプロイメント記述子ファイルを見つけてください。web.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 に答える