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币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!
其他相关推荐
Servlet页面间对象传递的方法
Servlet页面间对象传递的方法 1、request 2、session 3、application 4、cookie 5、其他的
两个servlet之间的值传递
很多时候我们需要用两个servlet分别来处理同一个数据这时就需要用的s
Java用POST传对象给Servlet
Java用POST传对象给Servlet
Jsp-Servlet常见 两种 页面间传递数据方式(request转发和session)
页面间传递数据方式:就是使用JSP页面的九大内置对象:域对象。 【request、response、session、application、out、pagecontext、config、page、exception】 ===和servlet的servletContext对象区别。两码事。 取值范围:不常用。 具体参考:http://www.cnblogs.com/leirenyuan/
两个JSP页面之间传对象
使用session进行两个JSP页面之间传对象1、在第一个页面session.setAttribute(&quot;u&quot;,u);2、在第二个页面UserBeen u = (UserBeen)session.getAttribute(&quot;u&quot;);
Java 用POST方式 传对象给 Servlet
转自:http://blog.csdn.net/a9529lty/article/details/6454156 Java代码 [java] view plain copy package dbConn;      import java.io.InputStream;      import java.io.ObjectOutpu
通过POST方式传递数据给服务器
1.新建动态web工程(ServerPOST)2.新建servlet。设置如下: 3.ServerPOST.javapackage com.example.servletPOST;import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet;
HttpURLConnection Post请求上传文件和参数到servlet
前台代码:  public String uplaod(String actionUrl, Map params) {          InputStream in = null;         String BOUNDARY = java.util.UUID.randomUUID().toString();         String PREFFIX = "--", LINEND
2个jsp页面之间传递类对象方法
1.每张表建一个类 如把一个类进行传参数时   第一个页面xx.jsp   // UserName为一个类对象  UserName user=new UserName();  设置相关的属性  user.setName("mes");  user.setPassword("123");  //相当用哈希map  request.setAttribute("user",user); 在另一个页面用x
java servlet使用jquery 的ajax交互 传递json对象
使用jQuery封装好的ajax方法进行ajax交互,节约了很多不必要的代码,也不用进行,浏览器兼容了。 这里使用的是.ajax方法,配置完web.xml 中的servlet之后,后台代码如下: //@ Saa public void doPost(HttpServletRequest request,HttpServletResponse response) throws S