问个傻傻的问题,如何接收某个servlet的response;

greenboat 2003-03-25 10:11:02
比如:jsp中用form post提交了一请求给某servlet,该servlet经处理后生成一response,我现要在前面那个jsp中得到该response,该怎么做呢?
...全文
19 点赞 收藏 9
写回复
9 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
sparkwu 2003-03-25
Modify:
Try it:
jsp1.htm
<HTML>
<BODY>
<FORM ACTION="jsp/jsp1.jsp" METHOD="POST">
<INPUT NAME="sample" VALUE="">
<BUTTON TYPE="SUBMIT">
SUBMIT
</BUTTON>
</FORM>
</BODY>
</HTML>

jsp1.jsp
<HTML>
<BODY>
<%
String param1 = request.getParameter("sample");
if (param1 != null)
{
RequestDispatcher rd = request.getRequestDispatcher("/servlet/practice.Servlet1");
rd.forward(request, response);
}
else
{
out.println("No selections were made");
}
%>
</BODY>
</HTML>

Servlet1.java
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class Servlet1 extends HttpServlet
{
private static final String CONTENT_TYPE = "text/html";
/**Initialize global variables*/
public void init(ServletConfig config) throws ServletException
{
super.init(config);
}

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);

// The following gets the original value posted in the HTML form:
String aString = request.getParameter("sample");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>Servlet1</title></head>");
out.println("<body>");
out.println("<p>Servlet 1 has received a POST. This is the reply:</p>");
out.println(aString);
out.println("</body></html>");

// This is an example of how to forward a post
RequestDispatcher rd = request.getRequestDispatcher("jsp/jsp1.jsp");
rd.include(request, response);
}

/**Clean up resources*/
public void destroy()
{
}
}
回复
sparkwu 2003-03-25
Try it:
jsp1.htm
<HTML>
<BODY>
<FORM ACTION="jsp/jsp1.jsp" METHOD="POST">
<INPUT NAME="sample" VALUE="">
<BUTTON TYPE="SUBMIT">
SUBMIT
</BUTTON>
</FORM>
</BODY>
</HTML>

jsp1.jsp
<HTML>
<BODY>
<%
String param1 = request.getParameter("sample");
if (param1 != null)
{
RequestDispatcher rd = request.getRequestDispatcher("/servlet/practice.Servlet1");
rd.forward(request, response);
}
else
{
out.println("No selections were made");
}
%>
</BODY>
</HTML>

Servlet1.java
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class Servlet1 extends HttpServlet
{
private static final String CONTENT_TYPE = "text/html";
/**Initialize global variables*/
public void init(ServletConfig config) throws ServletException
{
super.init(config);
}

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);

// The following gets the original value posted in the HTML form:
String aString = request.getParameter("sample");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>Servlet1</title></head>");
out.println("<body>");

// This shows the data from the post from choices.html
out.println("<p>Servlet 1 has received a POST. This is the reply:</p>");
out.println(aString);
out.println("</body></html>");

// This is an example of how to forward a post (in this case from choices.html to servlet2):
RequestDispatcher rd = request.getRequestDispatcher("jsp/jsp1.jsp");
rd.include(request, response);
}

/**Clean up resources*/
public void destroy()
{
}
}
回复
dybine 2003-03-25
jsp也是编译成servlet执行的,有response、request、session等对象的
回复
greenboat 2003-03-25
这些应些在servlet中吧,在jsp中是否直接就可用response对象了。
回复
kreven 2003-03-25
同意
回复
jcq 2003-03-25
RequestDispatcher rd = request.getRequestDispatcher(url);
rd.forward(request, response);
回复
greenboat 2003-03-25
能不能说的详细点。最好写几句代码,就一目了然了。
回复
jcq 2003-03-25
forward
回复
greenboat 2003-03-25
我想在servlet中把数据写到response中的stream中,没想到他却输出在屏幕上了。
DataOutputStream out = new DataOutputStream(response.getOutputStream));
out.writeByte("hello");
怎么会在屏幕上输出呢?

我在serlvet中写了
RequestDispatcher rd = request.getRequestDispatcher("resJsp.jsp");
rd.forward(request, response);
在resJsp中,应怎样接收呢?

回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2003-03-25 10:11
社区公告
暂无公告