关于servlet的一个问题

小灯光环
博客专家认证
2014-01-08 06:46:29
接触java时间也比较长了,一直对这个基础问题没搞清楚。所以在此请教各位大神:
就是servlet和jsp页面之间的跳转以及交互数据的问题,据我知道的有几种:
1.response.sendDirect().重定向。访问servlet即可跳转到指定的jsp。
2.request.getRequestDispacther().forword().请求转发。访问servlet,并可将作用域中的数据转发到指定的jsp。
3.response.getWriter().write().将指定内容作为响应写到前台。

我的问题:
1.第1,2种和第3种之间有什么关系,区别或者联系?
2.第三种具体原理是什么?我只知道ajax的响应可以接受到servlet中write出去的东西,但具体原理不清楚。
3.还有就是使用流给前台写数据和request.getRequestDispacther().forword()给前台发送数据有什么区别?
4.前两种和第三种能不能在一个servlet并存?比如我一边转发数据,一边用Writer给前台写,这个是什么情况?前台怎么取?

敏而好学不耻下问,遂求助各位希望这次能彻底搞清楚。谢谢。
...全文
248 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
Boki 2014-01-10
  • 打赏
  • 举报
回复
1.1是发指令给浏览器,叫他自己访问别人,2是服务器帮你联系别人,3和12没直接关系; 2.最原始的服务器可以说就是用socket直接通信,楼主应该花几分钟自己写个模拟的服务端/客户端程序,会发现你要写数据给另一端时,就是用的流来写;流这东西跟信纸一样,先往纸上写东西,才能投递给远处,当你把端口改成8080,就是个简易的tomcat了; 3.流和转发也没有直接关系,当你使用流时,就是要往另一端写数据通信(或收数据)了,而转发相当于调用另一个类,你这个类处理完了,把你接下来要做的事转给转发的类,由它自己决定接下来是干嘛,是保存文件,是用流写东西,随你定. 4.第四个问题到没试过,不过你要知道,Writer不是边写边传的,你要用flush还是close才能发出去,在这之前你一直是在服务器端写流的,就像信纸没写完是不会拿去投递的. 因此close和转发你先用那个,就会告知服务器你已经处理完,服务器会马上发送,就像return一样,下面语句失效了. ps:4是猜想,等有时间回来试下,lz你可以自己试试印象更深.
jackson_fighting 2014-01-09
  • 打赏
  • 举报
回复
Q.还有就是使用流给前台写数据和request.getRequestDispacther().forword()给前台发送数据有什么区别 A.流给前台写数据是服务器端行为。request.getRequestDispacther().forword() 是客户端的请求
jackson_fighting 2014-01-09
  • 打赏
  • 举报
回复
Q.第三种具体原理是什么?我只知道ajax的响应可以接受到servlet中write出去的东西,但具体原理不清楚。 A.原理这东西只有看源码了。
response.getWriter().print(result);
看下response.getWriter()获取一个PrintWriter继承自Writer 看下write的实现
 /**
     * Writes a portion of a string.
     * @param s A String
     * @param off Offset from which to start writing characters
     * @param len Number of characters to write
     */
    public void write(String s, int off, int len) {
	try {
	    synchronized (lock) {
		ensureOpen();
		out.write(s, off, len);
	    }
	}
	catch (InterruptedIOException x) {
	    Thread.currentThread().interrupt();
	}
	catch (IOException x) {
	    trouble = true;
	}
    }
ensureOpen(); 确保流已经打开了
  /**
     * Writes a portion of a string.
     *
     * @param  str
     *         A String
     *
     * @param  off
     *         Offset from which to start writing characters
     *
     * @param  len
     *         Number of characters to write
     *
     * @throws  IndexOutOfBoundsException
     *          If <tt>off</tt> is negative, or <tt>len</tt> is negative,
     *          or <tt>off+len</tt> is negative or greater than the length
     *          of the given string
     *
     * @throws  IOException
     *          If an I/O error occurs
     */
    public void write(String str, int off, int len) throws IOException {
	synchronized (lock) {
	    char cbuf[];
	    if (len <= writeBufferSize) {
		if (writeBuffer == null) {
		    writeBuffer = new char[writeBufferSize];
		}
		cbuf = writeBuffer;
	    } else {	// Don't permanently allocate very large buffers.
		cbuf = new char[len];
	    }
	    str.getChars(off, (off + len), cbuf, 0);
	    write(cbuf, 0, len);
	}
    }
就是普通的输出流了。
jackson_fighting 2014-01-09
  • 打赏
  • 举报
