Getting started with jeewx-api.jar, servletapi. jar
Jeewx-api.jar getting started
Attachment:
Http://download.csdn.net/detail/zxl78585/8549027
1. Apply for a test account on the official website.
Address: http://mp.weixin.qq.com/debug/cgi-bin/sandboxinfo? Action = showinfo & amp; t = sandbox/index
Click log on and use your own scan.
2. Configure Tomcat in Eclipse
In window-& gt; preferences-& gt; server-& gt; runtime environments
2. Open Eclipse and create a web project
In eclipse, the default jdk is 1.5. Here, we need to change jdk to 1.7. : Right-click the project, select build path, select configure build path, select jdk1.5 in libraries, click edit, and modify it to 1.7.
3. Import the jar package
Jeecg-the api package depends on the other nine packages and is imported together
Select these packages, right-click build path, and click add to build path. In this way, all the jar files are added to the project.
4. Create a form page
Create an index. jsp page in the WebContent directory
Html code
- <Body>
- <Form action = "ApiDemo" method = "post">
- <Table>
- <Tr>
- <Td> appId </td>
- <Td> <input type = "text" id = "appId" name = "appId"/> </td>
- </Tr>
- <Tr>
- <Td> appSecret </td>
- <Td> <input type = "text" id = "appSecret" name = "appSecret"/> </td>
- </Tr>
- <Tr>
- <Td colspan = "2" align = "center"> <input type = "submit" value = "submit"/> </td>
- </Tr>
- </Table>
- </Form>
- <%
- /* Obtain the content uploaded from the background */
- String data = (String) session. getAttribute ("data ");
- %>
- <% = Data %>
- </Body>
5. Create a servlet
An error is reported when a new servlet is created. This error is generally caused by the lack of jar. At this time, we do not need to import the tomcat package from external sources. In the build path, add the tomcat package to add the method.
Right-click a project
Build path-& gt; configure build path-& gt; libraries-& gt; add library-& gt; server runtime
6. In the doPost method, write:
Java code
- // Set the encoding format
- Request. setCharacterEncoding ("UTF-8 ");
- // Obtain parameters from the page
- String appid = request. getParameter ("appId ");
- String appsecret = request. getParameter ("appSecret ");
- // Determine whether appid and appsecret are empty
- If (StringUtils. isNotBlank (appid) & StringUtils. isNotBlank (appsecret )){
- // Token
- String token = null;
- // Obtain the token
- Try {
- // Call the method in jeewx-api to obtain the token
- Token = Your tokenapi. getAccessToken (appid, appsecret );
- } Catch (WexinReqException e ){
- }
- List <Wxuser> users = null;
- // Get all users through token
- Try {
- Users = login userapi. getAllWxuser (token, null );
- } Catch (WexinReqException e ){
- }
- StringBuffer result = new StringBuffer ();
- // Concatenate the result into a string
- For (Wxuser wxuser: users ){
- Result. append ("Country:" + wxuser. getCountry () + "City:" + wxuser. getCity () + "Gender:" + wxuser. getSex () + "<br/> ");
- }
- // Put the result into the session
- Request. getSession (). setAttribute ("data", result. toString ());
- // Jump to the previous page
- Response. sendRedirect ("index. jsp ");
- }
7. In the web. xml file
Xml Code
- <? Xml version = "1.0" encoding = "UTF-8"?>
- <Web-app xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns = "http://java.sun.com/xml/ns/javaee" xsi: schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id = "WebApp_ID" version = "2.5">
- <Display-name> apiDemo </display-name>
- <Welcome-file-list>
- <Welcome-file> index.html </welcome-file>
- <Welcome-file> index.htm </welcome-file>
- <Welcome-file> index. jsp </welcome-file>
- <Welcome-file> default.html </welcome-file>
- <Welcome-file> default.htm </welcome-file>
- <Welcome-file> default. jsp </welcome-file>
- </Welcome-file-list>
- <Servlet>
- <Description> </description>
- <Display-name> ApiDemo </display-name>
- <Servlet-name> ApiDemo </servlet-name>
- <Servlet-class> apiDemo. userServiceTest. ApiDemo </servlet-class>
- </Servlet>
- <Servlet-mapping>
- <Servlet-name> ApiDemo </servlet-name>
- <Url-pattern>/ApiDemo </url-pattern>
- </Servlet-mapping>
- </Web-app>
8. Run the tomcat server
Page:
Enter appId and appSecret in it and click Submit. The corresponding results will be returned below: