SPRINGMVC GET request to handle global session business

Source: Internet
Author: User

<a target=_blank href= "http://blog.csdn.net/is_zhoufeng/article/details/7559756" style= "Color:rgb (0, 0, 0); Text-decoration:none; font-family: ' Microsoft Yahei '; font-size:20px; line-height:30px; "Get Request object in >springmvc, Session object </a>
Requestattributes RA = requestcontextholder.getrequestattributes ();  HttpServletRequest request = ((servletrequestattributes) RA). Getrequest ();  Request.getsession (). SetAttribute (name, webuser);


(1) Web. xml

<session-config>
<session-timeout>30</session-timeout>
</session-config>

Note: 30 Finger minutes


You can also have all control inherit a Basecontrol class, and the Basecontrol class is defined as:

Package Com.jrgy.web.controller;import Java.io.serializable;import Java.util.arraylist;import java.util.HashMap; Import Java.util.list;import Java.util.map;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpsession;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.web.context.request.requestattributes;import Org.springframework.web.context.request.requestcontextholder;import Org.springframework.web.context.request.servletrequestattributes;import Com.jrgy.pojo.domain.FriendlyLink; Import Com.jrgy.pojo.domain.iteminfo;import Com.jrgy.pojo.domain.webuser;import Com.jrgy.util.cachemanager;import Com.jrgy.util.constants;import Com.jrgy.web.service.iassociationmanager;import Com.jrgy.web.service.iwebindexmanager;import Com.jrgy.web.service.iwebiteminfomanager;public class BaseController Implements Serializable{private static final long serialversionuid = 8695834026800952113l;/** *???? * */////HTTPSERVLETR Equest request = ((Servletrequestattributes) Requestcontextholder//.getrequestattributes ()). Getrequest (); CacheManager CacheManager = Cachemanager.getinstance (); @AutowiredIWebIndexManager webindexmanager;@ Autowirediwebiteminfomanager Webiteminfomanager; @AutowiredIAssociationManager webassociationmanager;/** * navigation bar * */ list<iteminfo> listiteminfo = new arraylist<iteminfo> ();/** * Links * */list<friendlylink> FRIENDLYLINKLS = new arraylist<friendlylink> (); List Itmecode = new ArrayList (); Map map = new HashMap ();/** *?????????? * @param Object???????? * @param String KEY? * @return Object * @throws Exception */public Object GetCache (Object object, String key) {Object cachedate = CacheManager. Get (key); if (cachedate! = null) {object = Cachedate;} return object;} /** * * @param key * @param object * @return void * @throws Exception */public void Addcache (String key, Object object) { Cachemanager.delcachebykey (key); Cachemanager.add (Key, object);} public void Addsessionwebuser (String name,webuser WebUser) {Requestattributes RA = requestcontextholder.getrequestattributes ();  HttpServletRequest request = ((servletrequestattributes) RA). Getrequest (); Request.getsession (). SetAttribute (name, webuser);} Public Object Getwebuserattribute (String name) {//TODO auto-generated method stubrequestattributes ra = Requestcontextho  Lder.getrequestattributes ();  HttpServletRequest request = ((servletrequestattributes) RA). Getrequest (); Object obj=null;if (!name.trim (). Equals ("")) {obj= request.getsession (). getattribute (name);} return obj;}}

This makes it easy for each control to get the objects in the servlet API.


Login save User to session

@RequestMapping ("/islogin.do") public void IsLogin (HttpServletRequest request, httpservletresponse response) {String SessionId = Request.getsession (). GetId (); Response.setcontenttype ("Text/html;charset=utf-8"); String userName = Request.getparameter ("UserName"); String PassWord = Request.getparameter ("UserPassword"); WebUser webuser=new WebUser (); String MSG = null;try {if (! Strutil.checkstr (UserName)) {msg= "2";} else if (! Strutil.checkstr (PassWord)) {msg= "3";} Else{webuser=mainwebusermanager.userlogin (Username,password); if (webuser!=null) {if ( Constants.webUserStruts.equals (Webuser.getwebusertype ())) {MSG = "5";} else{msg = "1"; Addsessionwebuser (Constants.login_webuser,webuser); Log.debug (username+ "");}} else{msg = "4";}} PrintWriter out = Response.getwriter (); String outmsg = "{Msg:" +msg+ "}"; Out.print (outmsg); Out.flush (); Out.close ();} catch (Exception e) {e.printstacktrace ();}}

The following code is required to use the session:

                String Name=constants.login_webuser; WebUser web= (WebUser) getwebuserattribute (name), if (web!=null) {currentuser=web.getwebusername ();}





SPRINGMVC GET request to handle global session business

Related Article

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.