本帖最后由 jmy815 于 2011-03-02 09:55:40 编辑

解决方案 »

  1.   

    拼 html  
    类似这样 out.println( " <html> "); 
            out.println( " <head> <title> TestServlet </title> </head> "); 
            out.println( " <body   bgcolor=\ "#ffffff\ "> "); 
            out.println( " <p> The   servlet   has   received   a   POST.   This   is   the   reply. </p> ");         out.println( " </body> </html> ");     } 
        //Process   the   HTTP   Put   request 
        public   void   doPut(HttpServletRequest   request,   HttpServletResponse   response)   throws   ServletException,   IOException   { 
        } 
        //Process   the   HTTP   Delete   request 
        public   void   doDelete(HttpServletRequest   request,   HttpServletResponse   response)   throws   ServletException,   IOException   { 
        } 
        //Clean   up   resources 
        public   void   destroy()   { 
        } 
      

  2.   

    你是要在客户端显示retCode还是在客户端要拿到retCode进行操作啊?
    显示retCode的话,就可以像之前那样拼html
    拿到retCode的话,可以用ajax或jquery等获取
      

  3.   

    拿到retCode的话,也可以用request.setAttribute()等设置下
    然后前台再获取
      

  4.   

    楼主你忘了写工程名了
    请求servlet的路径应该是
    http://localhost:8080/工程名称/MyTest
    另外为了能够正确的在页面上显示汉字,解决乱码问题,那个方法可以改写为:
    private void doProcess(HttpServletRequest request,
    HttpServletResponse response) throws ServletException, IOException {
    String retCode = "返回信息";
    response.setCharacterEncoding("gbk");
    PrintWriter toClient = response.getWriter();
    toClient.println(retCode);
    }
      

  5.   

    这种情况一般是ajax用到的
    直接自定义一个格式
    例如xml
    根据responsetext/xml自行解析就是了
      

  6.   

    楼上兄弟  那我怎么在客户端到流里面取到retCode的值来用呢  如果有代码的话最好了
      

  7.   

    我帮你写了一下,要想在页面上取得那个值就在servlet那个值放入request对象里了
    MyTest.javaimport java.io.IOException;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;public class MyTest extends HttpServlet {
    public MyTest() {
    super();
    } public void destroy() {
    super.destroy(); // Just puts "destroy" string in log
    } public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException { doProcess(request, response);
    } public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException { doProcess(request, response);
    }
    public void init() throws ServletException {
    // Put your code here
    } private void doProcess(HttpServletRequest request,
    HttpServletResponse response) throws ServletException, IOException {
    String retCode = "返回信息";
    //设定编码
    request.setCharacterEncoding("gbk");
    //将retCode放到request对象中
    request.setAttribute("retCode", retCode);
    //转发到index.jsp页面,然后在index.jsp页面通过el表达式或者request.getAttribute("retCode")获得retCode的值
    request.getRequestDispatcher("/index.jsp").forward(request, response);
    }
    }index.jsp
    <%@ page language="java" import="java.util.*" pageEncoding="gbk" isELIgnored="false"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        
        <title>My JSP 'index.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
      </head>
      
      <body>
        从servlet获取的值:${retCode}
    或者通过java代码<%=request.getAttribute("retCode")%>
      </body>
    </html>