输入输出流问题 急。。。。 本帖最后由 jmy815 于 2011-03-02 09:55:40 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 拼 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() { } } 你是要在客户端显示retCode还是在客户端要拿到retCode进行操作啊?显示retCode的话,就可以像之前那样拼html拿到retCode的话,可以用ajax或jquery等获取 拿到retCode的话,也可以用request.setAttribute()等设置下然后前台再获取 楼主你忘了写工程名了请求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);} 这种情况一般是ajax用到的直接自定义一个格式例如xml根据responsetext/xml自行解析就是了 楼上兄弟 那我怎么在客户端到流里面取到retCode的值来用呢 如果有代码的话最好了 我帮你写了一下,要想在页面上取得那个值就在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> Java socket服务器多线程 问一个spring mvc 中的beans的设置问题 Who can help me? 关于linux/unix下的JAVAEE开发的介绍。。。。 JAVA主要会什么?公司需要什么样的人才?SHH要学到什么程度? hibernate if语句问题 有个异常出现 不知道是什么原因? 小弟正学hibernate,求一个基于struts+hibernate的源码 请问学J2EE看哪本书好 怎样在TomCat启动之后,自动启动我需要的进程? SSH2 BaseDao与BaseService的配合使用 struts 2 upload
类似这样 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() {
}
}
显示retCode的话,就可以像之前那样拼html
拿到retCode的话,可以用ajax或jquery等获取
然后前台再获取
请求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);
}
直接自定义一个格式
例如xml
根据responsetext/xml自行解析就是了
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>