2 qilinzhengzhuan qilinzhengzhuan 于 2016.04.09 12:26 提问

两个servlet文件间通过post方法传一个自定义类的对象

这是发送对象的servlet文件

package Test;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

class TransferData{
int a;
int b;
int c;
}

public class myservlet extends HttpServlet {

/**
 * Constructor of the object.
 */
public myservlet() {
    super();
}

/**
 * Destruction of the servlet. <br>
 */
public void destroy() {
    super.destroy(); // Just puts "destroy" string in log
    // Put your code here
}

public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    /*response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
    out.println("<HTML>");
    out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
    out.println("  <BODY>");
    out.print("    This is ");
    out.print(this.getClass());
    out.println(", using the GET method");

    out.println("-------------");
    String str=request.getParameter("max");
    int max=Integer.parseInt(str);
    int sum=0;
    for(int i=1;i<=max;i++){
        sum+=i;
    }
    out.println(sum);
    */
    this.doPost(request, response);//service默认走的是doGet方法,调用doPost方法,才会走doPost,否则不走doPost方法
    /*
    out.println("  </BODY>");
    out.println("</HTML>");
    out.flush();
    out.close();*/
}


public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
    out.println("<HTML>");
    out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
    out.println("  <BODY>");
    out.print("    This is ");
    out.print(this.getClass());
    out.println(", using the POST method");

    String urlstr="http://xz-20140612mxua:8080/J2eeTest/test/getObject";
    try{
        URL url=new URL(urlstr);
        HttpURLConnection conn=(HttpURLConnection)url.openConnection();
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");
        conn.setConnectTimeout(10000);
        conn.setReadTimeout(20000);
        conn.connect();
        TransferData td=new TransferData();
        out.println("--------------------0");
        td.a=10;
        td.b=20;
        td.c=200;
        out.println("--------------------1");
        OutputStream outputstream=conn.getOutputStream();//创建输出流,并把该输出流关联到创建的联系conn上
        out.println("--------------------2");
        ObjectOutputStream oos = new ObjectOutputStream(outputstream);//创建对象输出流
        out.println("--------------------3");
        out.println(td);
        oos.writeObject(td);//通过对象输出流输出对象?有问题控制台只能输出到-----3这里
        out.println("--------------------4");
        out.println(td.a);
        out.println("--------------------5");
    }catch(Exception e){}

    out.println("  </BODY>");
    out.println("</HTML>");
    out.flush();
    out.close();
}

public void init() throws ServletException {
    // Put your code here
}

}

这是接受对象的servlet文件

package Test;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class getObject extends HttpServlet {

/**
 * Constructor of the object.
 */
public getObject() {
    super();
}

/**
 * Destruction of the servlet. <br>
 */
public void destroy() {
    super.destroy(); // Just puts "destroy" string in log
    // Put your code here
}

/**
 * The doGet method of the servlet. <br>
 *
 * This method is called when a form has its tag value method equals to get.
 * 
 * @param request the request send by the client to the server
 * @param response the response send by the server to the client
 * @throws ServletException if an error occurred
 * @throws IOException if an error occurred
 */
public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    /*response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
    out.println("<HTML>");
    out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
    out.println("  <BODY>");
    out.print("    This is ");
    out.print(this.getClass());
    out.println(", using the GET method");
    */
    this.doPost(request,response);
    /*
    out.println("  </BODY>");
    out.println("</HTML>");
    out.flush();
    out.close();*/
}

/**
 * The doPost method of the servlet. <br>
 *
 * This method is called when a form has its tag value method equals to post.
 * 
 * @param request the request send by the client to the server
 * @param response the response send by the server to the client
 * @throws ServletException if an error occurred
 * @throws IOException if an error occurred
 */
public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
    out.println("<HTML>");
    out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
    out.println("  <BODY>");
    out.print("    This is ");
    out.print(this.getClass());
    out.println(", using the POST method");

    out.println("--------------------0");
    try{
        InputStream in = request.getInputStream(); 
        out.println("--------------------1");
        ObjectInputStream ois = new ObjectInputStream(in);//?有问题
        out.println("--------------------2");
        TransferData td1 = (TransferData)(ois.readObject());  
        out.println("--------------------3");
        System.out.println(td1.a);  
        out.println("--------------------4");
        System.out.println(td1.b);  
        out.println("--------------------5");
        System.out.println(td1.c);  
        out.println("--------------------6");
    }catch(Exception e){}

    out.println("  </BODY>");
    out.println("</HTML>");
    out.flush();
    out.close();
}

/**
 * Initialization of the servlet. <br>
 *
 * @throws ServletException if an error occurs
 */
public void init() throws ServletException {
    // Put your code here
}

}

困扰很久了,求大神帮解答,问题不解决,感觉后面的东西没法继续学。

2个回答

ljheee
ljheee   Rxr 2016.04.09 12:50

不是 太明白 你的意思

建议使用MVC模型,不要在doPost方法中out.println

显示交给JSP
存储 就交给JavaBean

Servlet处理业务逻辑

qilinzhengzhuan
qilinzhengzhuan   2016.04.09 14:44

就是myservlet(这是一个servlet文件)中有一个对象Tick tick=new Tick( );
要通过post方法发送
getObject( 这是另一个servlet文件 )来接收这个对象
然后会对接受到的对象进行操作,比如显示它的属性什么的

老师要求把这个过程弄懂,然后能传送对象成功(就是在两个servlet文件中,servlet文件就是java文件)

现在不知道代码哪里有问题,就是传送不了对象。

求帮助啊。

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!