TA的每日心情 | 衰 2021-2-2 11:21 |
---|
签到天数: 36 天 [LV.5]常住居民I
|
java实现微信公众平台开发项目源码
& v) g- J. ]# E9 [7 J# ]1 K本文向大家介绍使用Java来实现微信公共平台功能,实现根据回复的内容返回对应的消息。供大家学习使用。
! q: P- a; I% a6 F6 {微信服务端收发消息接口:WechatServlet.java
* k4 ^: T- D' H7 C8 l. K1 W- package demo.servlet;
( g; f: s w Y! h: D - 1 W& S* ]) j4 q+ { w0 O; ^) k4 e
- import java.io.BufferedReader;# u& `1 C! u" c( a
- import java.io.IOException;/ f. L: u p' S
- import java.io.InputStream;) p. r/ q# E( W! k; N3 W
- import java.io.InputStreamReader;
+ J/ y. S7 O) ] z2 y - import java.util.Arrays;/ ]% D/ n9 x9 m& r" {6 ^
- 0 G* R' V6 S B- X) Z6 k* u
- import javax.servlet.ServletException;% e5 O8 B. Q2 ]9 R+ g
- import javax.servlet.http.HttpServlet;3 O/ R8 Z! A% N! e
- import javax.servlet.http.HttpServletRequest;; I: o* L |5 |) \
- import javax.servlet.http.HttpServletResponse;4 n0 E+ c+ H4 e$ T+ F
' v0 F/ H& h4 c3 e- import demo.process.WechatProcess;
) w! S4 x& W8 q0 W - import demo.util.SHA1;
- Z2 V: y5 ~+ A- C& b) x7 Q - /**: \" z) Z D+ Q& e+ P& V% Z
- * 微信服务端收发消息接口
# b1 _( k9 E" a4 i0 n! c- l" q - *
! _* i5 J" _7 {+ G - * @author 科帮网
; q# o# J8 a3 z5 C) V - *
( C6 N6 q7 H7 Q, n - */
4 b2 D1 ^& B# u% }0 r, r! V - @SuppressWarnings("serial")
% l a5 A+ g7 S4 ]3 a9 K - public class WechatServlet extends HttpServlet {8 g# ~3 K7 @7 \- }; I: d9 Q
9 S* X6 _" v7 r, {: d- // 自定义 token
6 f: t- I i+ u' r9 k' ` G2 y4 T) t- _ - private String TOKEN = "52itstyle";
$ M- e, r* T& X3 O7 K - public void doGet(HttpServletRequest request, HttpServletResponse response)8 N k9 H6 Z: T7 o, y' i$ p* N/ f
- throws ServletException, IOException {
4 }5 m0 C' S( f( i5 E/ B/ `7 a - String signature = request.getParameter("signature"); // 随机字符串
7 c6 {# R3 G: C% w; }$ U* q$ p8 r - String echostr = request.getParameter("echostr"); // 时间戳
' }3 C# d7 c4 k2 @! Y6 W/ g% U - String timestamp = request.getParameter("timestamp"); // 随机数
6 b! M4 k8 C4 y4 j2 T6 a - String nonce = request.getParameter("nonce");
, J+ N" F) g# R# q9 G a -
2 E- E$ S: u P+ E# d - String[] str = { TOKEN, timestamp, nonce };) \7 D+ [$ [0 O& ?7 @4 l8 c G
- Arrays.sort(str); // 字典序排序. U& I* I3 A* ]
- String bigStr = str[0] + str[1] + str[2]; // SHA1加密
7 Q" O# c! b: j! F - String digest = new SHA1().getDigestOfString(bigStr.getBytes()).toLowerCase(); 9 c; ?7 P" i- L
- // 确认请求来至微信
& F0 d: T" n& W1 x! M - if (digest.equals(signature)) {7 b+ f2 f/ k" H
- request.setCharacterEncoding("UTF-8");
- O5 @( P$ N4 z/ B - response.setCharacterEncoding("UTF-8");
# T- X7 n7 G8 \1 L5 V" A/ j -
; Y- f* h' j# {6 b - /** 读取接收到的xml消息 */9 A$ l7 C/ q! j. [0 V$ F
- StringBuffer sb = new StringBuffer();
0 M l7 ~- i5 }# S - InputStream is = request.getInputStream();
: [' W9 l; V$ t. W9 F$ X7 ] - InputStreamReader isr = new InputStreamReader(is, "UTF-8");
# N/ ] ]8 F" L! f& d X) U - BufferedReader br = new BufferedReader(isr);
1 C7 t, w! y3 _& Q R/ O7 d; n9 u - String s = "";
1 o8 j; M9 b1 J$ N @5 _ - while ((s = br.readLine()) != null) {* s& O: b# l3 O. D/ }
- sb.append(s);
4 D& `- M+ {+ i* x0 M# B" I - }
# v: l& g. C- m - String xml = sb.toString(); //次即为接收到微信端发送过来的xml数据+ ^+ I7 v2 z3 r0 G
- 7 c1 m# c4 K( f. |% P
- String result = ""; H$ p. d; B2 {. D- }
- /** 判断是否是微信接入激活验证,只有首次接入验证时才会收到echostr参数,此时需要把它直接返回 */
9 n5 `# c) ?, ^4 n9 z! ?5 Z5 j - if (echostr != null && echostr.length() > 1) {: ^7 v6 t6 S) x4 z) W7 F
- result = echostr;
" k8 B3 [* ]8 R8 g; w - } else {
% |% v# `, h% a& w/ r0 H3 t - //正常的微信处理流程
/ D/ ]' I/ m/ `4 z7 c- R4 O - result = new WechatProcess().processWechatMag(xml);
- u- [1 ?2 q, x. `5 v( O2 ^ - }
$ C2 [. `& Q$ P* A1 N - System.out.println("说的什么"+result);
! g B7 A" C9 H) H/ @ s/ Q - response.getWriter().print(result);5 O! N7 q4 z7 R) U& {; Q& w
- }
6 Y% b: ^$ d- r3 ~7 f4 |- I# P - }
8 Q# G6 ` s9 ^/ a1 H! T - 3 E3 D5 h) @& h5 Q3 I
- /**# u8 Q0 ^9 |4 _
- * The doPost method of the servlet. <br>
t1 S# Q. }/ J0 @" J+ m - *
* r! S' {4 e. H4 Z* R& U7 s2 ] - * This method is called when a form has its tag value method equals to
8 @$ W5 I; C e1 w$ a+ }6 C - * post.5 w* {) J' U& n
- * 1 P Q, u3 [7 U) m# V9 \; A
- * @param request
! G1 e) x3 q& ~ - * the request send by the client to the server3 ?/ M* D* S# X6 B/ S% b
- * @param response
, c; \+ X. Z0 h# N - * the response send by the server to the client
5 k4 Q& H- R6 d2 Y - * @throws ServletException
$ a6 `: m/ `, z( z6 b - * if an error occurred
* z% P5 d/ F! o$ ~4 B' R; e - * @throws IOException. q, j8 r9 k/ B3 P9 I+ @
- * if an error occurred( s% n' s8 k. e4 C S1 t+ I$ }
- */+ [: ?- y' Y: X$ ~/ b! s |. O
- public void doPost(HttpServletRequest request, HttpServletResponse response)* I1 t5 d; o: D
- throws ServletException, IOException {, k$ W; U! ^9 x: h; Q7 p- I
- doGet(request, response);6 e( I4 A6 o. b0 f( U
- }+ G( I* ~( I% j; _
- ' P/ L$ `& d; B2 f( f) @( d
- }
7 Z. }; s" e! c; O: y, h. m/ {! _/ t$ k
复制代码 接收到的微信xml实体类:ReceiveXmlEntity.java
& ^ T7 O7 D7 g( H+ M' w& M) x4 c- package demo.entity;4 z+ E% @1 Q- A4 O3 y$ k. Z
- /**
; r1 F5 z( l% p* C3 K - * 接收到的微信xml实体类! K+ T% w8 @5 K
- * @author 科帮网
+ M3 J, l y$ S4 F - *
/ ?5 h' V M8 F - */; ~2 Y5 H4 _0 H5 D( T9 s
- public class ReceiveXmlEntity {
+ I% Q1 y0 L) E) T - private String ToUserName="";
+ g3 y; g1 `1 D5 S - private String FromUserName="";
; | S" Q7 K8 b# L - private String CreateTime="";
) D9 l) l# k9 z1 ~! w - private String MsgType="";
( P4 D7 ]; U7 A8 k0 C9 ^9 ` - private String MsgId="";/ o g# H' ?1 S* Q i: \. u7 o' o, h( A
- private String Event="";3 @( O8 U% E; v0 |
- private String EventKey="";
' E2 J7 l9 r9 c5 k2 d9 E - private String Ticket="";
' N2 J0 E3 G) s/ l - private String Latitude="";1 P. ?2 S: m& [) r% x* t
- private String Longitude="";, N" w! H7 K7 `6 W
- private String Precision=""; {" Y% e( F8 x3 l
- private String PicUrl="";2 d* N0 Q8 ^& t/ S/ _
- private String MediaId="";; [8 y3 _, L: a ]1 ?, y- t
- private String Title="";4 Z5 |7 B2 {# I. G2 i/ D# ?
- private String Description="";
4 [8 V+ _) w, B- d - private String Url="";, {. ^! P& U# `- v8 _2 ]) C+ E! |
- private String Location_X="";/ _8 R$ m5 n/ ^3 R3 L( P
- private String Location_Y="";
8 }) ` L8 F' t5 l - private String Scale="";/ E+ V" Z5 g" X6 e: S
- private String Label="";0 [9 l A* `7 x9 b) z8 q/ ]
- private String Content="";
3 w! [* a( O% F5 e/ u+ Y h - private String Format="";. I! k1 ?* h; g a7 j
- private String Recognition="";
8 \' ?( T7 d+ F. y& d" T -
* v( U" ^8 t" Y9 g+ w& K# c - public String getRecognition() {$ O% J" T' o/ w* A9 x
- return Recognition;6 d; j7 [# k* r. S
- } G# n) r' {' q% E$ C( `
- public void setRecognition(String recognition) {' `- _2 i5 I, w' `
- Recognition = recognition;
, N- T/ ~5 _6 I- _" v4 N7 K - }
C' X6 g. j2 F" B, B - public String getFormat() {% t% u, }0 z/ W/ p! A9 I: L" ?
- return Format;
2 Z2 z1 c$ I3 [1 H1 q - }
: G! @: A H. k# a" z: j% F - public void setFormat(String format) {
$ b3 Y+ ~" U! t+ o% j; n) ~ - Format = format;( J" K5 V2 |$ a6 F3 a% K3 b
- }0 k- Q0 g: y9 i# ]1 g
- public String getContent() {: y; i% d; e/ q d. c
- return Content;
* D n5 }& C$ r - }
+ ?. y! Y1 b' V5 ?+ Q, N' i- d& T" K - public void setContent(String content) {
% H' A; G! K7 C! Q2 e - Content = content;
8 S2 O3 c j9 [3 | - }
% W9 @* Q1 A! V' B9 E - public String getLocation_X() {7 R( S6 F M) w% @! W {
- return Location_X;
# r+ P, ?6 \2 k% H8 r( \# [2 n P - }
4 \# s* P$ W5 a( h0 j6 u# Q- L' P - public void setLocation_X(String locationX) {
8 F+ D, Z" t/ Y6 Z1 P/ g v! X - Location_X = locationX;6 q' X9 p+ j4 [" j/ W7 R. [: y4 H
- }4 ~9 q+ [# U7 v5 i6 v5 X4 Q
- public String getLocation_Y() {" C: q# ^! q( |' ^& {* {5 G" O
- return Location_Y;
) i! [8 r, `1 Y4 ?: N/ G4 c) _ - }
3 W) u% i& ^/ G; m- E9 M - public void setLocation_Y(String locationY) {) C, A# u- f9 t
- Location_Y = locationY;6 I" \$ m1 @& n1 m A5 t1 `
- }+ }* q- d9 J' @: E$ ^% T; D3 o" [
- public String getScale() {
0 I- Z2 \ `1 j$ K/ v - return Scale;
9 U2 Q. O* @( c4 d+ F( _ - }+ p9 b7 e: B8 h0 _5 H. q) ]
- public void setScale(String scale) {
- \) i- n' f U" @ - Scale = scale;# X9 l7 P' \8 t9 m7 D2 u! s" r9 O
- }5 Q: A6 W/ K z- [1 ~9 X
- public String getLabel() {
5 n/ P; ]- S4 A$ n( T7 W( F1 ^. W - return Label;- z/ ?3 H! _# C. u6 Q+ r
- }9 J8 j- Z: R* @% d
- public void setLabel(String label) {, T# `! r/ G* Z: Y2 v+ R
- Label = label;7 q) O8 R' s8 Q2 I3 t2 L% M
- }7 n6 y7 `4 l4 @2 ]
- public String getTitle() {6 `2 B2 w/ L& b/ ]" k2 Q9 P5 e
- return Title;
4 f$ X) w1 x! C0 c9 \: c% l - }% L" P v0 x* _; {8 }
- public void setTitle(String title) {
, x: Z5 j- G! \/ ?+ ^) l - Title = title;5 k6 F! x! n/ g4 W2 R) P
- }
7 C( R/ w( Q3 @( q0 {. S - public String getDescription() {
: z w" ~8 u+ T. _* k3 q7 j - return Description;8 F7 l5 d$ [9 v, F! U
- }
' y8 ^5 v- K' C - public void setDescription(String description) {/ z; T: _1 {- }6 Y9 ~: O" E1 S
- Description = description;! U) h: g: ?0 I0 |0 N7 k0 X
- }4 i3 {4 F# A7 l
- public String getUrl() {
/ h. |4 |8 p7 V* Y+ b' A. @ - return Url;, D: J1 v; @- ]6 _) x2 v
- }5 }: J$ S% o: Y& B
- public void setUrl(String url) {& v! i' s: N/ H! ?- |! J: G
- Url = url;5 f# h G A6 r! H7 V7 K0 w% Z( w
- }* y1 Q, M- k7 D& }) R- j$ B* R8 _
- public String getPicUrl() {8 P1 ]2 F" X! L, q- q1 z
- return PicUrl;# k; f7 o- E& A6 E3 G) ^6 L
- }7 R. W& M8 b7 Y s9 w
- public void setPicUrl(String picUrl) {4 ]" V; p2 M" ]3 `- V$ Z, B
- PicUrl = picUrl;+ W# H( M1 l T) ]
- }
1 ^+ F: x# B* H o2 v - public String getMediaId() {
- F' P% N. @ `; Q( k - return MediaId;
3 t, M1 n i0 l; y6 B+ R7 @ Z u - }& ^( {$ j- A" Z
- public void setMediaId(String mediaId) {
3 {( I1 W$ S6 G" z/ Q+ e% \, n2 O - MediaId = mediaId;
, W( e3 w2 P, M$ X - }" Z/ d' A- c( i4 W X) L9 b6 V
- public String getEventKey() {
! h- m! `) _6 J5 E, `! m - return EventKey;
# {$ I7 {& A$ U1 t! }8 j# x9 c" O - }6 j* \* ~, R! Y7 m. z
- public void setEventKey(String eventKey) {9 ~0 B3 X' O- `, `* j) i
- EventKey = eventKey; R U8 G: u$ e3 i# s* I1 b9 c
- }) Z" ?' z& T% V! D
- public String getTicket() { R# N6 h* j' g0 _
- return Ticket;+ m2 R" [' J8 p: o/ d7 M
- }; [: |7 F/ G7 ^5 ?
- public void setTicket(String ticket) {- Y2 M8 h- | f
- Ticket = ticket;
4 d' _! n, Y% b/ b2 ?% d - }3 _/ W# }+ j( K7 W" Y: D# g/ `* P
- public String getLatitude() {$ B Q6 R+ E4 z8 l3 E" G! G5 j
- return Latitude;" d' O9 X7 m2 p6 b% t1 L) @
- }$ `/ E" I8 V8 o3 o- u- H: Y
- public void setLatitude(String latitude) {
+ z5 r& B/ \3 L# l9 p - Latitude = latitude;3 V6 D' W6 z( P& H% w" m
- }% D/ I: V) f3 @- G7 S; ]- g) a' s( `
- public String getLongitude() {$ _3 D3 Q2 e8 w+ _3 }# i
- return Longitude;0 H- @% I! a7 @
- }/ V6 c5 ^! t0 M+ S- ]( c0 V/ X
- public void setLongitude(String longitude) {
/ O4 i8 y3 j/ @ F; a! K - Longitude = longitude;% t) Q2 N! B- L5 q |8 p/ a9 p
- }, [, O9 R' {- ]2 l L. Z# [
- public String getPrecision() {
8 g& O5 o. \1 E" F - return Precision;
) s+ j) n# O7 d' J& _8 h. x5 u - }
2 T2 t1 u; x o: n) [( B7 P - public void setPrecision(String precision) {3 F2 E8 n4 o; }) Z8 ?( O4 g$ P* E
- Precision = precision;. b1 q" w8 c6 f8 f4 @
- }
7 V7 m8 @' z( x+ y - public String getEvent() {
4 ]1 R5 M- t' C \0 N6 ~ - return Event;/ M0 z& ^4 ]5 N* z# k9 w
- }3 d: B' u9 _2 m P" C# f5 d
- public void setEvent(String event) {8 a, i5 [" B, _( }! _( x
- Event = event;
3 V- r C* q- a+ O - }
8 b$ L$ K, [' S4 X+ _) O - public String getMsgId() {
* }' J, ~/ ~/ V$ E/ ^% ?( A. ~: P - return MsgId;
& {7 C6 Y. U# E0 R. t5 ~ - }
0 V. ]. S0 ]/ K - public void setMsgId(String msgId) {
* j# X p p/ t. f- U - MsgId = msgId;8 C8 Q! }! b8 M$ {8 p6 G2 t. r
- }
& D4 {( u6 q1 q2 o - public String getToUserName() {/ m0 `5 q5 b0 V6 T
- return ToUserName;5 d4 b1 @; K; z6 S
- }
3 I G' `/ `, ]. J5 t, \- A7 u - public void setToUserName(String toUserName) {
1 t( z2 y3 D! L4 } - ToUserName = toUserName;9 M+ C* U+ Z4 r$ P
- }
2 H! K! ^9 i5 W# D8 @ - public String getFromUserName() {0 C5 c7 f- ~* N8 |1 i5 B
- return FromUserName;4 X5 _4 m. h) ?% D1 D; Q6 a) \* }4 M7 a
- }
2 ^8 P4 {! Q5 O' n! V - public void setFromUserName(String fromUserName) {/ [) W9 P; |1 e5 x% O3 e
- FromUserName = fromUserName;
' A" W! T) }! R- o8 ^# n p - }
) B1 q' A# K( N7 l' |4 O" E' |4 h - public String getCreateTime() {
1 W' j4 J# I: P2 _1 k4 f8 _! \ - return CreateTime;* \3 ^; C U6 {8 O7 m5 P
- }
( y% a' G! V9 n( i - public void setCreateTime(String createTime) {/ @$ W7 d7 x, Z9 Q! Y
- CreateTime = createTime; ^( n( x6 J7 }4 [1 a
- }
( ?6 N, s4 y6 d6 i$ p' | - public String getMsgType() { f/ z8 p |* C( E5 ]6 @) ~: ~
- return MsgType;
& s7 _$ I& ]" z g - }
4 h* l/ h( O( a% l - public void setMsgType(String msgType) {3 w+ @4 X/ {. @" F& b2 `7 ^
- MsgType = msgType;
4 n: J* s; p+ t5 _, O - }' j& m! c1 \7 b- t' [
- }, n- E- \: ~+ I" V" z1 u
复制代码 调用图灵机器人api接口,获取智能回复内容 TulingApiProcess.java
7 R4 c! T1 S' F7 Y7 i( `& ]+ I
q( y6 q* v5 W6 X8 y' @' \/ g$ ^- package demo.process;
: a& q: D" V+ P* ^2 \( p - 6 s6 I' _; h4 B- t/ K: k
- import java.io.IOException;
' T: f: I8 O2 G2 h5 P - import java.io.UnsupportedEncodingException;+ T7 M6 u' R& f9 }* S" r: Q
- import java.net.URLEncoder;( h8 a( ~' G3 |+ m+ o5 [
- , i, c7 h7 y2 f( x1 L; t
- import org.apache.http.HttpResponse;. T& m: u' O+ _; S
- import org.apache.http.client.ClientProtocolException;
4 @7 A9 C% `! Q# h6 F - import org.apache.http.client.methods.HttpGet;
. {( @2 R! u, O. T, D6 D - import org.apache.http.impl.client.HttpClients;
/ }) l& F- Z$ m, p6 C2 n - import org.apache.http.util.EntityUtils;) Y* c, v7 A4 F q' t
- import org.json.JSONException;- j+ f+ G( |2 [8 }
- import org.json.JSONObject;- Q( w7 F1 w( x' ^1 K& A
$ E$ i Q* H8 f$ \1 y6 `8 |- /**
x; e4 l2 k( [+ d$ f- x - * 调用图灵机器人api接口,获取智能回复内容
" b- I0 L0 u9 A/ T+ Q - * @author 科帮网
d5 j( ?7 Z! d9 m - *
# E% Z# T9 ^. F) I1 X) G - */
; P) m+ m3 u; L) p$ } - public class TulingApiProcess {* y1 G C$ Q5 e1 W2 H2 _( @ l
- /**
4 F" d# d1 J- ^4 ?9 R - * 调用图灵机器人api接口,获取智能回复内容,解析获取自己所需结果
" s* i! `; i( }8 _0 }% n - * @param content# X; O5 c4 _, @, V, k
- * @return: e" t& G' u9 E0 s& N- v2 J
- */) f+ \$ j; e' c; Q$ m j7 Y
- public String getTulingResult(String content){7 y( z4 {2 D3 f8 P) W6 f( M
- /** 此处为图灵api接口,参数key需要自己去注册申请 */
2 h7 `, w$ S/ D1 u& u - String apiUrl = "http://www.tuling123.com/openapi/api?key=2a31b2f601f74b54ea13db1c82fe5d71&info=";5 ] c0 l( |4 g
- String param = "";
' S3 T5 @9 U; ?# N2 @; H - try {
/ Z8 J" Y3 K, h+ l/ W, B' e& w9 E - param = apiUrl+URLEncoder.encode(content,"utf-8");2 t1 a- l& x X% b5 t7 e
- } catch (UnsupportedEncodingException e1) {- V' J! D" o( ]! {
- e1.printStackTrace();4 x+ B1 K& @ W0 C1 i
- } //将参数转为url编码& X& H) B4 j# M3 Y1 A( C% o* T" r
- /** 发送httpget请求 */! U4 w0 f3 N7 ~
- HttpGet request = new HttpGet(param);
: Q) b! U! [; L7 h - String result = "";& y4 `8 O5 c0 F, b
- try {
( R7 c& `0 Q6 q" G0 W - HttpResponse response = HttpClients.createDefault().execute(request);. K5 `( F/ O$ f" v1 G
- if(response.getStatusLine().getStatusCode()==200){
. o8 x0 u: M' o8 G4 O4 l - result = EntityUtils.toString(response.getEntity());
9 a0 |8 g5 H# z7 P1 P! L1 k) o% ^8 p - } t c! s& c+ S; `5 o
- } catch (ClientProtocolException e) {
- \, i# \' x# ^ - e.printStackTrace();4 L: z: \' Q1 C
- } catch (IOException e) {
; I4 L+ @" Y4 y& D' A6 h# i - e.printStackTrace();
5 b$ E8 u* }& q2 E8 ^+ ^& X9 r - }$ I' y. ? Q1 L+ f% ]7 t
- /** 请求失败处理 */0 V6 s" i5 X/ \. A0 U
- if(null==result){2 m& M9 |* V0 E5 ?6 f: O
- return "对不起,你说的话真是太高深了……";$ h K3 I$ H2 A/ @4 u4 x+ U& r
- }
8 w7 m- r/ A& j0 r: `; |+ b t - ; a# K9 k! W4 v: ^+ _( R
- try {
P" C- X3 Y4 f% Q) y) \ - JSONObject json = new JSONObject(result);
^- P: i+ w5 I4 L - //以code=100000为例,参考图灵机器人api文档
. y$ n4 F, j) _1 Q5 t - if(100000==json.getInt("code")){
/ d) x8 R5 N% b: Y+ Y - result = json.getString("text");( u: V* ^8 D+ p$ U" d. d* C
- }5 B, F% Z2 j( Q5 k7 C8 x/ w
- } catch (JSONException e) {( _; {$ X1 X/ p7 h e
- e.printStackTrace();
" o) k2 c3 b+ {, G4 q% [: | - }5 f. T, k' D Q
- return result;
6 J4 t* a/ g0 g6 Z- {3 v - }
2 R8 a3 l3 K3 {. m6 j) V, U. H - }
/ U/ r1 m; u+ J: V) Y0 p0 Q4 f
复制代码 & K, l( k$ v( }
web.xml 配置:0 ^( M4 G! V$ E% E4 d+ ]
- <?xml version="1.0" encoding="UTF-8"?>
6 z2 a: d& W. S; | - <web-app version="2.5"
, }8 a" w7 S- `. [& C0 V - xmlns="http://java.sun.com/xml/ns/javaee" # G) Z3 [5 `% W+ }
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
9 O, U; t0 r. ?) \; B p. k - xsi:schemaLocation="http://java.sun.com/xml/ns/javaee , K/ S$ g5 g0 v& e/ C
- http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
0 m$ K& s9 s' r7 u - <servlet>
D9 v$ w0 M- E# j) t& H - <servlet-name>WechatServlet</servlet-name>) s Q' l9 S/ U8 ~* o$ s$ R
- <servlet-class>demo.servlet.WechatServlet</servlet-class>" G$ b; o( j6 t& N: A4 U' E o
- </servlet>/ _0 P: M$ I7 {0 U# b3 n
- ' Z9 o9 J1 ?7 a9 G) A
- <servlet-mapping>5 U' w) c, G$ b$ t! I* `. \; ?
- <servlet-name>WechatServlet</servlet-name>
& S' w. M [* W6 Z! I+ V4 F M - <url-pattern>/wechat.do</url-pattern>
0 T _, a, r3 \1 x9 l j" | - </servlet-mapping>& C' }; u5 V. V; m+ f r
- <welcome-file-list>
6 d6 ^! w l, W/ m& n6 z$ E! X% I( ~ - <welcome-file>index.jsp</welcome-file>$ t. o) r8 p- B8 N
- </welcome-file-list>
4 k- X q* R$ c/ l' e& _2 |! j - </web-app> T2 g3 w X6 b( H5 E; D
复制代码 + H; F9 k9 D. B4 {. }
项目源码下载地址:
6 J: r5 v" ?- f3 U* H) y& O) U, R# _/ Y$ }! w
6 @: ^ n, X$ h) {
|
|