当前位置: 代码迷 >> Java Web开发 >> servlet里如何样获得apllication对象,并往里面邦定对象
  详细解决方案

servlet里如何样获得apllication对象,并往里面邦定对象

热度:61   发布时间:2016-04-17 14:36:36.0
servlet里怎么样获得apllication对象,并往里面邦定对象!
如题!

------解决方案--------------------
我在jsp页面中的代码如下:
<%
pageContext.setAttribute( "a ", "ok ");
request.setAttribute( "b ", "ok ");
session.setAttribute( "c ", "ok ");
application.setAttribute( "d ", "ok ");
%>

***************************
该jsp页面被转换为Servlet后的java源代码如下:
package org.apache.jsp;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;

public final class a_jsp extends org.apache.jasper.runtime.HttpJspBase
implements org.apache.jasper.runtime.JspSourceDependent {

private static java.util.List _jspx_dependants;

public Object getDependants() {
return _jspx_dependants;
}

public void _jspService(HttpServletRequest request, HttpServletResponse response)
throws java.io.IOException, ServletException {

JspFactory _jspxFactory = null;
PageContext pageContext = null;
HttpSession session = null;
ServletContext application = null;
ServletConfig config = null;
JspWriter out = null;
Object page = this;
JspWriter _jspx_out = null;
PageContext _jspx_page_context = null;


try {
_jspxFactory = JspFactory.getDefaultFactory();
response.setContentType( "text/html;charset=GBK ");
pageContext = _jspxFactory.getPageContext(this, request, response,
null, true, 8192, true);
_jspx_page_context = pageContext;
application = pageContext.getServletContext();
config = pageContext.getServletConfig();
session = pageContext.getSession();
out = pageContext.getOut();
_jspx_out = out;

out.write( "\r\n ");
out.write( " <!DOCTYPE HTML PUBLIC \ "-//W3C//DTD HTML 4.01 Transitional//EN\ "> \r\n ");
out.write( " <head> \r\n ");
out.write( "\t <title> a.jsp </title> \r\n ");
out.write( " </head> \r\n ");
out.write( " <body> \r\n ");
out.write( "\t ");

pageContext.setAttribute( "a ", "ok ");
request.setAttribute( "b ", "ok ");
session.setAttribute( "c ", "ok ");
application.setAttribute( "d ", "ok ");

out.write( "\r\n ");
out.write( " </body> \r\n ");
out.write( " </html:html> \r\n ");
} catch (Throwable t) {
if (!(t instanceof SkipPageException)){
out = _jspx_out;
if (out != null && out.getBufferSize() != 0)
out.clearBuffer();
if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
}
} finally {
if (_jspxFactory != null) _jspxFactory.releasePageContext(_jspx_page_context);
}
}
}


lz现在明白了吧。Servlet中的ServerContext就是jsp中的application对象,
在Servlet中调用:this.getServletContext().setAttribute( "aaa ", "bbb ");即可。
  相关解决方案