Java Cache (2, threadlocal<t>)

Source: Internet
Author: User

Program Diagram


Description

The Threadcache class is the encapsulation of the Threadlocal<t> class, Usermanagerservlet the request method to create a user object, then puts the object in Threadlocal, and then Obtains the object from Usermanagerdao and returns it to the servlet


program

Threadcache

Package Com.tgb.util;import Com.tgb.entity.user;public class Threadcache {private static threadlocal<user> threadlocal;static {threadlocal= new threadlocal<user> ();} public static User GetUser () {return threadlocal.get ();} public static void SetUser (user user) {threadlocal.set (user);}}

Servlet

Package Com.tgb.servlet;import Java.io.ioexception;import Javax.servlet.servletexception;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import Com.tgb.entity.user;import Com.tgb.service.UserManagerService; Import Com.tgb.util.threadcache;public class Usermanagerservlet extends HttpServlet {public void doget ( HttpServletRequest request, HttpServletResponse response) throws Servletexception, IOException {doPost (Request, Response);} public void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {int id = integer.parseint (request.getparameter ("id")); String name = Request.getparameter ("name"); String Password = request.getparameter ("password"); User user = new user (); User.setid (ID); user.setname (name); User.setpassword (password); Threadcache.setuser (user); Usermanagerservice usermanagerservice = new Usermanagerservice (); User GetUser = Usermanagerservice.getuser (); System.oUt.println (GetUser); Response.sendredirect (Request.getcontextpath () + "/index.jsp");}}

Dao

Package Com.tgb.dao;import Com.tgb.entity.user;import Com.tgb.util.threadcache;public class UserDao {public User GetUser () {return Threadcache.getuser ();}}

Enhanced DemoOverviewfor the above demo, very simple, mainly to let everyone understand the function of threadlocal, its real purpose in the project is to store connection, from the service management services to achieve the purpose, the following will give you a better demo. Class Diagram Overview
DescriptionConnectionContext is a pool of database connection objects, using C3p0;Connectionpoolmanager is the encapsulation of threadlocal<t>;TransactionManager is the encapsulation of transactions;Filter Filters The request, opens the transaction, invokes the servlet, and finally, the filter commits the transaction;Basedao uses generics and reflection to implement simple ORM ideas so that the program operates on objects;
SummaryDemo2 inside the knowledge point is a lot of, is a more comprehensive example, page encapsulation, JSON encapsulation, THREADLOCAL,C3P0, transaction applications, reflection and generics, custom annotations (demo, not applied to the project), interested can download the source to see, There is not much to be introduced here.

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

Java Cache (2, threadlocal<t>)

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.