1、首先建一个SampleServlet类,(名字随意。)继承 HttpServlet
package com.maojd.test; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class SampleServlet extends HttpServlet{ private static final long serialVersionUID = -5404916983906926869L; /* @Override public void init() throws ServletException { super.init(); }*/ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // super.doGet(request, response); response.setContentType("text/plain"); PrintWriter out = response.getWriter(); System.out.println("请求SampleServlet GET Method mao"); out.print("请求SampleServlet GET Method mao"); out.flush(); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //super.doPost(request, response); this.doGet(request, response); response.setContentType("text/plain"); PrintWriter out = response.getWriter(); System.out.println("请求SampleServlet GET Method mao"); out.print("请求SampleServlet POST Method mao"); out.flush(); } }
2、配置servlet的路径
a) Web目录下建 WEB-INF
b)WEB-INF下建一个 web-custom.xml文件,配置对应servlet映射信息。这个文件相当于普通web工程的web.xml
web-custom.xml内容如下:
<?xml version='1.0' encoding='ISO-8859-1'?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app> <!-- Servlets --> <servlet> <servlet-name>SampleServlet</servlet-name> <servlet-class>com.maojd.test.SampleServlet</servlet-class> </servlet> <!-- Servlet mappings --> <servlet-mapping> <servlet-name>SampleServlet</servlet-name> <url-pattern>/servlet</url-pattern> </servlet-mapping> </web-app>
说明:( 重要!!!!!! )
Servlet的配置 url必须全部小写。有一个大写字母则找不到 对象的servlet 。
配置多个servlet,和单个方法一样。配置多个servlet 和 servlet-mapping即可。
3、在plugin.xml文件中配置JSP页面显示位置
<adminconsole> <tab id="tab-server"> <sidebar id="sidebar-server-settings"> <item id="sample-service" name="Sample Service maojd" url="myplugin-demo1.jsp" description="Click is trigger sample plugin" /> </sidebar> </tab> </adminconsole>
备注:item标签中的id是自己定义的,要保证唯一,不能与其他的插件冲突,因此我们可以将这个ID的命名规则设为( 插件名-页面 ) 。JSP页面会根据这个ID来确定左侧菜单栏的显示位置。
4、创建JSP页面
Web目录下建jsp页面 myplugin-demo1.jsp, 命名规范: 插件名-页面名.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>hello world: 你好openfire</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <meta name="pageID" content="sample-service"/> </head> <body> <h3>hello world jsp!! <a href="/plugins/myplugin/servlet">SampleServlet</a></h3> <div class="jive-contentBoxHeader">jive-contentBoxHeader</div> <div class="jive-contentBox">jive-contentBox</div> <div class="jive-table"> <table cellpadding="0" cellspacing="0" border="0" width="100%"> <thead> <tr> <th> sss</th> <th nowrap>a</th> <th nowrap>b</th> </tr> </thead> <tbody> <tr> <td align="center">asdf</td> <td align="center">asdf</td> <td align="center">asdf</td> </tr> <tr class="jive-even"> <td align="center">asdf</td> <td align="center">asdf</td> <td align="center">asdf</td> </tr> <tr class="jive-odd"> <td align="center">asdf</td> <td align="center">asdf</td> <td align="center">asdf</td> </tr> </tbody> </table> </div> </body> </html>
备注:Jsp需要注意:<meta name="pageID" content="sample-service"/>
PageID固定就这样不变, Sample-service可以随便,但是一定要和标题对应ID保持一致(top 导航id和左侧导航id对应), 一般是 和 plugin.xml 对应 。