Servlet中获取JSP内置对象
方便自己查询,嫌低级的勿喷。。。。
1.request
在servlet的doGet和doPost的参数中就有HttpServletRequest req参数,而JSP内置request对象就是HttpServletRequest接口的实例化对象,可以直接使用req。
2.response
在servlet的doGet和doPost的参数中就有HttpServletResponse resp参数,而JSP内置response对象就是HttpServletResponse接口的实例化对象,可以直接使用resp。
3.session
在servlet程序中要获取session对象,可以通过HttpServletRequest接口完成:
No | 方法 | 描述 |
1 | public HttpSession getSession() | 返回当前session |
2 | public HttpSession getSession(boolean create) | 返回当前session,如果没有则创建一个新的session对象返回 |
package org.lxh.servletdemo ; import java.io.* ; import javax.servlet.* ; import javax.servlet.http.* ; public class HttpSessionDemoServlet extends HttpServlet { public void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{ HttpSession ses = req.getSession() ; System.out.println("SESSION ID --> " + ses.getId()) ; ses.setAttribute("username","李兴华") ; // 设置session属性 System.out.println("username属性内容:" + ses.getAttribute("username")) ; } public void doPost(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{ this.doGet(req,resp) ; } }
4.application
application内置对象是ServletContext接口的实例,表示的是Servlet上下文,如果要在一个Servlet中使用此对象,直接通过GenericServlet类提供的方法即可:
No | 方法 | 描述 |
1 | public ServletContext getServletContext() | 取得ServletContext对象 |
package org.lxh.servletdemo ; import java.io.* ; import javax.servlet.* ; import javax.servlet.http.* ; public class ServletContextDemoServlet extends HttpServlet { public void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{ ServletContext app = super.getServletContext() ; System.out.println("真实路径:" + app.getRealPath("/")) ; } public void doPost(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{ this.doGet(req,resp) ; } }
5.config
config内置对象实际上是ServletConfig接口的实例,在Servlet程序中的init()方法里找到ServletConfig接口实例:
package org.lxh.servletdemo ; import java.io.* ; import javax.servlet.* ; import javax.servlet.http.* ; public class InitParamServlet extends HttpServlet { private String initParam = null ; // 用于保存初始化参数 public void init() throws ServletException{} public void init(ServletConfig config) throws ServletException{ this.initParam = config.getInitParameter("ref") ; // 接收的初始化参数名称暂时为ref } public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException,IOException{ System.out.println("初始化参数:" + this.initParam) ; } public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException,IOException{ this.doGet(req,resp) ; } }
6.out
out对象是javax.servlet.jsp.JspWriter类的实例化对象,此类继承于java.io.Writer类,然后在HttpServletResponse resp对象中可以获取java.io.PrintWriter类的实例化对象,而java.io.PrintWriter类同样继承于java.io.Writer类,所有out对象可以通过resp.getWriter()获取到:
package org.lxh.servletdemo ; import java.io.* ; import javax.servlet.* ; import javax.servlet.http.* ; public class InputServlet extends HttpServlet{ public void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{ String info = req.getParameter("info") ; // 假设参数名称为info PrintWriter out = resp.getWriter() ; out.println("<html>") ; out.println("<head><title>MLDNJAVA</title></head>") ; out.println("<body>") ; out.println("<h1>" + info + "</h1>") ; out.println("</body>") ; out.println("</html>") ; out.close() ; } public void doPost(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{ this.doGet(req,resp) ; } }