0

这个问题被问了很多次,但不知何故我无法让我的程序正常工作。

这是我的java程序:

public static void main( String [] args ) {

    try {

        URL url = new URL("http://localhost:8080/mywebapp/servletname");
        URLConnection conn = url.openConnection();
        conn.setDoOutput(true);

        BufferedWriter out = 
            new BufferedWriter( new OutputStreamWriter( conn.getOutputStream() ) );
        //out.write("xml=" + param1 + "\r\n");
        out.write("xml=xmltest\r\n");
        out.flush();
        out.close();
        BufferedReader in = 
            new BufferedReader( new InputStreamReader( conn.getInputStream() ) );

        String response;
        while ( (response = in.readLine()) != null ) {
            System.out.println( response );
        }
        in.close();
    }
    catch ( MalformedURLException ex ) {
        ex.printStackTrace();
        // a real program would need to handle this exception
    }
    catch ( IOException ex ) {
        ex.printStackTrace();
        // a real program would need to handle this exception
    }
}

调用 conn.getInputStream() 时抛出异常

异常抛出在这里:

java.io.IOException: Server returned HTTP response code: 500 for URL: http://localhost:8080/webapp/servletname  at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1436)
at com.barcap.mdm.nh.cron.TestUpload.main(TestUpload.java:35)

我的 servlet 代码在这里:

public class servletname extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
 * @see HttpServlet#HttpServlet()
 */
public servletname() {
    super();
    // TODO Auto-generated constructor stub
}

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    System.out.println("GET method reached .... but it's not supported");
    throw new ServletException("'GET' method not supported. 'POST' method required.");
    //doPost(request, response);
}

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    System.out.println("POST method reached");

    response.setContentType("text/xml");
    List<UploadUserVO> list = null;
    String xml = request.getParameter("xml");
    String responseXML = "<?xml version=\"1.0\"?><response result=\"__responseString__\"></response>";

    System.out.println("XML : " + xml);
    PrintWriter out = response.getWriter();

    try {
                  // perform some task here 
               } catch (Exception e)
}}}}

有人可以指出我在做什么缺少什么或有什么问题吗?

谢谢您的帮助。

4

0 回答 0