Access guide for beginners of JAVA and WeChat

Source: Internet
Author: User
I believe that many people are no longer familiar with development. I am also suffering from various problems from a development Cainiao, and then go to the search engine to search for various articles to read, but it is basically a piece of scattered information, there is no unified and systematic explanation of how applications are developed. Based on the actual development experience, the author summarizes and shares the experience with developers or developers who are working on the development. I believe that many people are no longer familiar with development. I am also suffering from various problems from a development Cainiao, and then go to the search engine to search for various articles to read, but it is basically a piece of scattered information, there is no unified and systematic explanation of how applications are developed. Based on the actual development experience, the author summarizes and shares the experience with developers or developers who are working on the development.

This article describes how to build a development platform. The ancients cloud: "The knife is not cut enough to work ". We also need to prepare some necessary conditions for application development.

Preparations

1. download and install Jdk1.7

2. download the Tomcat 7.0

3. download Myeclipse 6.5.

4. download the Json-lib third-party jar.

1. apply for a public IP address and publish your own application.

Example: 192.168.1.102

2. open myeclipse to create a project

As shown in:

JAVA implementation class: WeixinServlet

Package com. wx. servlet; import java. io. IOException; import java. io. inputStream; import java. io. outputStream; import java. security. messageDigest; import java. security. noSuchAlgorithmException; import java. util. arrays; import java. util. logging. logger; import javax. servlet. servletException; import javax. servlet. http. httpServlet; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; import net. sf. json. JSONObject; import net. sf. json. xml. XMLSerializer;/*** @ author haibing. xiao * @ since jdk1.6 * @ version 1.0 */public class WeixinServlet extends HttpServlet {private Logger log = Logger. getLogger (this. getClass (). getName (); private static final long serialVersionUID = 1L; private String Token; private String echostr; @ Overrideprotected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {connect (request, response) ;}@ Overrideprotected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {message (request, response );} /*** @ author haibing. xiao * @ return * @ exception * @ param **

Access connection validation

*/Private void connect (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {log.info ("RemoteAddr:" + request. getRemoteAddr (); log.info ("QueryString:" + request. getQueryString (); if (! Accessing (request, response) {log.info ("server access failed..."); return;} String echostr = getEchostr (); if (echostr! = Null &&! "". Equals (echostr) {log.info ("server access takes effect .......... "); response. getWriter (). print (echostr); // mutual authentication completed}/*** @ author haibing. xiao * Date 2013-05-29 * @ return boolean * @ exception ServletException, IOException * @ param **

Used to receive verification from the public platform

*/Private boolean accessing (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {String signature = request. getParameter ("signature"); String timestamp = request. getParameter ("timestamp"); String nonce = request. getParameter ("nonce"); String echostr = request. getParameter ("echostr"); if (isEmpty (signature) {return false;} if (isEmpty (timestamp) {return false;} if (isEmpty (nonce )) {return false;} if (isEmpty (echostr) {return false;} String [] ArrTmp = {Token, timestamp, nonce}; Arrays. sort (ArrTmp); StringBuffer sb = new StringBuffer (); for (int I = 0; I <ArrTmp. length; I ++) {sb. append (ArrTmp [I]);} String pwd = Encrypt (sb. toString (); log.info ("signature:" + signature + "timestamp:" + timestamp + "nonce:" + nonce + "pwd:" + pwd + "echostr: "+ echostr); if (trim (pwd ). equals (trim (signature) {this. echostr = echostr; return true;} else {return false;} private String Encrypt (String strSrc) {MessageDigest md = null; String strDes = null; byte [] bt = strSrc. getBytes (); try {md = MessageDigest. getInstance ("SHA-1"); md. update (bt); strDes = bytes2Hex (md. digest (); // to HexString} catch (NoSuchAlgorithmException e) {System. out. println ("Invalid algorithm. "); return null;} return strDes;} public String bytes2Hex (byte [] bts) {String des =" "; String tmp = null; for (int I = 0; I <bts. length; I ++) {tmp = (Integer. toHexString (bts [I] & 0xFF); if (tmp. length () = 1) {des + = "0";} des + = tmp;} return des;} public String getEchostr () {return echostr ;} /*** @ author haibing. xiao * @ return * @ exception ServletException, IOException * @ param **

XML Component Assembly

*/Private void message (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {InputStream is = request. getInputStream (); // get the length of the HTTP request stream int size = request. getContentLength (); // used to cache the data read each time. byte [] buffer = new byte [size]; // array byte [] xmldataByte = new byte [size]; int count = 0; int rbyte = 0; // read while (count <size) cyclically) {// The actual read length is stored in rbyte. rbyte = is. read (buffer); for (int I = 0; I Service forwarding component

**/Private void manageMessage (String requestStr, HttpServletRequest request, response) throws failed, IOException {String responseStr; try {XMLSerializer xmlSerializer = new XMLSerializer (); JSONObject jsonObject = (JSONObject) xmlSerializer. read (requestStr); String event = jsonObject. getString ("Event"); String msgtype = jsonObject. getString ("MsgType"); if ("CLICK ". equals (event) & "event ". equals (msgtype) {// menu click event String eventkey = jsonObject. getString ("EventKey"); if ("hytd_001 ". equals (eventkey) {// hytd_001 this is the sign value of the friend team button jsonObject. put ("Content", "Welcome to the click button of the friend team menu. ") ;}} responseStr = creatRevertText (jsonObject); // create XML log.info (" responseStr: "+ responseStr); OutputStream OS = response. getOutputStream (); OS. write (responseStr. getBytes ("UTF-8");} catch (Exception e) {e. printStackTrace () ;}} private String creatRevertText (JSONObject jsonObject) {StringBuffer revert = new StringBuffer (); revert. append (" "); Revert. append (" "+jsonObject.get("ToUserName")+" "); Revert. append (" "+jsonObject.get("FromUserName")+" "); Revert. append (" "+ JsonObject. get (" CreateTime ") +" "); Revert. append (" text "); Revert. append (" "+jsonObject.get("Content")+" "); Revert. append (" 0 "); Revert. append (" "); Return revert. toString () ;}@ Overridepublic void init () throws ServletException {Token = "test123" ;}private boolean isEmpty (String str) {return null == str | "". equals (str )? True: false;} private String trim (String str) {return null! = Str? Str. trim (): str ;}}

4. access http: // localhost: port number/context/wenxin

These are the details of the access guide for beginners of JAVA development. For more information, see other related articles on php Chinese network!

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.