既然jsp和servlet是等价的,在jsp中能够使用内置对象,那么在servlet中也能够使用。

1.获得out对象

能够使用例如以下代码获得out对象:

import java.io.PrintWriter;

...

public void doGet(HttpServletRequest request,HttpServletResponse response)throws

ServletException,IOException{

PrintWriter out = reponse.getWriter();

}

...

只是默认情况下,out对象是无法打印中文的。这是由于out输出流中有中文却没有设置编码。解决问题能够将

doGet代码改为:

response.setContentType("text/html;charset=gb2312");

PrintWriter out = response.getWriter();

//使用out对象

2.获得request和reponse对象

...

public void doGet(HttpServletRequest request,HttpServletResponse response)throws

ServletException,IOException{

//将reques參数当成request对象使用

//将reponse參数当成response使用

}

...

3.获得session对象

session对象相应的是HttpSession接口,在Servlet中它能够通过以下代码获得:

import javax.servlet.http.HttpSession;

...

public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{

HttpSession session = request.getSession();

//将session当成session对象来使用

}

...

4.获得application对象

application对象相应得是ServletContex接口,在Servlet中能够通过以下代码获得:

import javax.servlet.ServletContext;

...

public void doGet(HttpServletRequest request,HttpServletResponse response)throws

ServletException,IOException{

ServletContext application = this.getServletContext();

//将application当成application对象来使用

}

...

值得一提的是,能够使用application实现server内跳转。因为servlet和jsp的同质性,经常使用的servlet内跳转有两种:

(1)重定向(相应jsp中的sendRedirect)

response.sendRedirect("URL地址");

(2)server内跳转(相应jsp中的forward)

ServletContext application = this.getServletContext();

RequestDispatcher rd = application.getRequestDispatcher("url地址");

rd.forward(request,response);

这两种在servlet内的跳转与jsp中提到的跳转是等效的。注意,两种情况下的url地址写法不一样。在第一种,假设写绝

对路径,必须将虚拟文件夹跟文件夹写在里面,如"/Proj09/page.jsp"而另外一种方法中,不须要将虚拟路径根文件夹写在里

面,如“/page.jsp”

javaEE servlet获取jsp内置对象的更多相关文章

  1. Servlet中获取JSP内置对象

    方便自己查询,嫌低级的勿喷.... 1.request 在servlet的doGet和doPost的参数中就有HttpServletRequest req参数,而JSP内置request对象就是Htt ...

  2. Servlet与JSP内置对象的对应关系、Servlet获取表单、Servlet路径跳转

    项目的根目录指的是webroot: 服务器内部跳转: 或者使用../:“..”代表回到上一级目录

  3. JavaWeb#JSP内置对象

    [1.JSP内置对象简介] 内置对象:不加声明就可以在JSP页面脚本中使用的成员变量.(使用这些对象可以更容易收集客户端发送的请求信息,响应客户端的请求及存储客户信息.) 主要介绍:out,reque ...

  4. JSP内置对象与servlet对应关系

    隐式对象 说明 out 转译后对应JspWriter对象,其内部关联一个PringWriter对象 request 转译后对应HttpServletRequest/ServletRequest对象 r ...

  5. JavaWeb学习----JSP内置对象详解

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...

  6. jsp内置对象浅谈

    jsp内置对象浅谈 | 浏览:1184 | 更新:2013-12-11 16:01 JSP内置对象:我们在使用JSP进行页面编程时可以直接使用而不需自己创建的一些Web容器已为用户创建好的JSP内置对 ...

  7. Jsp内置对象及EL表达式的使用

    一.JSP的内置对象(9个JSP内置对象) JSP的内置对象引用名称 对应的类型 request HttpServletRequest response HttpServletResponse ses ...

  8. JavaWeb之 JSP:内置对象,EL表达式,JSP标签基础

    JSP的内置对象 什么是JSP的内置对象呢? 在JSP页面进行编程的时候,如果我们要使用一些对象,如:HttpSession,ServletConfig,ServletContext这些对象,如果每次 ...

  9. JSP内置对象详解

    jsp中内置对象:request.response.session.applecation.out.pagecontesx.config.page.exception.cookie 1.request ...

随机推荐

  1. GO学习资源站

    GO语言学习资源网站 http://golangtc.com https://gobyexample.com http://golang-examples.tumblr.com

  2. C++ new和delete实现原理——new和delete最终调用malloc和free

    new和delete最终调用malloc和free,关于malloc和free实现原理参见这篇文章: http://blog.csdn.net/passion_wu128/article/detail ...

  3. 常用的wsdl地址

    天气预报Web Service,数据来源于中国气象局 Endpoint Disco WSDL IP地址来源搜索Web Service(是目前最完整的IP地址数据) Endpoint Disco WSD ...

  4. HTTP based RESTful APIs - asp.net web api

    1.HTTP http://www.w3.org/Protocols/rfc2616/rfc2616.html 2.REST是什么http://www.ics.uci.edu/~fielding/pu ...

  5. [Leetcode][Python]51: N-Queens

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 51: N-Queenshttps://oj.leetcode.com/pro ...

  6. [Leetcode][Python]49: Anagrams

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 49: Anagramshttps://leetcode.com/proble ...

  7. 2.6. Statistical Models, Supervised Learning and Function Approximation

    Statical model regression $y_i=f_{\theta}(x_i)+\epsilon_i,E(\epsilon)=0$ 1.$\epsilon\sim N(0,\sigma^ ...

  8. hdu1258Sum It Up (DFS)

    Description Given a specified total t and a list of n integers, find all distinct sums using numbers ...

  9. 2、Lucene 最简单的使用(小例子)

    在了解了Lucene以后,我打算亲手来做一个Lucene的小例子,这个例子只是Lucene最简单的应用:使用Lucene实现标准的英文搜索: 1.下载Lucene 下载Lucene,到Lucene的官 ...

  10. ADO.Net两种访问数据库模式

    在连接模式下的数据库访问通常包括以下几个步骤: 1.通过数据库连接类(DbConnection)链接类指定到数据库服务器的数据库 2.通过数据库命令类(DbCommand)在数据库上执行SQL命令,可 ...