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

greenboat 2003-03-25 10:11:02
比如:jsp中用form post提交了一请求给某servlet,该servlet经处理后生成一response,我现要在前面那个jsp中得到该response,该怎么做呢?
...全文
57 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
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中,应怎样接收呢?

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