servlet--get session,application built-in objects

Source: Internet
Author: User


In the previous blog, using the servlet to obtain the Request,response,config object, in fact, through the servlet program can also get session,application and other built-in objects.


1, obtained httpsession through HttpServletRequest

<span style= "White-space:pre" ></span>//returns the current Sessionpublic abstract HttpSession getsession ();// Returns the current session, and if not, creates a new session object that returns public abstract HttpSession getsession (Boolean Paramboolean);

package linkin;import java.io.ioexception;import javax.servlet.ServletException; Import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Javax.servlet.http.httpsession;public class LinkinServlet extends Httpservlet{private static final Long serialversionuid = 1L; @Overrideprotected void Doget (HttpServletRequest req, HttpServletResponse resp) throws Servletexception, ioexception{httpsession session = Req.getsession (); System.out.println (Session.getid ());//4214f4ce399e02730c3219cf0bd83623session.setattribute ("Age", 25); System.out.println (Session.getattribute ("Age"));//25} @Overrideprotected void DoPost (HttpServletRequest req, HttpServletResponse resp) throws Servletexception, Ioexception{this.doget (req, resp);}} 
<?xml version= "1.0" encoding= "UTF-8"? ><web-app version= "2.5" xmlns= "Http://java.sun.com/xml/ns/javaee" Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http ://java.sun.com/xml/ns/javaee/web-app_2_5.xsd "><servlet><servlet-name>linkinservlet</ Servlet-name><servlet-class>linkin. Linkinservlet</servlet-class><init-param><param-name>name</param-name><param-value >LinkinPark...</param-value></init-param></servlet><servlet-mapping>< servlet-name>linkinservlet</servlet-name><url-pattern>/linkinservlet</url-pattern></ Servlet-mapping></web-app>


2, using Genericservlet to obtain ServletContext instance

Public ServletContext Getservletcontext ()  {    return Getservletconfig (). Getservletcontext ();  

Package Linkin;import Java.io.ioexception;import Javax.servlet.servletcontext;import Javax.servlet.servletexception;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;public class LinkinServlet Extends httpservlet{private static final long serialversionuid = 1L; @Overrideprotected void Doget (HttpServletRequest req , HttpServletResponse resp) throws Servletexception, Ioexception{servletcontext context = Getservletcontext ();//Get "/", That is, the actual physical location of the project on the server System.out.println (Context.getrealpath ("/"));//e:\workserver\apache-tomcat-6.0.37\webapps\ Linkin} @Overrideprotected void DoPost (HttpServletRequest req, HttpServletResponse resp) throws Servletexception, Ioexception{this.doget (req, resp);}}
<?xml version= "1.0" encoding= "UTF-8"? ><web-app version= "2.5" xmlns= "Http://java.sun.com/xml/ns/javaee" Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http ://java.sun.com/xml/ns/javaee/web-app_2_5.xsd "><servlet><servlet-name>linkinservlet</ Servlet-name><servlet-class>linkin. Linkinservlet</servlet-class><init-param><param-name>name</param-name><param-value >LinkinPark...</param-value></init-param></servlet><servlet-mapping>< servlet-name>linkinservlet</servlet-name><url-pattern>/linkinservlet</url-pattern></ Servlet-mapping></web-app>


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

servlet--get session,application built-in objects

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.