欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java之通过反射机制选择servlet中的对应的方法

程序员文章站 2022-07-07 21:58:40
此方法用于在对Javaee开发中的通过对应的名称而选择servlet中的对应的方法 注:主要代码如下 示例 注:在后面的jsp页面中提交form表单中的 action="<%=request.getContextPath() %>/saveTeacher.ado"可以直接通过反射机制调用servle ......

此方法用于在对javaee开发中的通过对应的名称而选择servlet中的对应的方法

注:主要代码如下

protected void doget(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception {
        /**
         * 这是设置浏览器编码的问题
         */
        req.setcharacterencoding("utf-8");
        resp.setcontenttype("text/html,charset=utf-8");
        
        string servletpath = req.getservletpath();
        string methodname = servletpath.substring(1);
        methodname = methodname.substring(0, methodname.length()-4);
        system.out.println(methodname);
        
        try {
            /**
             * 根据反射机制选择servlet对应的方法
             */
            method method = getclass().getdeclaredmethod(methodname, httpservletrequest.class,
                    httpservletresponse.class);
            /**
             * 利用反射调用对应的方法
             */
            method.invoke(this, req,resp);
        } catch (exception e) {
            e.printstacktrace();
            system.out.println("这里是登录的时候反射机制的地方出错了!");
            req.getrequestdispatcher("error.jsp").forward(req, resp);
        }
    }
    
    @override
    protected void dopost(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception {
        doget(req, resp);
    }

示例

注:在后面的jsp页面中提交form表单中的 action="<%=request.getcontextpath() %>/saveteacher.ado"可以直接通过反射机制调用servlet中的saveteacher方法

public class administratorsservlet extends httpservlet {

    private static final long serialversionuid = 1l;
    
    @override
    protected void doget(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception {
        /**
         * 这是设置浏览器编码的问题
         */
        req.setcharacterencoding("utf-8");
        resp.setcontenttype("text/html,charset=utf-8");
        
        string servletpath = req.getservletpath();
        string methodname = servletpath.substring(1);
        methodname = methodname.substring(0, methodname.length()-4);
        system.out.println(methodname);
        
        try {
            /**
             * 根据反射机制选择servlet对应的方法
             */
            method method = getclass().getdeclaredmethod(methodname, httpservletrequest.class,
                    httpservletresponse.class);
            /**
             * 利用反射调用对应的方法
             */
            method.invoke(this, req,resp);
        } catch (exception e) {
            e.printstacktrace();
            system.out.println("这里是登录的时候反射机制的地方出错了!");
            req.getrequestdispatcher("error.jsp").forward(req, resp);
        }
    }
    
    @override
    protected void dopost(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception {
        doget(req, resp);
    }
    
    /**
     * 基于对教师的基本操作    
     */
    
    /**
     * 添加一个教师
     * @param req
     * @param resp
     * @throws servletexception
     * @throws ioexception
     */
    private void saveteacher(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception {
        try {
            teachers teacher = new teachers();
            teacher.setteacherid(integer.parseint(req.getparameter("teacherid")));
            teacher.setteachername(req.getparameter("teachername"));
            teacher.setsex(req.getparameter("sex"));
            teacher.setphoneno(req.getparameter("phoneno"));
            teacher.setaddress(req.getparameter("address"));
            teacher.setbirthday(new date(new simpledateformat("yyyy-mm-dd").parse(req.getparameter("birthday")).gettime()));
            teacher.setsalary(new bigdecimal(req.getparameter("salary")));
            iteachersservice teacherservice = new teachersserviceimpl();
            int rows = teacherservice.save(teacher);
            if(rows>0) {
                system.out.println("保存一个教师基本信息成功!");
                req.getrequestdispatcher("savesuccess.jsp").forward(req, resp);
            }
        } catch (exception e) {
            e.printstacktrace();
            system.out.println("添加一个教师对象基本信息的时候失败!");
            resp.sendredirect("error.jsp");
        }

    }
}

一、首先在web.xml文件里面配置相关的信息如下

注: <url-pattern>*.ado</url-pattern>  中的*则代表所选的对应方法
<servlet>
      <servlet-name>administratorsservlet</servlet-name>
      <servlet-class>cn.gbb.student_achievement_system.controller.administratorsservlet</servlet-class>
  </servlet>
  <servlet-mapping>
      <servlet-name>administratorsservlet</servlet-name>
      <url-pattern>*.ado</url-pattern>
  </servlet-mapping>

二、在jsp页面中from表单提交的时候需要注意它提交的action里面的内容

注: <form action="<%=request.getcontextpath() %>/saveteacher.ado" method="get">
 1 <body style="height: 100%;width: 100%; background-image:url(lib/skr.jpg); background-size: cover;">
 2         <div style="height: 100px;width: 100%; text-align: center;">
 3             <h1>学生成绩管理系统</h1>
 4         </div>
 5         <div align="center">
 6                 <form action="<%=request.getcontextpath() %>/saveteacher.ado" method="get">
 7                     <%
 8                         teachers_login teacherlogin = (teachers_login) request.getattribute("teacherlogin");
 9                     %>
10                         <input type="hidden" name="teacherid" value="<%=teacherlogin.getteacherid()%>">
11                         <input type="hidden" name="teachername" value="<%=teacherlogin.getteachername()%>">
12                     <table>
13                         <tr>
14                             <th colspan="2" align="center">请填写教师的基本信息</th>
15                         </tr>
16                         <tr>
17                             <td>职工号:</td>
18                             <td><%=teacherlogin.getteacherid()%></td>
19                         </tr>
20                         <tr>
21                             <td>姓名:</td>
22                             <td><%=teacherlogin.getteachername()%></td>
23                         </tr>
24                         <tr>
25                             <td>性别:</td>
26                             <td><input type="text" name="sex"></td>
27                         </tr>
28                         <tr>
29                             <td>电话号码:</td>
30                             <td><input type="text" name="phoneno"></td>
31                         </tr>
32                         <tr>
33                             <td>家庭住址:</td>
34                             <td><input type="text" name="address"></td>
35                         </tr>
36                         <tr>
37                             <td>出生日期:</td>
38                             <td><input type="text" name="birthday"></td>
39                         </tr>
40                         <tr>
41                             <td>工资:</td>
42                             <td><input type="text" name="salary"></td>
43                         </tr>
44                         <tr>
45                             <td colspan="2" style="text-align: center;"><input type="submit" value="确定"><input type="reset" value="重置"></td>
46                         </tr>
47                     </table>
48                 </form>
49                 <p style="text-align: center;"><a href="administratorsmain.jsp">返回首页</a></p>
50         </div>
51 </body>

        总结:这样就可以在jsp页面提交的时候选择servlet里面的所对应的方法了,希望对您有所帮助,谢谢!!!!!