回复
Q.第1,2种和第3种之间有什么关系,区别或者联系? A.回答这个问题之前应该需要回答一下什么是request和response Servlet框架是由两个java包组成:javax.servlet和javax.servlet.http; javax.servlet包:包中定义了所有的servlet类都必须实现的接口和类(与Http协议无关的一般性servlet类); 此包里面的ServetRequest是定义一个对象封装客户向Servlet的请求信息 ServletResponse 定义一个对象辅助Servlet讲请求的信息发送给客户端 javax.servlet.http包:包中定义了采用Http协议通信的HttpServlet类(增加了与Http协议有关的功能); 此包的HttpServletRequest/Resonse 扩展自ServletRequet/Response接口。 request均是客户端行为,response均是服务端行为 response.getWriter().write() 可以看一下源码 response.getWriter() 返回的是PrintWriter
/**
     * Returns a <code>PrintWriter</code> object that
     * can send character text to the client.
     * The <code>PrintWriter</code> uses the character
     * encoding returned by {@link #getCharacterEncoding}.
     * If the response's character encoding has not been
     * specified as described in <code>getCharacterEncoding</code>
     * (i.e., the method just returns the default value 
     * <code>ISO-8859-1</code>), <code>getWriter</code>
     * updates it to <code>ISO-8859-1</code>.
     * <p>Calling flush() on the <code>PrintWriter</code>
     * commits the response.
     * <p>Either this method or {@link #getOutputStream} may be called
     * to write the body, not both.
     *
     * 
     * @return 		a <code>PrintWriter</code> object that 
     *			can return character data to the client 
     *
     * @exception UnsupportedEncodingException
     *			if the character encoding returned
     *			by <code>getCharacterEncoding</code> cannot be used
     *
     * @exception IllegalStateException
     *			if the <code>getOutputStream</code>
     * 			method has already been called for this 
     *			response object
     *
     * @exception IOException
     *			if an input or output exception occurred
     *
     * @see 		#getOutputStream
     * @see 		#setCharacterEncoding
     *
     */

    public PrintWriter getWriter() throws IOException;
object that can send character text to the client. 至于response.sendDirect()和request.getRequestDispacther().forword().这个就没必要说了, 如果想想更深的了解下。可以看下源码的注释吧
J猪猪侠 2014-01-09
  • 打赏
  • 举报
回复
“我既要给微信服务器发送xml数据(通过write()),又要给mobile page转发数据(通过getRequestDispacther().forword())。。 ”

这种用法不行的!write()后再forword,write()方法不会起作用;
再说write()是向浏览器发送数据啊
小灯光环 2014-01-08
  • 打赏
  • 举报
回复
引用 2 楼 peng_hao1988 的回复:
既然是初学,你管他什么原理呢,只要知道在什么场景下用什么方法即可。 第1、2、3种方式之间没什么关系都是跳转的不同实现方式。 第1、2种方式的区别就是 转发和重定向的区别,网上有详细的介绍不赘述。 第3种方式就是用最原始的方法将内容输出给客户端,jsp中的内容就是以这种方式输出到客户端的,为了将java和html代码分离开才用jsp来编写页面代码,你也可以不用jsp,直接通过这种方式将html代码以字符串的形式输出给客户端。
嗯。你说的这些我也知道,其实我最想知道的是能不能在同一个servlet既进行转发,又使用response.getWriter().write()。我之前做的一个微信项目就牵扯这个问题,我既要给微信服务器发送xml数据(通过write()),又要给mobile page转发数据(通过getRequestDispacther().forword())。。
小灯光环 2014-01-08
  • 打赏
  • 举报
回复
引用 1 楼 rui888 的回复:
第一第二个 网上有 第三个也有 第4个试试吧。
前两个我知道 我主要想问的是第三个 你给我说的第三个是response.getWriter().write()与out.print()的区别 我问的是response.getWriter().write()和request.getRequestDispacther().forword()的区别
桃园闲人 2014-01-08
  • 打赏
  • 举报
回复
既然是初学,你管他什么原理呢,只要知道在什么场景下用什么方法即可。 第1、2、3种方式之间没什么关系都是跳转的不同实现方式。 第1、2种方式的区别就是 转发和重定向的区别,网上有详细的介绍不赘述。 第3种方式就是用最原始的方法将内容输出给客户端,jsp中的内容就是以这种方式输出到客户端的,为了将java和html代码分离开才用jsp来编写页面代码,你也可以不用jsp,直接通过这种方式将html代码以字符串的形式输出给客户端。
tony4geek 2014-01-08
  • 打赏
  • 举报
回复
第一第二个 网上有 第三个也有 第4个试试吧。

81,091

社区成员

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

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