微信servlet开发
一、web.xml中注册servlet类
<!--微信信息拦截 -->
<servlet>
<servlet-name>wxServlet</servlet-name>
<servlet-class>com.outsource.weixin.client.servlet.WxServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>wxServlet</servlet-name>
<url-pattern>/wxServlet.php</url-pattern>
</servlet-mapping>
二、处理业务的servlet类
package com.outsource.weixin.client.servlet;
/**
*微信信息控制层
*/
public class WxServlet extends HttpServlet {
private static final long serialVersionUID = -2776902810130266533L;
private static Logger log = Logger.getLogger(WxServlet.class.getName());
// 自定义 token
private String TOKEN = "weixin_kz";
public static String wsUrl="http://115.29.49.7";
private WxService wxService;
private StatUserService statUserService;
@Override
public void init() throws ServletException {
super.init();
ServletContext servletContext = this.getServletContext();
WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);
wxService = (WxService)ctx.getBean("wxService");
statUserService = (StatUserService)ctx.getBean("statUserService");
}
@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String xmlReceive = null;
String xmlSend = null;
String msgContent=null;
response.setContentType("application/xml");
response.setCharacterEncoding("UTF-8");
request.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
String openId=null;
try {
xmlReceive = WxUtil.inputStream2String(request.getInputStream());----获取xml形式的参数
try {
Map map = WxUtil.xml2Map(xmlReceive); ----把xml形式的参数转换成map键值对形式
openId=(String) map.get("FromUserName");
SysUser user=statUserService.getSysUser(openId);
//证明学生没有绑定系统账号
if(user==null || user.getSysUserId().equals("")){
xmlSend=wxService.sendText(map,"欢迎关注论文系统微信公众号,请立即绑定认证学生信息," +
"点击<a href='"+wsUrl+"/vsm/weixin/stuwx/jsp/stuUserBound.jsp?openId="+openId+"'>绑定认证.</a>");
}else{
if(map.get("Content")!=null &&!"".equals(map.get("Content")))
map.put("Content", map.get("Content").toString().trim().toLowerCase());
if (map.get("MsgType").equals("text")) {
msgContent=map.get("Content").toString();
}else if(map.get("MsgType").equals("event")){
//订阅
if(map.get("Event").equals("subscribe")){
xmlSend=wxService.sendText(map,"欢迎 "+user.getRealName()+"同学\n登录论文系统微信公众号");
}
//取消订阅
else if(map.get("Event").equals("unsubscribe")){
statUserService.Unbind(openId);
}
else if(map.get("Event").equals("LOCATION")){
xmlSend=wxService.getBaiduAddress(wxService.getBaiduGeocoding(map),map);
}else if(map.get("Event").equals("SCAN")){
System.out.println(map);
}else if(map.get("Event").equals("CLICK")){
//我的信息
if(map.get("EventKey").equals("me")){
xmlSend=wxService.sendText(map,"【学号】:"+user.getLoginName()+"\n" + "【姓名】:"+user.getRealName());
}//上传周记
else if(map.get("EventKey").equals("sczj")){
IsWeeklyEntity iwe=statUserService.getIsWeekly(openId);
//不能写周记
if(iwe.getStatus()==0){
xmlSend=wxService.sendText(map,"实习未开始不能上传周记!");
}else if(iwe.getStatus()==1){
List<Article> articleList=new ArrayList();
Article a=new Article();
a.setTitle("上传第"+iwe.getWeeklyCount()+"周记");
a.setDescription("请按时上传周记!");
a.setPicUrl(wsUrl+"/vsm/upload/image/zj.jpg");
a.setUrl(wsUrl+"/vsm/weixin/stuwx/jsp/uploadWeekly.jsp?openId="+openId+"&weeklyCount="+iwe.getWeeklyCount());
articleList.add(a);
xmlSend=wxService.sendArticle(map, articleList);
}else if(iwe.getStatus()==2){
xmlSend=wxService.sendText(map,"实习已结束!");
}
}
//周记评分查看
else if(map.get("EventKey").equals("zjpfck")){
List<Article> articleList=new ArrayList();
Article a=new Article();
a.setTitle("周记评分查看");
a.setDescription("周记评分查看");
a.setPicUrl(wsUrl+"/vsm/upload/image/zj.jpg");
a.setUrl(wsUrl+"/vsm/upwk.do?getPageWeeklyNotes&openId="+openId);
articleList.add(a);
xmlSend=wxService.sendArticle(map, articleList);
}//学生评教
else if(map.get("EventKey").equals("xspj")){
XspjEntity xj = statUserService.getGuide(openId);
if(xj.getStatus()==0){
xmlSend=wxService.sendText(map,"未分配指导老师,不能评教!");
}else if(xj.getStatus()==1){
List<Article> articleList=new ArrayList();
Article a=new Article();
a.setTitle("学生评教");
a.setDescription("学生评教");
a.setPicUrl(wsUrl+"/vsm/upload/image/pj.jpg");
a.setUrl(wsUrl+"/vsm/weixin/stuwx/jsp/teachingEvaluation.jsp?openId="+openId+"&guideId="+xj.getGuideId()+"&guideName="+xj.getGuideName()+"&studentId="+xj.getStudentId());
System.out.println(wsUrl+"/vsm/weixin/stuwx/jsp/teachingEvaluation.jsp?openId="+openId+"&guideId="+xj.getGuideId()+"&guideName="+xj.getGuideName()+"&studentId="+xj.getStudentId());
articleList.add(a);
xmlSend=wxService.sendArticle(map, articleList);
}else if(xj.getStatus()==2){
xmlSend=wxService.sendText(map,"你已对指导老师【"+xj.getGuideName()+"】评教过了,评分为"+xj.getScore()+"分");
}
}
//成绩查看
else if(map.get("EventKey").equals("sxcjck")){
UserPracticeEntity upe=statUserService.getUserPractice(openId);
if(upe!=null && !upe.getStudent().equals("")){
xmlSend=wxService.sendText(map,"【指导老师】:"+upe.getTeacherName()+
"\n" +"【周记成绩】:"+upe.getWeeklyScore()+
"\n" +"【实习成绩】:"+upe.getReportScore()+
"\n" +"【总成绩】:"+upe.getTotalGrade()+
"\n" +"【总评价】:"+upe.getGradeLabel());
}else{
xmlSend=wxService.sendText(map,"暂无");
}
}
//答辩安排查看
else if(map.get("EventKey").equals("dbap")){
ReplyPlanEntity replyPlan = statUserService.getReplyPlan(openId);
if(replyPlan!=null && !replyPlan.getGroupName().equals("")){
xmlSend=wxService.sendText(map,"【小组名称】:"+replyPlan.getGroupName()+
"\n" +"【答辩时间】:"+replyPlan.getReplyDateTimeChina()+
"\n" +"【答辩地址】:"+replyPlan.getReplyAddress()+
"\n" +"【组长】:"+replyPlan.getStudentLeaderName()+
"\n" +"【联系电话】:"+replyPlan.getStudentLeaderTel()+
"\n" +"【答辩人数】:"+replyPlan.getReplyCount());
}else{
xmlSend=wxService.sendText(map,"暂无");
}
}//论文成绩查看
else if(map.get("EventKey").equals("lwcjck")){
StatUserEntity statUserEntity = statUserService.getStatUser(openId);
if(statUserEntity!=null && !statUserEntity.getRealName().equals("")){
xmlSend=wxService.sendText(map,"【姓名】:"+statUserEntity.getRealName()+
"\n" +"【课题名称】:"+statUserEntity.getTopicName()+
"\n" +"【论文成绩】:"+statUserEntity.getThesisGrade()+
"\n" +"【答辩成绩】:"+statUserEntity.getReplyGrade()+
"\n" +"【总成绩】:"+statUserEntity.getTotalGrade());
}else{
xmlSend=wxService.sendText(map,"暂无");
}
}//中期检查成绩查看
else if(map.get("EventKey").equals("zqjccjck")){
ThesisCheckEntity thesisCheckEntity = statUserService.getCheck(openId);
if(thesisCheckEntity!=null && !thesisCheckEntity.getStudentName().equals("")){
xmlSend=wxService.sendText(map,"【姓名】:"+thesisCheckEntity.getStudentName()+
"\n" +"【课题名称】:"+thesisCheckEntity.getTopicName()+
"\n" +"【评价成绩】:"+thesisCheckEntity.getTotalEvaluate()+" 分"+
"\n" +"【评价建议】:"+thesisCheckEntity.getSuggest());
}else{
xmlSend=wxService.sendText(map,"暂无");
}
}
else{
xmlSend=wxService.sendText(map,"暂无");
}
}
}else if(map.get("MsgType").equals("location")){
}else if(map.get("MsgType").equals("image")){
// xmlSend=wxService.setMakefriends(map);
}else if(map.get("MsgType").equals("voice")){
}
}
System.out.println(xmlSend);
out.write(xmlSend);
} catch (Exception e) {
e.printStackTrace();
log.error(e.getMessage());
}
} finally {
if (out != null) {
out.flush();
out.close();
out = null;
}
}
}
protected void doGet(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"); ----随机字符串
// 重写totring方法,得到三个参数的拼接字符串
List<String> list = new ArrayList<String>(3) {
private static final long serialVersionUID = 2621444383666420433L;
public String toString() {
return this.get(0) + this.get(1) + this.get(2);
}
};
list.add(TOKEN);
list.add(timestamp);
list.add(nonce);
Collections.sort(list);// 排序
String tmpStr = WxUtil.Encrypt(list.toString());// SHA-1加密
Writer out = response.getWriter();
if (signature.equals(tmpStr)) {
out.write(echostr);// 请求验证成功,返回随机码
} else {
out.write("");
}
out.flush();
out.close();
out = null;
}
}